From 0dea73faf815dbb697690f02196600afe22f64f3 Mon Sep 17 00:00:00 2001 From: Deyaaeldeen Almahallawi Date: Wed, 29 Sep 2021 20:16:32 -0400 Subject: [PATCH] [Schema Registry Avro] make schema group optional (#17922) * [Schema Registry Avro] make schema group optional * update the changelog * address feedback --- .../schema-registry-avro/CHANGELOG.md | 2 ++ .../schema-registry-avro/README.md | 2 +- .../review/schema-registry-avro.api.md | 3 ++- .../samples-dev/schemaRegistryAvroSample.ts | 2 +- .../src/schemaRegistryAvroSerializer.ts | 24 +++++++++++-------- .../test/utils/mockedSerializer.ts | 2 +- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/sdk/schemaregistry/schema-registry-avro/CHANGELOG.md b/sdk/schemaregistry/schema-registry-avro/CHANGELOG.md index cec37c91c27a..0b299c74a2f2 100644 --- a/sdk/schemaregistry/schema-registry-avro/CHANGELOG.md +++ b/sdk/schemaregistry/schema-registry-avro/CHANGELOG.md @@ -6,6 +6,8 @@ ### Breaking Changes +- `schemaGroup` is no longer a constructor parameter and has been moved to the constructor options because it is only required for serialization. + ### Bugs Fixed ### Other Changes diff --git a/sdk/schemaregistry/schema-registry-avro/README.md b/sdk/schemaregistry/schema-registry-avro/README.md index 43520a2fa295..0d8dafb5a8d8 100644 --- a/sdk/schemaregistry/schema-registry-avro/README.md +++ b/sdk/schemaregistry/schema-registry-avro/README.md @@ -71,7 +71,7 @@ const { SchemaRegistryClient } = require("@azure/schema-registry"); const { SchemaRegistryAvroSerializer } = require("@azure/schema-registry-avro"); const client = new SchemaRegistryClient("", new DefaultAzureCredential()); -const serializer = new SchemaRegistryAvroSerializer(client, ""); +const serializer = new SchemaRegistryAvroSerializer(client, { groupName: "" }); // Example Avro schema const schema = JSON.stringify({ diff --git a/sdk/schemaregistry/schema-registry-avro/review/schema-registry-avro.api.md b/sdk/schemaregistry/schema-registry-avro/review/schema-registry-avro.api.md index 8a2a97833133..5dd99b436358 100644 --- a/sdk/schemaregistry/schema-registry-avro/review/schema-registry-avro.api.md +++ b/sdk/schemaregistry/schema-registry-avro/review/schema-registry-avro.api.md @@ -10,7 +10,7 @@ import { SchemaRegistry } from '@azure/schema-registry'; // @public export class SchemaRegistryAvroSerializer { - constructor(client: SchemaRegistry, groupName: string, options?: SchemaRegistryAvroSerializerOptions); + constructor(client: SchemaRegistry, options?: SchemaRegistryAvroSerializerOptions); deserialize(input: Buffer | Blob | Uint8Array): Promise; serialize(value: unknown, schema: string): Promise; } @@ -18,6 +18,7 @@ export class SchemaRegistryAvroSerializer { // @public export interface SchemaRegistryAvroSerializerOptions { autoRegisterSchemas?: boolean; + groupName?: string; } // (No @packageDocumentation comment for this package) diff --git a/sdk/schemaregistry/schema-registry-avro/samples-dev/schemaRegistryAvroSample.ts b/sdk/schemaregistry/schema-registry-avro/samples-dev/schemaRegistryAvroSample.ts index 3dafe4dd173f..5fd0bee5c071 100644 --- a/sdk/schemaregistry/schema-registry-avro/samples-dev/schemaRegistryAvroSample.ts +++ b/sdk/schemaregistry/schema-registry-avro/samples-dev/schemaRegistryAvroSample.ts @@ -60,7 +60,7 @@ export async function main() { await client.registerSchema(schemaDescription); // Create a new serializer backed by the client - const serializer = new SchemaRegistryAvroSerializer(client, groupName); + const serializer = new SchemaRegistryAvroSerializer(client, { groupName }); // serialize an object that matches the schema const value: User = { firstName: "Jane", lastName: "Doe" }; diff --git a/sdk/schemaregistry/schema-registry-avro/src/schemaRegistryAvroSerializer.ts b/sdk/schemaregistry/schema-registry-avro/src/schemaRegistryAvroSerializer.ts index ceb1bdc79e8d..4c945b3832d0 100644 --- a/sdk/schemaregistry/schema-registry-avro/src/schemaRegistryAvroSerializer.ts +++ b/sdk/schemaregistry/schema-registry-avro/src/schemaRegistryAvroSerializer.ts @@ -54,6 +54,11 @@ export interface SchemaRegistryAvroSerializerOptions { * Automatic schema registration is NOT recommended for production scenarios. */ autoRegisterSchemas?: boolean; + /** + * The group name to be used when registering/looking up a schema. Must be specified + * if you will be calling `serialize`. + */ + groupName?: string; } /** @@ -66,21 +71,14 @@ export class SchemaRegistryAvroSerializer { * * @param client - Schema Registry where schemas are registered and obtained. * Usually this is a SchemaRegistryClient instance. - * - * @param groupName - The schema group to use when making requests to the - * registry. */ - constructor( - client: SchemaRegistry, - groupName: string, - options?: SchemaRegistryAvroSerializerOptions - ) { + constructor(client: SchemaRegistry, options?: SchemaRegistryAvroSerializerOptions) { this.registry = client; - this.schemaGroup = groupName; + this.schemaGroup = options?.groupName; this.autoRegisterSchemas = options?.autoRegisterSchemas ?? false; } - private readonly schemaGroup: string; + private readonly schemaGroup?: string; private readonly registry: SchemaRegistry; private readonly autoRegisterSchemas: boolean; @@ -190,6 +188,12 @@ export class SchemaRegistryAvroSerializer { throw new Error("Schema must have a name."); } + if (!this.schemaGroup) { + throw new Error( + "Schema group must have been specified in the constructor options when the client was created in order to serialize." + ); + } + const description: SchemaDescription = { groupName: this.schemaGroup, name: avroType.name, diff --git a/sdk/schemaregistry/schema-registry-avro/test/utils/mockedSerializer.ts b/sdk/schemaregistry/schema-registry-avro/test/utils/mockedSerializer.ts index 0882d8704238..2efdde39a394 100644 --- a/sdk/schemaregistry/schema-registry-avro/test/utils/mockedSerializer.ts +++ b/sdk/schemaregistry/schema-registry-avro/test/utils/mockedSerializer.ts @@ -13,7 +13,7 @@ export async function createTestSerializer( if (!autoRegisterSchemas) { await registerTestSchema(registry); } - return new SchemaRegistryAvroSerializer(registry, testGroup, { autoRegisterSchemas }); + return new SchemaRegistryAvroSerializer(registry, { autoRegisterSchemas, groupName: testGroup }); } export async function registerTestSchema(registry: SchemaRegistry): Promise {