Skip to content

Commit

Permalink
[Schema Registry Avro] Remove old format compatability (#21648)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR
@azure/schema-registry-avro

### Issues associated with this PR
Fixes #20063

### Describe the problem that is addressed by this PR
The old payload format was planned to be supported until the last beta before the first stable release and since the first stable release is scheduled for May, the compatability should be removed now.

### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_

### Checklists
- [x] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_
- [x] Added a changelog (if necessary)
  • Loading branch information
deyaaeldeen authored Apr 28, 2022
1 parent f8cd963 commit 61daf06
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 69 deletions.
11 changes: 4 additions & 7 deletions sdk/schemaregistry/schema-registry-avro/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# Release History

## 1.0.0-beta.9 (Unreleased)

### Features Added

### Breaking Changes

### Bugs Fixed
## 1.0.0 (2022-05-10)

### Other Changes

- Compatability for old payload format has been removed.
- Errors may include a `cause` field that stores inner errors if any.

## 1.0.0-beta.8 (2022-04-05)

### Features Added
Expand Down
10 changes: 0 additions & 10 deletions sdk/schemaregistry/schema-registry-avro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ by setting the `messageAdapter` option in the constructor with a corresponding
message producer and consumer. Azure messaging client libraries export default
adapters for their message types.

### Backward Compatibility

The serializer v1.0.0-beta.5 and under serializes data into binary arrays. Starting from
v1.0.0-beta.6, the serializer returns messages instead that contain the serialized payload.
For a smooth transition to using the newer versions, the serializer also supports
deserializing messages with payloads that follow the old format where the schema ID
is part of the payload.

This backward compatibility is temporary and will be removed in v1.0.0.

## Examples

### Serialize and deserialize an `@azure/event-hubs`'s `EventData`
Expand Down
2 changes: 1 addition & 1 deletion sdk/schemaregistry/schema-registry-avro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure/schema-registry-avro",
"version": "1.0.0-beta.9",
"version": "1.0.0",
"description": "Schema Registry Avro Serializer Library with typescript type definitions for node.js and browser.",
"sdk-type": "client",
"main": "dist/index.js",
Expand Down
49 changes: 2 additions & 47 deletions sdk/schemaregistry/schema-registry-avro/src/avroSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,67 +224,22 @@ function getSchemaId(contentType: string): string {
return contentTypeParts[1];
}

/**
* Tries to deserialize data in the preamble format. If that does not succeed, it
* returns it as is.
* @param data - The message content
* @param contentType - The message content type
* @returns a message
*/
function convertPayload(data: Uint8Array, contentType: string): MessageContent {
try {
return tryReadingPreambleFormat(Buffer.from(data));
} catch (_e: unknown) {
return {
data,
contentType,
};
}
}

function convertMessage<MessageT>(
message: MessageT,
adapter?: MessageAdapter<MessageT>
): MessageContent {
const messageConsumer = adapter?.consume;
if (messageConsumer) {
const { data, contentType } = messageConsumer(message);
return convertPayload(data, contentType);
return messageConsumer(message);
} else if (isMessageContent(message)) {
return convertPayload(message.data, message.contentType);
return message;
} else {
throw new Error(
`Expected either a message adapter to be provided to the serializer or the input message to have data and contentType fields`
);
}
}

/**
* Maintains backward compatability by supporting the serialized value format created
* by earlier beta serializers
* @param buffer - The input buffer
* @returns a message that contains the data and content type with the schema ID
*/
function tryReadingPreambleFormat(buffer: Buffer): MessageContent {
const FORMAT_INDICATOR = 0;
const SCHEMA_ID_OFFSET = 4;
const PAYLOAD_OFFSET = 36;
if (buffer.length < PAYLOAD_OFFSET) {
throw new RangeError("Buffer is too small to have the correct format.");
}
const format = buffer.readUInt32BE(0);
if (format !== FORMAT_INDICATOR) {
throw new TypeError(`Buffer has unknown format indicator.`);
}
const schemaIdBuffer = buffer.slice(SCHEMA_ID_OFFSET, PAYLOAD_OFFSET);
const schemaId = schemaIdBuffer.toString("utf-8");
const payloadBuffer = buffer.slice(PAYLOAD_OFFSET);
return {
data: payloadBuffer,
contentType: `${avroMimeType}+${schemaId}`,
};
}

function getSerializerForSchema(schema: string): AVSCSerializer {
return wrapError(
() => avro.Type.forSchema(JSON.parse(schema), { omitRecordMethods: true }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe("AvroSerializer", function () {
assert.equal(deserializedValue.age, 30);
});

it("deserializes from the old format", async () => {
it("ignores the old format", async () => {
const registry = createTestRegistry();
const schemaId = await registerTestSchema(registry);
const serializer = await createTestSerializer<MessageContent>({
Expand All @@ -142,12 +142,12 @@ describe("AvroSerializer", function () {

data.write(schemaId, 4, 32, "utf-8");
payload.copy(data, 36);
assert.deepStrictEqual(
await serializer.deserialize({
await assert.isRejected(
serializer.deserialize({
data,
contentType: "avro/binary+000",
}),
testValue
`Schema does not exist`
);
});

Expand Down

0 comments on commit 61daf06

Please sign in to comment.