diff --git a/sdk/schemaregistry/schema-registry-avro/test/public/utils/mockedRegistryClient.ts b/sdk/schemaregistry/schema-registry-avro/test/public/utils/mockedRegistryClient.ts index c06430f02aec..2f3a75111430 100644 --- a/sdk/schemaregistry/schema-registry-avro/test/public/utils/mockedRegistryClient.ts +++ b/sdk/schemaregistry/schema-registry-avro/test/public/utils/mockedRegistryClient.ts @@ -17,6 +17,8 @@ import { isLive } from "./isLive"; import { testSchemaIds } from "./dummies"; import { v4 as uuid } from "uuid"; +type UpdatedSchemaDescription = Required>; + function getEnvVar(name: string): string { const value = env[name]; if (!value) { @@ -59,7 +61,7 @@ function createMockedTestRegistry(): SchemaRegistry { return { registerSchema, getSchemaProperties, getSchema }; async function registerSchema( - schema: SchemaDescription, + schema: UpdatedSchemaDescription, _options?: RegisterSchemaOptions ): Promise { let result = mapByContent.get(schema.definition); @@ -93,7 +95,7 @@ function createMockedTestRegistry(): SchemaRegistry { } async function getSchemaProperties( - schema: SchemaDescription, + schema: UpdatedSchemaDescription, _options?: GetSchemaPropertiesOptions ): Promise { const storedSchema = mapByContent.get(schema.definition); diff --git a/sdk/schemaregistry/schema-registry/CHANGELOG.md b/sdk/schemaregistry/schema-registry/CHANGELOG.md index 7191231fdb64..c382066b627c 100644 --- a/sdk/schemaregistry/schema-registry/CHANGELOG.md +++ b/sdk/schemaregistry/schema-registry/CHANGELOG.md @@ -4,7 +4,7 @@ ### Features Added -- `getSchemaByVersion` is added to query schemas by their version. +- An overload of `getSchema` is added that queries schemas by their version. - `version` property is added to `SchemaProperties`. ## 1.1.0 (2022-05-10) diff --git a/sdk/schemaregistry/schema-registry/README.md b/sdk/schemaregistry/schema-registry/README.md index 46e78c5daa3e..22cba07e34a3 100644 --- a/sdk/schemaregistry/schema-registry/README.md +++ b/sdk/schemaregistry/schema-registry/README.md @@ -125,7 +125,7 @@ const { DefaultAzureCredential } = require("@azure/identity"); const { SchemaRegistryClient } = require("@azure/schema-registry"); const client = new SchemaRegistryClient("", new DefaultAzureCredential()); -const foundSchema = await client.getSchemaByVersion({ name:"", groupName: "group name", version }); +const foundSchema = await client.getSchema({ name:"", groupName: "group name", version }); if (foundSchema) { console.log(`Got schema definition=${foundSchema.definition}`); } diff --git a/sdk/schemaregistry/schema-registry/review/schema-registry.api.md b/sdk/schemaregistry/schema-registry/review/schema-registry.api.md index 61ea60ed7d3a..b6a1d882e5b5 100644 --- a/sdk/schemaregistry/schema-registry/review/schema-registry.api.md +++ b/sdk/schemaregistry/schema-registry/review/schema-registry.api.md @@ -8,10 +8,6 @@ import { CommonClientOptions } from '@azure/core-client'; import { OperationOptions } from '@azure/core-client'; import { TokenCredential } from '@azure/core-auth'; -// @public -export interface GetSchemaByVersionOptions extends OperationOptions { -} - // @public export interface GetSchemaOptions extends OperationOptions { } @@ -32,10 +28,11 @@ export interface Schema { // @public export interface SchemaDescription { - definition: string; - format: string; + definition?: string; + format?: string; groupName: string; name: string; + version?: number; } // @public @@ -59,7 +56,7 @@ export class SchemaRegistryClient implements SchemaRegistry { constructor(fullyQualifiedNamespace: string, credential: TokenCredential, options?: SchemaRegistryClientOptions); readonly fullyQualifiedNamespace: string; getSchema(schemaId: string, options?: GetSchemaOptions): Promise; - getSchemaByVersion(schemaVersion: SchemaVersion, options?: GetSchemaByVersionOptions): Promise; + getSchema(schemaDescription: SchemaDescription, options?: GetSchemaOptions): Promise; getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise; registerSchema(schema: SchemaDescription, options?: RegisterSchemaOptions): Promise; } @@ -69,13 +66,6 @@ export interface SchemaRegistryClientOptions extends CommonClientOptions { apiVersion?: string; } -// @public -export interface SchemaVersion { - groupName: string; - name: string; - version: number; -} - // (No @packageDocumentation comment for this package) ``` diff --git a/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts b/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts index f85c8e189686..2f7923c9bd05 100644 --- a/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts +++ b/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts @@ -55,7 +55,7 @@ export async function main() { ); // Get definition of existing schema by its version - const foundSchema = await client.getSchemaByVersion({ + const foundSchema = await client.getSchema({ groupName, name, version, diff --git a/sdk/schemaregistry/schema-registry/src/models.ts b/sdk/schemaregistry/schema-registry/src/models.ts index 2ca84fff31df..ac1fc20699ac 100644 --- a/sdk/schemaregistry/schema-registry/src/models.ts +++ b/sdk/schemaregistry/schema-registry/src/models.ts @@ -25,22 +25,6 @@ export interface SchemaProperties { version: number; } -/** - * Version of a schema - */ -export interface SchemaVersion { - /** - * Version of the schema - */ - version: number; - - /** Schema group under which schema is or should be registered. */ - groupName: string; - - /** Name of schema.*/ - name: string; -} - /** * Schema definition with its name, format, and group. */ @@ -54,10 +38,13 @@ export interface SchemaDescription { /** * The format of schema and it must match the serialization type of the schema's group. */ - format: string; + format?: string; /** String representation of schema. */ - definition: string; + definition?: string; + + /** Version of schema */ + version?: number; } /** @@ -95,11 +82,6 @@ export interface GetSchemaPropertiesOptions extends OperationOptions {} */ export interface GetSchemaOptions extends OperationOptions {} -/** - * Options to configure SchemaRegistryClient.getSchemaByVersion. - */ -export interface GetSchemaByVersionOptions extends OperationOptions {} - /** * Represents a store of registered schemas. * diff --git a/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts b/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts index d2b3ea39b1a9..9d960c64b1aa 100644 --- a/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts +++ b/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts @@ -3,7 +3,6 @@ import { DEFAULT_SCOPE, SDK_VERSION } from "./constants"; import { - GetSchemaByVersionOptions, GetSchemaOptions, GetSchemaPropertiesOptions, RegisterSchemaOptions, @@ -12,7 +11,6 @@ import { SchemaProperties, SchemaRegistry, SchemaRegistryClientOptions, - SchemaVersion, } from "./models"; import { InternalPipelineOptions, @@ -22,6 +20,7 @@ import { TracingClient, createTracingClient } from "@azure/core-tracing"; import { convertSchemaIdResponse, convertSchemaResponse } from "./conversions"; import { GeneratedSchemaRegistryClient } from "./generated/generatedSchemaRegistryClient"; import { TokenCredential } from "@azure/core-auth"; +import { isDefined } from "./utils"; import { logger } from "./logger"; /** @@ -91,7 +90,12 @@ export class SchemaRegistryClient implements SchemaRegistry { schema: SchemaDescription, options: RegisterSchemaOptions = {} ): Promise { - const { groupName, name: schemaName, definition: schemaContent, format } = schema; + const { + groupName, + name: schemaName, + definition: schemaContent, + format, + } = isDefined(schema, ["definition", "format"]); return this._tracing.withSpan( "SchemaRegistryClient.registerSchema", options, @@ -119,7 +123,12 @@ export class SchemaRegistryClient implements SchemaRegistry { schema: SchemaDescription, options: GetSchemaPropertiesOptions = {} ): Promise { - const { groupName, name: schemaName, definition: schemaContent, format } = schema; + const { + groupName, + name: schemaName, + definition: schemaContent, + format, + } = isDefined(schema, ["definition", "format"]); return this._tracing.withSpan( "SchemaRegistryClient.getSchemaProperties", options, @@ -153,11 +162,7 @@ export class SchemaRegistryClient implements SchemaRegistry { * @param schemaId - Unique schema ID. * @returns Schema with given ID. */ - getSchema(schemaId: string, options: GetSchemaOptions = {}): Promise { - return this._tracing.withSpan("SchemaRegistryClient.getSchema", options, (updatedOptions) => - this._client.schema.getById(schemaId, updatedOptions).then(convertSchemaResponse) - ); - } + getSchema(schemaId: string, options?: GetSchemaOptions): Promise; /** * Gets an existing schema by version. If the schema was not found, a RestError with @@ -173,14 +178,21 @@ export class SchemaRegistryClient implements SchemaRegistry { } * ``` * - * @param schemaVersion - schema version. + * @param schemaDescription - schema version. * @returns Schema with given ID. */ - getSchemaByVersion( - schemaVersion: SchemaVersion, - options: GetSchemaByVersionOptions = {} + getSchema(schemaDescription: SchemaDescription, options?: GetSchemaOptions): Promise; + // implementation + getSchema( + schemaDescription: string | SchemaDescription, + options: GetSchemaOptions = {} ): Promise { - const { groupName, name, version } = schemaVersion; + if (typeof schemaDescription === "string") { + return this._tracing.withSpan("SchemaRegistryClient.getSchema", options, (updatedOptions) => + this._client.schema.getById(schemaDescription, updatedOptions).then(convertSchemaResponse) + ); + } + const { groupName, name, version } = isDefined(schemaDescription, ["version"]); return this._tracing.withSpan( "SchemaRegistryClient.getSchemaByVersion", options, diff --git a/sdk/schemaregistry/schema-registry/src/utils.ts b/sdk/schemaregistry/schema-registry/src/utils.ts new file mode 100644 index 000000000000..3f5927240bda --- /dev/null +++ b/sdk/schemaregistry/schema-registry/src/utils.ts @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +type WithRequired = T & { [P in K]-?: T[P] }; + +export const isDefined = , V extends Array>( + obj: T, + props: V +): WithRequired => { + for (const prop of props) { + if (obj[prop] === undefined) { + throw new Error(`Expected ${String(prop)} to be present`); + } + } + + return obj as WithRequired; +}; diff --git a/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts b/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts index ffb90b0de774..ec9d203df192 100644 --- a/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts +++ b/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts @@ -188,10 +188,6 @@ describe("SchemaRegistryClient", function () { // NOTE: IDs may differ here as we could get a different version with same definition. }); - it("fails to get schema by ID when given invalid ID", async () => { - await isRejected(client.getSchema(null!), { messagePattern: /null/ }); - }); - it("fails to get schema when no schema exists with given ID", async () => { await isRejected(client.getSchema("ffffffffffffffffffffffffffffffff", errorOptions), { statusCode: 404, @@ -215,7 +211,7 @@ describe("SchemaRegistryClient", function () { it("gets schema by version", async () => { const registered = await client.registerSchema(schema, options); assertIsValidSchemaProperties(registered); - const found = await client.getSchemaByVersion( + const found = await client.getSchema( { groupName: registered.groupName, name: registered.name, @@ -232,7 +228,7 @@ describe("SchemaRegistryClient", function () { }); it("schema with whitespace", async () => { - const schema2: SchemaDescription = { + const schema2 = { name: "azsdk_js_test2", groupName: assertEnvironmentVariable("SCHEMA_REGISTRY_GROUP"), format: "Avro",