Skip to content

Commit 4d16087

Browse files
authored
[Schema Registry Avro] Support backward compatibility for old preamble format (Azure#20091)
* [Schema Registry Avro] Support backward compatibility for old preamble format * fix linting * address feedback
1 parent 7eb4967 commit 4d16087

File tree

4 files changed

+81
-8
lines changed

4 files changed

+81
-8
lines changed

sdk/schemaregistry/schema-registry-avro/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Features Added
66

7-
- The serializer APIs have been revamped to work on messages instead of buffers where the payload is the pure encoded-data. The schema ID became part of the content type of that message. This change will improve the experience of using this encoder with the other messaging clients (e.g. Event Hubs, Service Bus, and Event Grid clients).
7+
- The encoder APIs have been revamped to work on messages instead of buffers where the payload is the pure encoded-data. The schema ID became part of the content type of that message. This change will improve the experience of using this encoder with the other messaging clients (e.g. Event Hubs, Service Bus, and Event Grid clients). The encoder also supports decoding messages with payloads that follow the old format where the schema ID was part of the payload.
88
- `decodeMessageData` now supports decoding using a different but compatible schema
99

1010
### Breaking Changes

sdk/schemaregistry/schema-registry-avro/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ by setting the `messageAdapter` option in the constructor with a corresponding
5858
message producer and consumer. Azure messaging client libraries export default
5959
adapters for their message types.
6060

61+
### Backward Compatibility
62+
63+
The encoder v1.0.0-beta.5 and under encodes data into binary arrays. Starting from
64+
v1.0.0-beta.6, the encoder returns messages instead that contain the encoded payload.
65+
For a smooth transition to using the newer versions, the encoder also supports
66+
decoding messages with payloads that follow the old format where the schema ID
67+
is part of the payload.
68+
69+
This backward compatibility is temporary and will be removed in v1.0.0.
70+
6171
## Examples
6272

6373
### Encode and decode an `@azure/event-hubs`'s `EventData`

sdk/schemaregistry/schema-registry-avro/src/schemaRegistryAvroEncoder.ts

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class SchemaRegistryAvroEncoder<MessageT = MessageWithMetadata> {
9595
options: DecodeMessageDataOptions = {}
9696
): Promise<unknown> {
9797
const { schema: readerSchema } = options;
98-
const { body, contentType } = getPayloadAndContent(message, this.messageAdapter);
98+
const { body, contentType } = convertMessage(message, this.messageAdapter);
9999
const buffer = Buffer.from(body);
100100
const writerSchemaId = getSchemaId(contentType);
101101
const writerSchema = await this.getSchema(writerSchemaId);
@@ -201,18 +201,63 @@ function getSchemaId(contentType: string): string {
201201
return contentTypeParts[1];
202202
}
203203

204-
function getPayloadAndContent<MessageT>(
204+
/**
205+
* Tries to decode a body in the preamble format. If that does not succeed, it
206+
* returns it as is.
207+
* @param body - The message body
208+
* @param contentType - The message content type
209+
* @returns a message with metadata
210+
*/
211+
function convertPayload(body: Uint8Array, contentType: string): MessageWithMetadata {
212+
try {
213+
return tryReadingPreambleFormat(Buffer.from(body));
214+
} catch (_e: unknown) {
215+
return {
216+
body,
217+
contentType,
218+
};
219+
}
220+
}
221+
222+
function convertMessage<MessageT>(
205223
message: MessageT,
206-
messageAdapter?: MessageAdapter<MessageT>
224+
adapter?: MessageAdapter<MessageT>
207225
): MessageWithMetadata {
208-
const messageConsumer = messageAdapter?.consumeMessage;
226+
const messageConsumer = adapter?.consumeMessage;
209227
if (messageConsumer) {
210-
return messageConsumer(message);
228+
const { body, contentType } = messageConsumer(message);
229+
return convertPayload(body, contentType);
211230
} else if (isMessageWithMetadata(message)) {
212-
return message;
231+
return convertPayload(message.body, message.contentType);
213232
} else {
214233
throw new Error(
215-
`Either the messageConsumer option should be defined or the message should have body and contentType fields`
234+
`Expected either a message adapter to be provided to the encoder or the input message to have body and contentType fields`
216235
);
217236
}
218237
}
238+
239+
/**
240+
* Maintains backward compatability by supporting the encoded value format created
241+
* by earlier beta serializers
242+
* @param buffer - The input buffer
243+
* @returns a message that contains the body and content type with the schema ID
244+
*/
245+
function tryReadingPreambleFormat(buffer: Buffer): MessageWithMetadata {
246+
const FORMAT_INDICATOR = 0;
247+
const SCHEMA_ID_OFFSET = 4;
248+
const PAYLOAD_OFFSET = 36;
249+
if (buffer.length < PAYLOAD_OFFSET) {
250+
throw new RangeError("Buffer is too small to have the correct format.");
251+
}
252+
const format = buffer.readUInt32BE(0);
253+
if (format !== FORMAT_INDICATOR) {
254+
throw new TypeError(`Buffer has unknown format indicator.`);
255+
}
256+
const schemaIdBuffer = buffer.slice(SCHEMA_ID_OFFSET, PAYLOAD_OFFSET);
257+
const schemaId = schemaIdBuffer.toString("utf-8");
258+
const payloadBuffer = buffer.slice(PAYLOAD_OFFSET);
259+
return {
260+
body: payloadBuffer,
261+
contentType: `${avroMimeType}+${schemaId}`,
262+
};
263+
}

sdk/schemaregistry/schema-registry-avro/test/schemaRegistryAvroEncoder.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,22 @@ describe("SchemaRegistryAvroEncoder", function () {
189189
/no matching field for default-less com.azure.schemaregistry.samples.AvroUser.age/
190190
);
191191
});
192+
193+
it("decodes from the old format", async () => {
194+
const registry = createTestRegistry();
195+
const schemaId = await registerTestSchema(registry);
196+
const encoder = await createTestEncoder(false, registry);
197+
const payload = testAvroType.toBuffer(testValue);
198+
const buffer = Buffer.alloc(36 + payload.length);
199+
200+
buffer.write(schemaId, 4, 32, "utf-8");
201+
payload.copy(buffer, 36);
202+
assert.deepStrictEqual(
203+
await encoder.decodeMessageData({
204+
body: buffer,
205+
contentType: "avro/binary+000",
206+
}),
207+
testValue
208+
);
209+
});
192210
});

0 commit comments

Comments
 (0)