Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { isLive } from "./isLive";
import { testSchemaIds } from "./dummies";
import { v4 as uuid } from "uuid";

type UpdatedSchemaDescription = Required<Omit<SchemaDescription, "version">>;

function getEnvVar(name: string): string {
const value = env[name];
if (!value) {
Expand Down Expand Up @@ -59,7 +61,7 @@ function createMockedTestRegistry(): SchemaRegistry {
return { registerSchema, getSchemaProperties, getSchema };

async function registerSchema(
schema: SchemaDescription,
schema: UpdatedSchemaDescription,
_options?: RegisterSchemaOptions
): Promise<SchemaProperties> {
let result = mapByContent.get(schema.definition);
Expand Down Expand Up @@ -93,7 +95,7 @@ function createMockedTestRegistry(): SchemaRegistry {
}

async function getSchemaProperties(
schema: SchemaDescription,
schema: UpdatedSchemaDescription,
_options?: GetSchemaPropertiesOptions
): Promise<SchemaProperties> {
const storedSchema = mapByContent.get(schema.definition);
Expand Down
2 changes: 1 addition & 1 deletion sdk/schemaregistry/schema-registry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sdk/schemaregistry/schema-registry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const { DefaultAzureCredential } = require("@azure/identity");
const { SchemaRegistryClient } = require("@azure/schema-registry");

const client = new SchemaRegistryClient("<fullyQualifiedNamespace>", new DefaultAzureCredential());
const foundSchema = await client.getSchemaByVersion({ name:"<schema name>", groupName: "group name", version });
const foundSchema = await client.getSchema({ name:"<schema name>", groupName: "group name", version });
if (foundSchema) {
console.log(`Got schema definition=${foundSchema.definition}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Expand All @@ -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
Expand All @@ -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<Schema>;
getSchemaByVersion(schemaVersion: SchemaVersion, options?: GetSchemaByVersionOptions): Promise<Schema>;
getSchema(schemaDescription: SchemaDescription, options?: GetSchemaOptions): Promise<Schema>;
getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise<SchemaProperties>;
registerSchema(schema: SchemaDescription, options?: RegisterSchemaOptions): Promise<SchemaProperties>;
}
Expand All @@ -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)

```
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 5 additions & 23 deletions sdk/schemaregistry/schema-registry/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
40 changes: 26 additions & 14 deletions sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import { DEFAULT_SCOPE, SDK_VERSION } from "./constants";
import {
GetSchemaByVersionOptions,
GetSchemaOptions,
GetSchemaPropertiesOptions,
RegisterSchemaOptions,
Expand All @@ -12,7 +11,6 @@ import {
SchemaProperties,
SchemaRegistry,
SchemaRegistryClientOptions,
SchemaVersion,
} from "./models";
import {
InternalPipelineOptions,
Expand All @@ -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";

/**
Expand Down Expand Up @@ -91,7 +90,12 @@ export class SchemaRegistryClient implements SchemaRegistry {
schema: SchemaDescription,
options: RegisterSchemaOptions = {}
): Promise<SchemaProperties> {
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,
Expand Down Expand Up @@ -119,7 +123,12 @@ export class SchemaRegistryClient implements SchemaRegistry {
schema: SchemaDescription,
options: GetSchemaPropertiesOptions = {}
): Promise<SchemaProperties> {
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,
Expand Down Expand Up @@ -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<Schema> {
return this._tracing.withSpan("SchemaRegistryClient.getSchema", options, (updatedOptions) =>
this._client.schema.getById(schemaId, updatedOptions).then(convertSchemaResponse)
);
}
getSchema(schemaId: string, options?: GetSchemaOptions): Promise<Schema>;

/**
* Gets an existing schema by version. If the schema was not found, a RestError with
Expand All @@ -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<Schema>;
// implementation
getSchema(
schemaDescription: string | SchemaDescription,
options: GetSchemaOptions = {}
): Promise<Schema> {
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,
Expand Down
17 changes: 17 additions & 0 deletions sdk/schemaregistry/schema-registry/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };

export const isDefined = <T extends Record<string, any>, V extends Array<keyof T>>(
obj: T,
props: V
): WithRequired<T, V[number]> => {
for (const prop of props) {
if (obj[prop] === undefined) {
throw new Error(`Expected ${String(prop)} to be present`);
}
}

return obj as WithRequired<T, V[number]>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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",
Expand Down