Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/schema references #121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 72 additions & 4 deletions src/SchemaRegistry.ts
Original file line number Diff line number Diff line change
@@ -20,21 +20,36 @@ import {
ConfluentSubject,
SchemaRegistryAPIClientOptions,
AvroConfluentSchema,
ProtocolOptions,
LegacyOptions
} from './@types'
import {
helperTypeFromSchemaType,
schemaTypeFromString,
schemaFromConfluentSchema,
} from './schemaTypeResolver'
import { Type } from 'avsc/types'

interface RegisteredSchema {
id: number
}

interface SchemaVersion {
subject: string
version: number
}

interface Opts {
compatibility?: COMPATIBILITY
separator?: string
subject: string,
referenceSchemaIds?: number[]
}

interface Reference {
name: string
subject: string
version: number
}

const DEFAULT_OPTS = {
@@ -97,12 +112,23 @@ export default class SchemaRegistry {
schema: RawAvroSchema | ConfluentSchema,
userOpts?: Opts,
): Promise<RegisteredSchema> {
const { compatibility, separator } = { ...DEFAULT_OPTS, ...userOpts }
const { compatibility, separator, referenceSchemaIds } = { ...DEFAULT_OPTS, ...userOpts }
let opts = this.options;

if (referenceSchemaIds) {
const referenceSchemas = Object.assign({}, ...await Promise.all(referenceSchemaIds.map(async (id) => {
const referenceSchema = await this.getSchema(id) as AvroSchema
return { [referenceSchema.name]: referenceSchema }
})))

opts = this.populateRegistryWithReferenceSchemas(referenceSchemas)
}

const confluentSchema: ConfluentSchema = this.getConfluentSchema(schema)

const helper = helperTypeFromSchemaType(confluentSchema.type)
const schemaInstance = schemaFromConfluentSchema(confluentSchema, this.options)
const schemaInstance = schemaFromConfluentSchema(confluentSchema, opts)
// const schemaInstance = schemaFromConfluentSchema(confluentSchema, this.options, referenceSchemas)
helper.validate(schemaInstance)

let subject: ConfluentSubject
@@ -133,11 +159,26 @@ export default class SchemaRegistry {
}
}

let references;
if (referenceSchemaIds) {
references = await Promise.all(referenceSchemaIds.map(async (id) => {
const refResponseData: SchemaVersion[] = (await this.api.Schema.versions({ id })).data()
const name = refResponseData[0].subject.split(separator).slice(-1)[0];
return {
name,
subject: refResponseData[0].subject,
version: refResponseData[0].version
}
}))
}


const response = await this.api.Subject.register({
subject: subject.name,
body: {
schemaType: confluentSchema.type,
schema: confluentSchema.schema,
references
},
})

@@ -150,13 +191,26 @@ export default class SchemaRegistry {

public async getSchema(registryId: number): Promise<Schema | AvroSchema> {
const schema = this.cache.getSchema(registryId)
let opts = this.options

if (schema) {
return schema
}

const response = await this.getSchemaOriginRequest(registryId)
const foundSchema: { schema: string; schemaType: string } = response.data()
const foundSchema: { schema: string; schemaType: string; references?: Reference[]; } = response.data()

if (foundSchema.references) {
const referenceSchemas = Object.assign({}, ...await Promise.all(foundSchema.references.map(async (reference) => {
const referenceSchemaId = await this.getRegistryId(reference.subject, reference.version)
// @ts-ignore TODO: Fix typings for Schema...
const referenceType: Type = await this.getSchema(referenceSchemaId)
return { [reference.subject]: referenceType }
})));

opts = this.populateRegistryWithReferenceSchemas(referenceSchemas);
}

const rawSchema = foundSchema.schema
const schemaType = schemaTypeFromString(foundSchema.schemaType)

@@ -168,10 +222,24 @@ export default class SchemaRegistry {
type: schemaType,
schema: rawSchema,
}
const schemaInstance = schemaFromConfluentSchema(confluentSchema, this.options)
const schemaInstance = schemaFromConfluentSchema(confluentSchema, opts)
return this.cache.setSchema(registryId, schemaInstance)
}

private populateRegistryWithReferenceSchemas(referenceSchemas: { [x: string]: Type }) {
const schemaOptions = (this.options as LegacyOptions)?.forSchemaOptions || (this.options as ProtocolOptions)?.[SchemaType.AVRO]

return {
[SchemaType.AVRO]: {
...schemaOptions,
registry: {
...schemaOptions?.registry,
...referenceSchemas
}
}
}
}

public async encode(registryId: number, payload: any): Promise<Buffer> {
if (!registryId) {
throw new ConfluentSchemaRegistryArgumentError(
5 changes: 5 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ export interface SchemaRegistryAPIClientArgs {
export type SchemaRegistryAPIClient = Client<{
Schema: {
find: (_: any) => any
versions: (_: any) => any
}
Subject: {
all: (_: any) => any
@@ -65,6 +66,10 @@ export default ({
method: 'get',
path: '/schemas/ids/{id}',
},
versions: {
method: 'get',
path: '/schemas/ids/{id}/versions'
}
},
Subject: {
all: {