Skip to content

Commit

Permalink
[Schema Registry Avro] make schema group optional (#17922)
Browse files Browse the repository at this point in the history
* [Schema Registry Avro] make schema group optional

* update the changelog

* address feedback
  • Loading branch information
deyaaeldeen authored Sep 30, 2021
1 parent 3ebdf60 commit 0dea73f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
2 changes: 2 additions & 0 deletions sdk/schemaregistry/schema-registry-avro/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sdk/schemaregistry/schema-registry-avro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const { SchemaRegistryClient } = require("@azure/schema-registry");
const { SchemaRegistryAvroSerializer } = require("@azure/schema-registry-avro");

const client = new SchemaRegistryClient("<endpoint>", new DefaultAzureCredential());
const serializer = new SchemaRegistryAvroSerializer(client, "<group>");
const serializer = new SchemaRegistryAvroSerializer(client, { groupName: "<group>" });

// Example Avro schema
const schema = JSON.stringify({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ 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<unknown>;
serialize(value: unknown, schema: string): Promise<Uint8Array>;
}

// @public
export interface SchemaRegistryAvroSerializerOptions {
autoRegisterSchemas?: boolean;
groupName?: string;
}

// (No @packageDocumentation comment for this package)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
Expand Down

0 comments on commit 0dea73f

Please sign in to comment.