From c9f723de9b6029049a6353f985cacb172b53e112 Mon Sep 17 00:00:00 2001 From: Maciej Kusnierz Date: Tue, 4 Jun 2024 09:36:58 +0200 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20handle=20protobuf=20s?= =?UTF-8?q?chemas=20referenced=20by=20-1=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It is possible to reference a schema with -1 version which means 'latest'. Get the latest version number for such schemas to avoid duplicates --- src/SchemaRegistry.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/SchemaRegistry.ts b/src/SchemaRegistry.ts index cb8ce32..d0ebe71 100644 --- a/src/SchemaRegistry.ts +++ b/src/SchemaRegistry.ts @@ -214,6 +214,17 @@ export default class SchemaRegistry { referencesSet: Set, ): Promise { const { name, subject, version } = reference + + // if version is -1, get the latest version to handle duplicates correctly + if (version === -1) { + const latestRegistryVersionNumber = await this.getLatestSchemaVersionNumber(subject) + return this.getreferencedSchemasFromReference( + { name, subject, version: latestRegistryVersionNumber }, + helper, + referencesSet, + ) + } + const key = `${name}-${subject}-${version}` // avoid duplicates @@ -370,6 +381,11 @@ export default class SchemaRegistry { return id } + async getLatestSchemaVersionNumber(subject: string): Promise { + const response = await this.api.Subject.latestVersion({ subject }) + const { version }: { version: number } = response.data() + return version + } private getSchemaOriginRequest(registryId: number) { // ensure that cache-misses result in a single origin request if (this.cacheMissRequests[registryId]) { From 8a312586d2d1dd03ab229aa17fa96395249af371 Mon Sep 17 00:00:00 2001 From: Maciej Kusnierz Date: Tue, 4 Jun 2024 15:54:59 +0200 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20simplify=20-1=20?= =?UTF-8?q?version=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SchemaRegistry.ts | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/SchemaRegistry.ts b/src/SchemaRegistry.ts index d0ebe71..0bafc03 100644 --- a/src/SchemaRegistry.ts +++ b/src/SchemaRegistry.ts @@ -213,18 +213,13 @@ export default class SchemaRegistry { helper: SchemaHelper, referencesSet: Set, ): Promise { - const { name, subject, version } = reference - - // if version is -1, get the latest version to handle duplicates correctly - if (version === -1) { - const latestRegistryVersionNumber = await this.getLatestSchemaVersionNumber(subject) - return this.getreferencedSchemasFromReference( - { name, subject, version: latestRegistryVersionNumber }, - helper, - referencesSet, - ) - } + const { name, subject } = reference + const versionResponse = await this.api.Subject.version(reference) + const foundSchema = versionResponse.data() as SchemaResponse + + // rely on version retrieved from the registry, because references may use -1 as version + const { version } = foundSchema const key = `${name}-${subject}-${version}` // avoid duplicates @@ -233,9 +228,6 @@ export default class SchemaRegistry { } referencesSet.add(key) - const versionResponse = await this.api.Subject.version(reference) - const foundSchema = versionResponse.data() as SchemaResponse - const schema = helper.toConfluentSchema(foundSchema) const referencedSchemas = await this.getreferencedSchemasRecursive( schema, @@ -381,11 +373,6 @@ export default class SchemaRegistry { return id } - async getLatestSchemaVersionNumber(subject: string): Promise { - const response = await this.api.Subject.latestVersion({ subject }) - const { version }: { version: number } = response.data() - return version - } private getSchemaOriginRequest(registryId: number) { // ensure that cache-misses result in a single origin request if (this.cacheMissRequests[registryId]) {