From 86092bcf8d1f0ccc332a4aa8d6ea44a6b4f2a1ec Mon Sep 17 00:00:00 2001 From: Deyaaeldeen Almahallawi Date: Thu, 8 Sep 2022 18:07:42 -0700 Subject: [PATCH 1/4] [Schema Registry] Add getSchemaByVersion --- sdk/schemaregistry/schema-registry/README.md | 13 ++++ .../schema-registry/package.json | 2 +- .../review/schema-registry.api.md | 14 ++++ .../samples-dev/getSchemaByVersion.ts | 66 +++++++++++++++++++ .../samples-dev/schemaRegistrySample.ts | 29 ++++---- .../schema-registry/src/conversions.ts | 6 +- .../generatedSchemaRegistryClient.ts | 58 ++++++++++++++++ .../generatedSchemaRegistryClientContext.ts | 59 ----------------- .../src/generated/models/index.ts | 46 +++++++++++++ .../src/generated/models/mappers.ts | 66 +++++++++++++++++++ .../src/generated/models/parameters.ts | 11 ++++ .../src/generated/operations/schema.ts | 49 ++++++++++++++ .../generated/operationsInterfaces/schema.ts | 16 +++++ .../src/getSchemaDefinition.browser.ts | 2 +- .../src/getSchemaDefinition.ts | 2 +- .../schema-registry/src/models.ts | 33 +++++++++- .../src/schemaRegistryClient.ts | 61 +++++++++++++---- .../test/public/schemaRegistry.spec.ts | 10 +-- .../test/public/utils/recordedClient.ts | 4 +- 19 files changed, 446 insertions(+), 101 deletions(-) create mode 100644 sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts delete mode 100644 sdk/schemaregistry/schema-registry/src/generated/generatedSchemaRegistryClientContext.ts diff --git a/sdk/schemaregistry/schema-registry/README.md b/sdk/schemaregistry/schema-registry/README.md index 4344a87f51e5..46e78c5daa3e 100644 --- a/sdk/schemaregistry/schema-registry/README.md +++ b/sdk/schemaregistry/schema-registry/README.md @@ -118,6 +118,19 @@ if (foundSchema) { } ``` +### Get definition of existing schema by version + +```javascript +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 }); +if (foundSchema) { + console.log(`Got schema definition=${foundSchema.definition}`); +} +``` + ## Troubleshooting ### Logging diff --git a/sdk/schemaregistry/schema-registry/package.json b/sdk/schemaregistry/schema-registry/package.json index 4a014e469eef..09b040474a49 100644 --- a/sdk/schemaregistry/schema-registry/package.json +++ b/sdk/schemaregistry/schema-registry/package.json @@ -42,7 +42,7 @@ "//metadata": { "constantPaths": [ { - "path": "src/generated/generatedSchemaRegistryClientContext.ts", + "path": "src/generated/generatedSchemaRegistryClient.ts", "prefix": "packageDetails" }, { diff --git a/sdk/schemaregistry/schema-registry/review/schema-registry.api.md b/sdk/schemaregistry/schema-registry/review/schema-registry.api.md index 00c3f3c6c0b0..17949d12236b 100644 --- a/sdk/schemaregistry/schema-registry/review/schema-registry.api.md +++ b/sdk/schemaregistry/schema-registry/review/schema-registry.api.md @@ -8,6 +8,10 @@ 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 { } @@ -40,11 +44,13 @@ export interface SchemaProperties { groupName: string; id: string; name: string; + version: number; } // @public export interface SchemaRegistry { getSchema(schemaId: string, options?: GetSchemaOptions): Promise; + getSchemaByVersion(schema: SchemaVersion, options: GetSchemaByVersionOptions): Promise; getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise; registerSchema(schema: SchemaDescription, options?: RegisterSchemaOptions): Promise; } @@ -54,6 +60,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; getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise; registerSchema(schema: SchemaDescription, options?: RegisterSchemaOptions): Promise; } @@ -63,6 +70,13 @@ 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 new file mode 100644 index 000000000000..19478200d1a5 --- /dev/null +++ b/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts @@ -0,0 +1,66 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Demonstrates the use of a SchemaRegistryClient to register and retrieve schema. + */ + +import { DefaultAzureCredential } from "@azure/identity"; +import { SchemaRegistryClient, SchemaDescription } from "@azure/schema-registry"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +// Set these environment variables or edit the following values +const fullyQualifiedNamespace = + process.env["SCHEMA_REGISTRY_ENDPOINT"] || ""; +const groupName = process.env["SCHEMA_REGISTRY_GROUP"] || "AzureSdkSampleGroup"; + +// Sample Avro Schema for user with first and last names +const schemaObject = { + type: "record", + name: "User", + namespace: "com.azure.schemaregistry.samples", + fields: [ + { + name: "firstName", + type: "string", + }, + { + name: "lastName", + type: "string", + }, + ], +}; + +const name = `${schemaObject.namespace}-${schemaObject.name}`; + +// Description of the schema for registration +const schemaDescription: SchemaDescription = { + name, + groupName, + format: "Avro", + definition: JSON.stringify(schemaObject), +}; + +export async function main() { + // Create a new client + const client = new SchemaRegistryClient(fullyQualifiedNamespace, new DefaultAzureCredential()); + + // Register a schema and get back its ID. + const { id, version } = await client.registerSchema(schemaDescription); + console.log( + `Registered schema with the following properties:\n- ID=${id}\n- Version: ${version}` + ); + + // Get definition of existing schema by its ID + const foundSchema = await client.getSchema(id); + if (foundSchema) { + console.log(`Got schema definition=${foundSchema.definition}`); + } +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); diff --git a/sdk/schemaregistry/schema-registry/samples-dev/schemaRegistrySample.ts b/sdk/schemaregistry/schema-registry/samples-dev/schemaRegistrySample.ts index 2aa9c4a85223..ef28ecb6a765 100644 --- a/sdk/schemaregistry/schema-registry/samples-dev/schemaRegistrySample.ts +++ b/sdk/schemaregistry/schema-registry/samples-dev/schemaRegistrySample.ts @@ -15,7 +15,7 @@ dotenv.config(); // Set these environment variables or edit the following values const fullyQualifiedNamespace = process.env["SCHEMA_REGISTRY_ENDPOINT"] || ""; -const group = process.env["SCHEMA_REGISTRY_GROUP"] || "AzureSdkSampleGroup"; +const groupName = process.env["SCHEMA_REGISTRY_GROUP"] || "AzureSdkSampleGroup"; // Sample Avro Schema for user with first and last names const schemaObject = { @@ -34,10 +34,12 @@ const schemaObject = { ], }; +const name = `${schemaObject.namespace}-${schemaObject.name}`; + // Description of the schema for registration const schemaDescription: SchemaDescription = { - name: `${schemaObject.namespace}-${schemaObject.name}`, - groupName: group, + name, + groupName, format: "Avro", definition: JSON.stringify(schemaObject), }; @@ -47,18 +49,17 @@ export async function main() { const client = new SchemaRegistryClient(fullyQualifiedNamespace, new DefaultAzureCredential()); // Register a schema and get back its ID. - const registered = await client.registerSchema(schemaDescription); - console.log(`Registered schema with ID=${registered.id}`); - - // Get ID for existing schema by its description. - // Note that this would throw if it had not been previously registered. - const found = await client.getSchemaProperties(schemaDescription); - if (found) { - console.log(`Got schema ID=${found.id}`); - } + const { id, version } = await client.registerSchema(schemaDescription); + console.log( + `Registered schema with the following properties:\n- ID=${id}\n- Version: ${version}` + ); - // Get definition of existing schema by its ID - const foundSchema = await client.getSchema(registered.id); + // Get definition of existing schema by its version + const foundSchema = await client.getSchemaByVersion({ + groupName, + name, + version, + }); if (foundSchema) { console.log(`Got schema definition=${foundSchema.definition}`); } diff --git a/sdk/schemaregistry/schema-registry/src/conversions.ts b/sdk/schemaregistry/schema-registry/src/conversions.ts index 81a607643094..ba6c7db33cff 100644 --- a/sdk/schemaregistry/schema-registry/src/conversions.ts +++ b/sdk/schemaregistry/schema-registry/src/conversions.ts @@ -1,12 +1,12 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { SchemaProperties, Schema } from "./models"; +import { Schema, SchemaProperties } from "./models"; import { SchemaGetByIdResponse, - SchemaRegisterResponse, SchemaQueryIdByContentResponse as SchemaQueryIdByDefinitionResponse, + SchemaRegisterResponse, } from "./generated/models"; import { getSchemaDefinition } from "./getSchemaDefinition"; @@ -34,6 +34,7 @@ export async function convertSchemaResponse(response: GeneratedSchemaResponse): format: mapContentTypeToFormat(response.contentType!), groupName: response.schemaGroupName!, name: response.schemaName!, + version: response.schemaVersion!, }, }; } @@ -54,6 +55,7 @@ export function convertSchemaIdResponse( format: schemaFormat, groupName: response.schemaGroupName!, name: response.schemaName!, + version: response.schemaVersion!, }; }; } diff --git a/sdk/schemaregistry/schema-registry/src/generated/generatedSchemaRegistryClient.ts b/sdk/schemaregistry/schema-registry/src/generated/generatedSchemaRegistryClient.ts index 55b4335eb582..823fba4318c2 100644 --- a/sdk/schemaregistry/schema-registry/src/generated/generatedSchemaRegistryClient.ts +++ b/sdk/schemaregistry/schema-registry/src/generated/generatedSchemaRegistryClient.ts @@ -7,6 +7,12 @@ */ import * as coreClient from "@azure/core-client"; +import * as coreRestPipeline from "@azure/core-rest-pipeline"; +import { + PipelineRequest, + PipelineResponse, + SendRequest +} from "@azure/core-rest-pipeline"; import { SchemaGroupsOperationsImpl, SchemaImpl } from "./operations"; import { SchemaGroupsOperations, Schema } from "./operationsInterfaces"; import { GeneratedSchemaRegistryClientOptionalParams } from "./models"; @@ -52,6 +58,29 @@ export class GeneratedSchemaRegistryClient extends coreClient.ServiceClient { baseUri: options.endpoint ?? options.baseUri ?? "https://{endpoint}" }; super(optionsWithDefaults); + + if (options?.pipeline && options.pipeline.getOrderedPolicies().length > 0) { + const pipelinePolicies: coreRestPipeline.PipelinePolicy[] = options.pipeline.getOrderedPolicies(); + const bearerTokenAuthenticationPolicyFound = pipelinePolicies.some( + (pipelinePolicy) => + pipelinePolicy.name === + coreRestPipeline.bearerTokenAuthenticationPolicyName + ); + if (!bearerTokenAuthenticationPolicyFound) { + this.pipeline.removePolicy({ + name: coreRestPipeline.bearerTokenAuthenticationPolicyName + }); + this.pipeline.addPolicy( + coreRestPipeline.bearerTokenAuthenticationPolicy({ + scopes: `${optionsWithDefaults.baseUri}/.default`, + challengeCallbacks: { + authorizeRequestOnChallenge: + coreClient.authorizeRequestOnClaimChallenge + } + }) + ); + } + } // Parameter assignments this.endpoint = endpoint; @@ -59,6 +88,35 @@ export class GeneratedSchemaRegistryClient extends coreClient.ServiceClient { this.apiVersion = options.apiVersion || "2021-10"; this.schemaGroupsOperations = new SchemaGroupsOperationsImpl(this); this.schema = new SchemaImpl(this); + this.addCustomApiVersionPolicy(options.apiVersion); + } + + /** A function that adds a policy that sets the api-version (or equivalent) to reflect the library version. */ + private addCustomApiVersionPolicy(apiVersion?: string) { + if (!apiVersion) { + return; + } + const apiVersionPolicy = { + name: "CustomApiVersionPolicy", + async sendRequest( + request: PipelineRequest, + next: SendRequest + ): Promise { + const param = request.url.split("?"); + if (param.length > 1) { + const newParams = param[1].split("&").map((item) => { + if (item.indexOf("api-version") > -1) { + return "api-version=" + apiVersion; + } else { + return item; + } + }); + request.url = param[0] + "?" + newParams.join("&"); + } + return next(request); + } + }; + this.pipeline.addPolicy(apiVersionPolicy); } schemaGroupsOperations: SchemaGroupsOperations; diff --git a/sdk/schemaregistry/schema-registry/src/generated/generatedSchemaRegistryClientContext.ts b/sdk/schemaregistry/schema-registry/src/generated/generatedSchemaRegistryClientContext.ts deleted file mode 100644 index 20b82fef3038..000000000000 --- a/sdk/schemaregistry/schema-registry/src/generated/generatedSchemaRegistryClientContext.ts +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. - * Licensed under the MIT License. - * - * Code generated by Microsoft (R) AutoRest Code Generator. - * Changes may cause incorrect behavior and will be lost if the code is regenerated. - */ - -import * as coreClient from "@azure/core-client"; -import { GeneratedSchemaRegistryClientOptionalParams } from "./models"; - -export class GeneratedSchemaRegistryClientContext extends coreClient.ServiceClient { - endpoint: string; - apiVersion: string; - - /** - * Initializes a new instance of the GeneratedSchemaRegistryClientContext class. - * @param endpoint The Schema Registry service endpoint, for example - * my-namespace.servicebus.windows.net. - * @param options The parameter options - */ - constructor( - endpoint: string, - options?: GeneratedSchemaRegistryClientOptionalParams - ) { - if (endpoint === undefined) { - throw new Error("'endpoint' cannot be null"); - } - - // Initializing default values for options - if (!options) { - options = {}; - } - const defaults: GeneratedSchemaRegistryClientOptionalParams = { - requestContentType: "application/json; charset=utf-8" - }; - - const packageDetails = `azsdk-js-schema-registry/1.0.2`; - const userAgentPrefix = - options.userAgentOptions && options.userAgentOptions.userAgentPrefix - ? `${options.userAgentOptions.userAgentPrefix} ${packageDetails}` - : `${packageDetails}`; - - const optionsWithDefaults = { - ...defaults, - ...options, - userAgentOptions: { - userAgentPrefix - }, - baseUri: options.endpoint || "https://{endpoint}" - }; - super(optionsWithDefaults); - // Parameter assignments - this.endpoint = endpoint; - - // Assigning values to Constant parameters - this.apiVersion = options.apiVersion || "2021-10"; - } -} diff --git a/sdk/schemaregistry/schema-registry/src/generated/models/index.ts b/sdk/schemaregistry/schema-registry/src/generated/models/index.ts index ac15a4622395..8dfa59e2b422 100644 --- a/sdk/schemaregistry/schema-registry/src/generated/models/index.ts +++ b/sdk/schemaregistry/schema-registry/src/generated/models/index.ts @@ -78,6 +78,30 @@ export interface SchemaGetVersionsExceptionHeaders { xMsErrorCode?: string; } +/** Defines headers for Schema_getSchemaVersion operation. */ +export interface SchemaGetSchemaVersionHeaders { + /** URL location of schema, identified by schema group, schema name, and version. */ + location?: string; + /** The content type for given schema. Each schema type has an associated content-type. */ + contentType?: string; + /** References specific schema in registry namespace. */ + schemaId?: string; + /** URL location of schema, identified by schema ID. */ + schemaIdLocation?: string; + /** References schema group. */ + schemaGroupName?: string; + /** References schema name. */ + schemaName?: string; + /** Version of the returned schema. */ + schemaVersion?: number; +} + +/** Defines headers for Schema_getSchemaVersion operation. */ +export interface SchemaGetSchemaVersionExceptionHeaders { + /** Error code for specific error that occurred. */ + xMsErrorCode?: string; +} + /** Defines headers for Schema_queryIdByContent operation. */ export interface SchemaQueryIdByContentHeaders { /** URL location of schema, identified by schema group, schema name, and version. */ @@ -158,6 +182,28 @@ export interface SchemaGetVersionsOptionalParams /** Contains response data for the getVersions operation. */ export type SchemaGetVersionsResponse = SchemaVersions; +/** Optional parameters. */ +export interface SchemaGetSchemaVersionOptionalParams + extends coreClient.OperationOptions {} + +/** Contains response data for the getSchemaVersion operation. */ +export type SchemaGetSchemaVersionResponse = SchemaGetSchemaVersionHeaders & { + /** + * BROWSER ONLY + * + * The response body as a browser Blob. + * Always `undefined` in node.js. + */ + blobBody?: Promise; + /** + * NODEJS ONLY + * + * The response body as a node.js Readable stream. + * Always `undefined` in the browser. + */ + readableStreamBody?: NodeJS.ReadableStream; +}; + /** Optional parameters. */ export interface SchemaQueryIdByContentOptionalParams extends coreClient.OperationOptions {} diff --git a/sdk/schemaregistry/schema-registry/src/generated/models/mappers.ts b/sdk/schemaregistry/schema-registry/src/generated/models/mappers.ts index 82266ac3c078..2acc0f7b033c 100644 --- a/sdk/schemaregistry/schema-registry/src/generated/models/mappers.ts +++ b/sdk/schemaregistry/schema-registry/src/generated/models/mappers.ts @@ -210,6 +210,72 @@ export const SchemaGetVersionsExceptionHeaders: coreClient.CompositeMapper = { } }; +export const SchemaGetSchemaVersionHeaders: coreClient.CompositeMapper = { + type: { + name: "Composite", + className: "SchemaGetSchemaVersionHeaders", + modelProperties: { + location: { + serializedName: "location", + type: { + name: "String" + } + }, + contentType: { + serializedName: "content-type", + type: { + name: "String" + } + }, + schemaId: { + serializedName: "schema-id", + type: { + name: "String" + } + }, + schemaIdLocation: { + serializedName: "schema-id-location", + type: { + name: "String" + } + }, + schemaGroupName: { + serializedName: "schema-group-name", + type: { + name: "String" + } + }, + schemaName: { + serializedName: "schema-name", + type: { + name: "String" + } + }, + schemaVersion: { + serializedName: "schema-version", + type: { + name: "Number" + } + } + } + } +}; + +export const SchemaGetSchemaVersionExceptionHeaders: coreClient.CompositeMapper = { + type: { + name: "Composite", + className: "SchemaGetSchemaVersionExceptionHeaders", + modelProperties: { + xMsErrorCode: { + serializedName: "x-ms-error-code", + type: { + name: "String" + } + } + } + } +}; + export const SchemaQueryIdByContentHeaders: coreClient.CompositeMapper = { type: { name: "Composite", diff --git a/sdk/schemaregistry/schema-registry/src/generated/models/parameters.ts b/sdk/schemaregistry/schema-registry/src/generated/models/parameters.ts index fe1572475cf8..b4133de93072 100644 --- a/sdk/schemaregistry/schema-registry/src/generated/models/parameters.ts +++ b/sdk/schemaregistry/schema-registry/src/generated/models/parameters.ts @@ -96,6 +96,17 @@ export const schemaName: OperationURLParameter = { } }; +export const schemaVersion: OperationURLParameter = { + parameterPath: "schemaVersion", + mapper: { + serializedName: "schemaVersion", + required: true, + type: { + name: "Number" + } + } +}; + export const schemaContent: OperationParameter = { parameterPath: "schemaContent", mapper: { diff --git a/sdk/schemaregistry/schema-registry/src/generated/operations/schema.ts b/sdk/schemaregistry/schema-registry/src/generated/operations/schema.ts index 8f3ce0c867a0..7720ba84df8f 100644 --- a/sdk/schemaregistry/schema-registry/src/generated/operations/schema.ts +++ b/sdk/schemaregistry/schema-registry/src/generated/operations/schema.ts @@ -17,6 +17,8 @@ import { SchemaGetByIdResponse, SchemaGetVersionsOptionalParams, SchemaGetVersionsResponse, + SchemaGetSchemaVersionOptionalParams, + SchemaGetSchemaVersionResponse, SchemaQueryIdByContentOptionalParams, SchemaQueryIdByContentResponse, SchemaRegisterOptionalParams, @@ -69,6 +71,26 @@ export class SchemaImpl implements Schema { ); } + /** + * Gets one specific version of one schema. + * @param groupName Schema group under which schema is registered. Group's serialization type should + * match the serialization type specified in the request. + * @param schemaName Name of schema. + * @param schemaVersion Version number of specific schema. + * @param options The options parameters. + */ + getSchemaVersion( + groupName: string, + schemaName: string, + schemaVersion: number, + options?: SchemaGetSchemaVersionOptionalParams + ): Promise { + return this.client.sendOperationRequest( + { groupName, schemaName, schemaVersion, options }, + getSchemaVersionOperationSpec + ); + } + /** * Gets the ID referencing an existing schema within the specified schema group, as matched by schema * content comparison. @@ -162,6 +184,33 @@ const getVersionsOperationSpec: coreClient.OperationSpec = { headerParameters: [Parameters.accept], serializer }; +const getSchemaVersionOperationSpec: coreClient.OperationSpec = { + path: + "/$schemaGroups/{groupName}/schemas/{schemaName}/versions/{schemaVersion}", + httpMethod: "GET", + responses: { + 200: { + bodyMapper: { + type: { name: "Stream" }, + serializedName: "parsedResponse" + }, + headersMapper: Mappers.SchemaGetSchemaVersionHeaders + }, + default: { + bodyMapper: Mappers.ErrorModel, + headersMapper: Mappers.SchemaGetSchemaVersionExceptionHeaders + } + }, + queryParameters: [Parameters.apiVersion], + urlParameters: [ + Parameters.endpoint, + Parameters.groupName, + Parameters.schemaName, + Parameters.schemaVersion + ], + headerParameters: [Parameters.accept], + serializer +}; const queryIdByContentOperationSpec: coreClient.OperationSpec = { path: "/$schemaGroups/{groupName}/schemas/{schemaName}:get-id", httpMethod: "POST", diff --git a/sdk/schemaregistry/schema-registry/src/generated/operationsInterfaces/schema.ts b/sdk/schemaregistry/schema-registry/src/generated/operationsInterfaces/schema.ts index fbec6932ebbd..48bae5745ce6 100644 --- a/sdk/schemaregistry/schema-registry/src/generated/operationsInterfaces/schema.ts +++ b/sdk/schemaregistry/schema-registry/src/generated/operationsInterfaces/schema.ts @@ -12,6 +12,8 @@ import { SchemaGetByIdResponse, SchemaGetVersionsOptionalParams, SchemaGetVersionsResponse, + SchemaGetSchemaVersionOptionalParams, + SchemaGetSchemaVersionResponse, SchemaQueryIdByContentOptionalParams, SchemaQueryIdByContentResponse, SchemaRegisterOptionalParams, @@ -42,6 +44,20 @@ export interface Schema { schemaName: string, options?: SchemaGetVersionsOptionalParams ): Promise; + /** + * Gets one specific version of one schema. + * @param groupName Schema group under which schema is registered. Group's serialization type should + * match the serialization type specified in the request. + * @param schemaName Name of schema. + * @param schemaVersion Version number of specific schema. + * @param options The options parameters. + */ + getSchemaVersion( + groupName: string, + schemaName: string, + schemaVersion: number, + options?: SchemaGetSchemaVersionOptionalParams + ): Promise; /** * Gets the ID referencing an existing schema within the specified schema group, as matched by schema * content comparison. diff --git a/sdk/schemaregistry/schema-registry/src/getSchemaDefinition.browser.ts b/sdk/schemaregistry/schema-registry/src/getSchemaDefinition.browser.ts index 7005d722c73d..6483382b0a39 100644 --- a/sdk/schemaregistry/schema-registry/src/getSchemaDefinition.browser.ts +++ b/sdk/schemaregistry/schema-registry/src/getSchemaDefinition.browser.ts @@ -4,7 +4,7 @@ /** * Gets the schema definition from the response * @param response - The service response for a get schema by ID request. - * @returns a string representing an Avro schema definition + * @returns a string representing a schema definition */ export async function getSchemaDefinition< T extends { diff --git a/sdk/schemaregistry/schema-registry/src/getSchemaDefinition.ts b/sdk/schemaregistry/schema-registry/src/getSchemaDefinition.ts index 605ad89d1b12..d3079b20ad0a 100644 --- a/sdk/schemaregistry/schema-registry/src/getSchemaDefinition.ts +++ b/sdk/schemaregistry/schema-registry/src/getSchemaDefinition.ts @@ -4,7 +4,7 @@ /** * Gets the schema definition from the response * @param response - The service response for a get schema by ID request. - * @returns a string representing an Avro schema definition + * @returns a string representing a schema definition */ export async function getSchemaDefinition< T extends { diff --git a/sdk/schemaregistry/schema-registry/src/models.ts b/sdk/schemaregistry/schema-registry/src/models.ts index 5a6af0d28e42..d657442e17f6 100644 --- a/sdk/schemaregistry/schema-registry/src/models.ts +++ b/sdk/schemaregistry/schema-registry/src/models.ts @@ -12,7 +12,6 @@ export interface SchemaProperties { /** * Serialization type of schema. - * Currently only 'avro' is supported, but this is subject to change. */ format: string; @@ -21,6 +20,25 @@ export interface SchemaProperties { /** Name of schema.*/ name: string; + + /** The version of schema */ + 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; } /** @@ -35,7 +53,6 @@ export interface SchemaDescription { /** * The format of schema and it must match the serialization type of the schema's group. - * "Avro" is the only currently accepted value at the time of this package's release. */ format: string; @@ -78,6 +95,11 @@ 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. * @@ -119,4 +141,11 @@ export interface SchemaRegistry { * @returns Schema with given ID. */ getSchema(schemaId: string, options?: GetSchemaOptions): Promise; + + /** + * Gets an existing schema by version. + * @param schema - Schema version + * @returns Schema with given version + */ + getSchemaByVersion(schema: SchemaVersion, options: GetSchemaByVersionOptions): Promise; } diff --git a/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts b/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts index 89be0d40cf1f..d2b3ea39b1a9 100644 --- a/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts +++ b/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts @@ -1,26 +1,27 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { GeneratedSchemaRegistryClient } from "./generated/generatedSchemaRegistryClient"; -import { TokenCredential } from "@azure/core-auth"; -import { createTracingClient, TracingClient } from "@azure/core-tracing"; -import { - bearerTokenAuthenticationPolicy, - InternalPipelineOptions, -} from "@azure/core-rest-pipeline"; -import { convertSchemaIdResponse, convertSchemaResponse } from "./conversions"; - +import { DEFAULT_SCOPE, SDK_VERSION } from "./constants"; import { + GetSchemaByVersionOptions, GetSchemaOptions, GetSchemaPropertiesOptions, - SchemaDescription, - SchemaRegistryClientOptions, - SchemaRegistry, RegisterSchemaOptions, - SchemaProperties, Schema, + SchemaDescription, + SchemaProperties, + SchemaRegistry, + SchemaRegistryClientOptions, + SchemaVersion, } from "./models"; -import { DEFAULT_SCOPE, SDK_VERSION } from "./constants"; +import { + InternalPipelineOptions, + bearerTokenAuthenticationPolicy, +} from "@azure/core-rest-pipeline"; +import { TracingClient, createTracingClient } from "@azure/core-tracing"; +import { convertSchemaIdResponse, convertSchemaResponse } from "./conversions"; +import { GeneratedSchemaRegistryClient } from "./generated/generatedSchemaRegistryClient"; +import { TokenCredential } from "@azure/core-auth"; import { logger } from "./logger"; /** @@ -157,4 +158,36 @@ export class SchemaRegistryClient implements SchemaRegistry { this._client.schema.getById(schemaId, updatedOptions).then(convertSchemaResponse) ); } + + /** + * Gets an existing schema by version. If the schema was not found, a RestError with + * status code 404 will be thrown, which could be caught as follows: + * + * ```js + * ... + * } catch (e) { + if (typeof e === "object" && e.statusCode === 404) { + ...; + } + throw e; + } + * ``` + * + * @param schemaVersion - schema version. + * @returns Schema with given ID. + */ + getSchemaByVersion( + schemaVersion: SchemaVersion, + options: GetSchemaByVersionOptions = {} + ): Promise { + const { groupName, name, version } = schemaVersion; + return this._tracing.withSpan( + "SchemaRegistryClient.getSchemaByVersion", + options, + (updatedOptions) => + this._client.schema + .getSchemaVersion(groupName, name, version, updatedOptions) + .then(convertSchemaResponse) + ); + } } diff --git a/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts b/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts index 00ce0c62b206..5812d3621efc 100644 --- a/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts +++ b/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts @@ -1,16 +1,16 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { createRecordedClient, recorderOptions } from "./utils/recordedClient"; import { Recorder, assertEnvironmentVariable } from "@azure-tools/test-recorder"; +import { Schema, SchemaDescription, SchemaProperties, SchemaRegistryClient } from "../../src"; import { assert, use as chaiUse } from "chai"; -import chaiPromises from "chai-as-promised"; -chaiUse(chaiPromises); +import { createRecordedClient, recorderOptions } from "./utils/recordedClient"; import { ClientSecretCredential } from "@azure/identity"; +import { Context } from "mocha"; import { HttpHeaders } from "@azure/core-rest-pipeline"; +import chaiPromises from "chai-as-promised"; -import { Schema, SchemaDescription, SchemaProperties, SchemaRegistryClient } from "../../src"; -import { Context } from "mocha"; +chaiUse(chaiPromises); const options = { onResponse: (rawResponse: { status: number }) => { diff --git a/sdk/schemaregistry/schema-registry/test/public/utils/recordedClient.ts b/sdk/schemaregistry/schema-registry/test/public/utils/recordedClient.ts index 312b6ed53b1b..e8a6f6bda648 100644 --- a/sdk/schemaregistry/schema-registry/test/public/utils/recordedClient.ts +++ b/sdk/schemaregistry/schema-registry/test/public/utils/recordedClient.ts @@ -1,13 +1,13 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { createTestCredential } from "@azure-tools/test-credential"; -import { SchemaRegistryClient } from "../../../src"; import { Recorder, RecorderStartOptions, assertEnvironmentVariable, } from "@azure-tools/test-recorder"; +import { SchemaRegistryClient } from "../../../src"; +import { createTestCredential } from "@azure-tools/test-credential"; export const recorderOptions: RecorderStartOptions = { envSetupForPlayback: { From f608cfc23a28b0d2943af808ad58b4e080b71613 Mon Sep 17 00:00:00 2001 From: Deyaaeldeen Almahallawi Date: Thu, 8 Sep 2022 18:31:13 -0700 Subject: [PATCH 2/4] added changelog entry --- sdk/schemaregistry/schema-registry/CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sdk/schemaregistry/schema-registry/CHANGELOG.md b/sdk/schemaregistry/schema-registry/CHANGELOG.md index e96e8f6babc8..7191231fdb64 100644 --- a/sdk/schemaregistry/schema-registry/CHANGELOG.md +++ b/sdk/schemaregistry/schema-registry/CHANGELOG.md @@ -1,5 +1,12 @@ # Release History +## 1.1.0 (Unreleased) + +### Features Added + +- `getSchemaByVersion` is added to query schemas by their version. +- `version` property is added to `SchemaProperties`. + ## 1.1.0 (2022-05-10) ### Features Added From 5297f8413cbbea0dfec861bd149bca01aee6c3d8 Mon Sep 17 00:00:00 2001 From: Deyaaeldeen Almahallawi Date: Thu, 8 Sep 2022 20:49:49 -0700 Subject: [PATCH 3/4] address feedback --- .../schema-registry/review/schema-registry.api.md | 1 - .../schema-registry/samples-dev/getSchemaByVersion.ts | 8 ++++++-- .../schema-registry/samples-dev/schemaRegistrySample.ts | 8 ++------ sdk/schemaregistry/schema-registry/src/models.ts | 7 ------- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/sdk/schemaregistry/schema-registry/review/schema-registry.api.md b/sdk/schemaregistry/schema-registry/review/schema-registry.api.md index 17949d12236b..61ea60ed7d3a 100644 --- a/sdk/schemaregistry/schema-registry/review/schema-registry.api.md +++ b/sdk/schemaregistry/schema-registry/review/schema-registry.api.md @@ -50,7 +50,6 @@ export interface SchemaProperties { // @public export interface SchemaRegistry { getSchema(schemaId: string, options?: GetSchemaOptions): Promise; - getSchemaByVersion(schema: SchemaVersion, options: GetSchemaByVersionOptions): Promise; getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise; registerSchema(schema: SchemaDescription, options?: RegisterSchemaOptions): Promise; } diff --git a/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts b/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts index 19478200d1a5..ef28ecb6a765 100644 --- a/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts +++ b/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts @@ -54,8 +54,12 @@ export async function main() { `Registered schema with the following properties:\n- ID=${id}\n- Version: ${version}` ); - // Get definition of existing schema by its ID - const foundSchema = await client.getSchema(id); + // Get definition of existing schema by its version + const foundSchema = await client.getSchemaByVersion({ + groupName, + name, + version, + }); if (foundSchema) { console.log(`Got schema definition=${foundSchema.definition}`); } diff --git a/sdk/schemaregistry/schema-registry/samples-dev/schemaRegistrySample.ts b/sdk/schemaregistry/schema-registry/samples-dev/schemaRegistrySample.ts index ef28ecb6a765..19478200d1a5 100644 --- a/sdk/schemaregistry/schema-registry/samples-dev/schemaRegistrySample.ts +++ b/sdk/schemaregistry/schema-registry/samples-dev/schemaRegistrySample.ts @@ -54,12 +54,8 @@ export async function main() { `Registered schema with the following properties:\n- ID=${id}\n- Version: ${version}` ); - // Get definition of existing schema by its version - const foundSchema = await client.getSchemaByVersion({ - groupName, - name, - version, - }); + // Get definition of existing schema by its ID + const foundSchema = await client.getSchema(id); if (foundSchema) { console.log(`Got schema definition=${foundSchema.definition}`); } diff --git a/sdk/schemaregistry/schema-registry/src/models.ts b/sdk/schemaregistry/schema-registry/src/models.ts index d657442e17f6..2ca84fff31df 100644 --- a/sdk/schemaregistry/schema-registry/src/models.ts +++ b/sdk/schemaregistry/schema-registry/src/models.ts @@ -141,11 +141,4 @@ export interface SchemaRegistry { * @returns Schema with given ID. */ getSchema(schemaId: string, options?: GetSchemaOptions): Promise; - - /** - * Gets an existing schema by version. - * @param schema - Schema version - * @returns Schema with given version - */ - getSchemaByVersion(schema: SchemaVersion, options: GetSchemaByVersionOptions): Promise; } From 7a57e0312e42e34fdc916bf7412b2374c764c088 Mon Sep 17 00:00:00 2001 From: Deyaaeldeen Almahallawi Date: Fri, 9 Sep 2022 16:27:39 -0700 Subject: [PATCH 4/4] add a test --- .../recording_gets_schema_by_version.json | 110 ++++++++++++++++++ .../recording_gets_schema_by_version.json | 92 +++++++++++++++ .../samples-dev/getSchemaByVersion.ts | 4 +- .../test/public/schemaRegistry.spec.ts | 20 ++++ 4 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 sdk/schemaregistry/schema-registry/recordings/browsers/schemaregistryclient/recording_gets_schema_by_version.json create mode 100644 sdk/schemaregistry/schema-registry/recordings/node/schemaregistryclient/recording_gets_schema_by_version.json diff --git a/sdk/schemaregistry/schema-registry/recordings/browsers/schemaregistryclient/recording_gets_schema_by_version.json b/sdk/schemaregistry/schema-registry/recordings/browsers/schemaregistryclient/recording_gets_schema_by_version.json new file mode 100644 index 000000000000..3c677ecab0a2 --- /dev/null +++ b/sdk/schemaregistry/schema-registry/recordings/browsers/schemaregistryclient/recording_gets_schema_by_version.json @@ -0,0 +1,110 @@ +{ + "Entries": [ + { + "RequestUri": "https://endpoint/$schemaGroups/group-1/schemas/azsdk_js_test?api-version=2021-10", + "RequestMethod": "PUT", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip, deflate, br", + "Accept-Language": "en-US", + "Authorization": "Sanitized", + "Connection": "keep-alive", + "Content-Length": "160", + "Content-Type": "application/json; serialization=Avro", + "Referer": "http://localhost:9876/", + "sec-ch-ua": "", + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": "", + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Site": "same-site", + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/102.0.5002.0 Safari/537.36", + "x-ms-client-request-id": "b5fbea52-5fe0-48d7-a79e-6cb8f51549f4", + "x-ms-useragent": "azsdk-js-schema-registry/1.1.0 core-rest-pipeline/1.9.0 OS/Linuxx86_64" + }, + "RequestBody": { + "type": "record", + "name": "User", + "namespace": "com.azure.schemaregistry.samples", + "fields": [ + { + "name": "name", + "type": "string" + }, + { + "name": "favoriteNumber", + "type": "int" + } + ] + }, + "StatusCode": 204, + "ResponseHeaders": { + "Content-Length": "0", + "Date": "Fri, 09 Sep 2022 22:57:56 GMT", + "Location": "https://endpoint/$schemagroups/group-1/schemas/azsdk_js_test/versions/1?api-version=2021-10", + "Schema-Group-Name": "group-1", + "Schema-Id": "bbf801958fa1455cbb5b6b87cdfd9118", + "Schema-Id-Location": "https://endpoint:443/$schemagroups/$schemas/bbf801958fa1455cbb5b6b87cdfd9118?api-version=2021-10", + "Schema-Name": "azsdk_js_test", + "Schema-Version": "1", + "Schema-Versions-Location": "https://endpoint:443/$schemagroups/group-1/schemas/azsdk_js_test/versions?api-version=2021-10", + "Server": "Microsoft-HTTPAPI/2.0", + "Strict-Transport-Security": "max-age=31536000" + }, + "ResponseBody": null + }, + { + "RequestUri": "https://endpoint/$schemaGroups/group-1/schemas/azsdk_js_test/versions/1?api-version=2021-10", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip, deflate, br", + "Accept-Language": "en-US", + "Authorization": "Sanitized", + "Connection": "keep-alive", + "Referer": "http://localhost:9876/", + "sec-ch-ua": "", + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": "", + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Site": "same-site", + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/102.0.5002.0 Safari/537.36", + "x-ms-client-request-id": "0866ff60-0d04-443f-8518-4f58605d9324", + "x-ms-useragent": "azsdk-js-schema-registry/1.1.0 core-rest-pipeline/1.9.0 OS/Linuxx86_64" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "Content-Type": "application/json; serialization=Avro", + "Date": "Fri, 09 Sep 2022 22:57:56 GMT", + "Location": "https://endpoint/$schemagroups/group-1/schemas/azsdk_js_test/versions/1?api-version=2021-10", + "Schema-Group-Name": "group-1", + "Schema-Id": "bbf801958fa1455cbb5b6b87cdfd9118", + "Schema-Id-Location": "https://endpoint:443/$schemagroups/$schemas/bbf801958fa1455cbb5b6b87cdfd9118?api-version=2021-10", + "Schema-Name": "azsdk_js_test", + "Schema-Version": "1", + "Schema-Versions-Location": "https://endpoint:443/$schemagroups/group-1/schemas/azsdk_js_test/versions?api-version=2021-10", + "Server": "Microsoft-HTTPAPI/2.0", + "Strict-Transport-Security": "max-age=31536000", + "Transfer-Encoding": "chunked" + }, + "ResponseBody": { + "type": "record", + "name": "User", + "namespace": "com.azure.schemaregistry.samples", + "fields": [ + { + "name": "name", + "type": "string" + }, + { + "name": "favoriteNumber", + "type": "int" + } + ] + } + } + ], + "Variables": {} +} diff --git a/sdk/schemaregistry/schema-registry/recordings/node/schemaregistryclient/recording_gets_schema_by_version.json b/sdk/schemaregistry/schema-registry/recordings/node/schemaregistryclient/recording_gets_schema_by_version.json new file mode 100644 index 000000000000..08d7e66a356c --- /dev/null +++ b/sdk/schemaregistry/schema-registry/recordings/node/schemaregistryclient/recording_gets_schema_by_version.json @@ -0,0 +1,92 @@ +{ + "Entries": [ + { + "RequestUri": "https://endpoint/$schemaGroups/group-1/schemas/azsdk_js_test?api-version=2021-10", + "RequestMethod": "PUT", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip,deflate", + "Authorization": "Sanitized", + "Connection": "keep-alive", + "Content-Length": "160", + "Content-Type": "application/json; serialization=Avro", + "User-Agent": "azsdk-js-schema-registry/1.1.0 core-rest-pipeline/1.9.0 Node/v18.6.0 OS/(x64-Linux-5.10.102.1-microsoft-standard-WSL2)", + "x-ms-client-request-id": "3944f826-92de-4e18-b0cc-8c2c392726ec" + }, + "RequestBody": { + "type": "record", + "name": "User", + "namespace": "com.azure.schemaregistry.samples", + "fields": [ + { + "name": "name", + "type": "string" + }, + { + "name": "favoriteNumber", + "type": "int" + } + ] + }, + "StatusCode": 204, + "ResponseHeaders": { + "Content-Length": "0", + "Date": "Fri, 09 Sep 2022 22:57:39 GMT", + "Location": "https://endpoint/$schemagroups/group-1/schemas/azsdk_js_test/versions/1?api-version=2021-10", + "Schema-Group-Name": "group-1", + "Schema-Id": "bbf801958fa1455cbb5b6b87cdfd9118", + "Schema-Id-Location": "https://endpoint:443/$schemagroups/$schemas/bbf801958fa1455cbb5b6b87cdfd9118?api-version=2021-10", + "Schema-Name": "azsdk_js_test", + "Schema-Version": "1", + "Schema-Versions-Location": "https://endpoint:443/$schemagroups/group-1/schemas/azsdk_js_test/versions?api-version=2021-10", + "Server": "Microsoft-HTTPAPI/2.0", + "Strict-Transport-Security": "max-age=31536000" + }, + "ResponseBody": null + }, + { + "RequestUri": "https://endpoint/$schemaGroups/group-1/schemas/azsdk_js_test/versions/1?api-version=2021-10", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip,deflate", + "Authorization": "Sanitized", + "Connection": "keep-alive", + "User-Agent": "azsdk-js-schema-registry/1.1.0 core-rest-pipeline/1.9.0 Node/v18.6.0 OS/(x64-Linux-5.10.102.1-microsoft-standard-WSL2)", + "x-ms-client-request-id": "6d9783e7-be7b-426e-b93a-38494320b0ba" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "Content-Type": "application/json; serialization=Avro", + "Date": "Fri, 09 Sep 2022 22:57:40 GMT", + "Location": "https://endpoint/$schemagroups/group-1/schemas/azsdk_js_test/versions/1?api-version=2021-10", + "Schema-Group-Name": "group-1", + "Schema-Id": "bbf801958fa1455cbb5b6b87cdfd9118", + "Schema-Id-Location": "https://endpoint:443/$schemagroups/$schemas/bbf801958fa1455cbb5b6b87cdfd9118?api-version=2021-10", + "Schema-Name": "azsdk_js_test", + "Schema-Version": "1", + "Schema-Versions-Location": "https://endpoint:443/$schemagroups/group-1/schemas/azsdk_js_test/versions?api-version=2021-10", + "Server": "Microsoft-HTTPAPI/2.0", + "Strict-Transport-Security": "max-age=31536000", + "Transfer-Encoding": "chunked" + }, + "ResponseBody": { + "type": "record", + "name": "User", + "namespace": "com.azure.schemaregistry.samples", + "fields": [ + { + "name": "name", + "type": "string" + }, + { + "name": "favoriteNumber", + "type": "int" + } + ] + } + } + ], + "Variables": {} +} diff --git a/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts b/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts index ef28ecb6a765..f85c8e189686 100644 --- a/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts +++ b/sdk/schemaregistry/schema-registry/samples-dev/getSchemaByVersion.ts @@ -2,7 +2,7 @@ // Licensed under the MIT License. /** - * @summary Demonstrates the use of a SchemaRegistryClient to register and retrieve schema. + * @summary Demonstrates the use of getSchemaByVersion. */ import { DefaultAzureCredential } from "@azure/identity"; @@ -48,7 +48,7 @@ export async function main() { // Create a new client const client = new SchemaRegistryClient(fullyQualifiedNamespace, new DefaultAzureCredential()); - // Register a schema and get back its ID. + // Register a schema and get back its ID and version. const { id, version } = await client.registerSchema(schemaDescription); console.log( `Registered schema with the following properties:\n- ID=${id}\n- Version: ${version}` diff --git a/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts b/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts index 5812d3621efc..ffb90b0de774 100644 --- a/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts +++ b/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts @@ -37,6 +37,7 @@ function assertIsValidSchemaProperties( assert.equal(schemaProperties.format, expectedSerializationType); assert.isNotEmpty(schemaProperties.groupName); assert.isNotEmpty(schemaProperties.name); + assert.isAtLeast(schemaProperties.version, 1); } function assertIsValidSchema(schema: Schema, expectedSerializationType = "Avro"): asserts schema { @@ -211,6 +212,25 @@ describe("SchemaRegistryClient", function () { assert.equal(found.definition, schema.definition); }); + it("gets schema by version", async () => { + const registered = await client.registerSchema(schema, options); + assertIsValidSchemaProperties(registered); + const found = await client.getSchemaByVersion( + { + groupName: registered.groupName, + name: registered.name, + version: registered.version, + }, + { + onResponse: (rawResponse: { status: number }) => { + assert.equal(rawResponse.status, 200); + }, + } + ); + assertIsValidSchema(found); + assert.equal(found.definition, schema.definition); + }); + it("schema with whitespace", async () => { const schema2: SchemaDescription = { name: "azsdk_js_test2",