Repository navigation
[feat][client] Expose Client.getSchemaInfo - #512
Conversation
Add Client.getSchemaInfo(topic, version), which fetches the schema registered for a topic at a given version. It wraps the C++ client's Client::getSchemaInfoAsync and resolves to the same SchemaInfo shape that producers and consumers accept. Together with Message.getLongSchemaVersion() (apache#509), a consumer can look up the writer schema of each message and decode messages produced with an older, compatible schema. Master Issue: apache#362 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
The change silently drops registered __proto__ schema properties and accepts invalid numeric versions that can return the wrong writer schema.
2 open issues
What changed in this PR
Exposes Client.getSchemaInfo(topic, version) so consumers can retrieve a message’s writer schema over the binary protocol. This complements the schema-version access added in #509 and supports manual decoding across schema evolution.
Changes:
- Adds a promise-based schema lookup through the JavaScript and native client layers.
- Converts native schema information into the existing JavaScript
SchemaInfoshape. - Adds TypeScript declarations and tests for versioned lookup and invalid arguments.
| File | Description |
|---|---|
tstest.ts |
Checks the new method’s TypeScript return type. |
tests/client.test.js |
Tests schema evolution, missing versions, and invalid arguments. |
src/SchemaInfo.h |
Declares schema conversion helpers. |
src/SchemaInfo.cc |
Converts schema types, definitions, and metadata into JavaScript objects. |
src/Client.js |
Exposes the JavaScript forwarding method. |
src/Client.h |
Declares the native binding method. |
src/Client.cc |
Implements asynchronous lookup and promise resolution or rejection. |
index.d.ts |
Declares and documents the public API. |
Review effort: Balanced
|
Could you add a test for fetching a KeyValue schema with getSchemaInfo() and verify that the binary schema data is preserved? The current UTF-8 string conversion may corrupt the binary length fields in KeyValue schemas. |
…ents - Return the schema data from getSchemaInfo as a Buffer. KeyValue schema data is binary, and converting it to a UTF-8 string replaced its length prefixes with U+FFFD. Accept a Buffer as the schema option too, so a fetched schema can be passed back to a producer or consumer. - Reject a schema version that is not a non-negative safe integer. Int64Value() turned 0.5, -1 or NaN into a different, existing version. - Define schema properties as own properties, so a "__proto__" key is not dropped by the prototype setter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@gaoran10 yes added a test and conversion does corrupt it. changing |

Fixes #362
Follow-up to #509, which exposed
Message.getLongSchemaVersion().Motivation
A consumer of an Avro or JSON topic needs the writer schema of each message to decode messages produced with an older, compatible schema. #509 gives the consumer the writer schema version. This PR lets it fetch the schema for that version over the binary protocol, without an admin REST call. The bundled C++ client already provides
Client::getSchemaInfoAsync(topic, version, callback), but the Node client does not expose it.Modifications
src/Client.h,src/Client.cc,src/Client.js: addClient.getSchemaInfo(topic, version). It wrapsClient::getSchemaInfoAsyncand returns a promise. The name follows the C++ method without theAsyncsuffix, asgetPartitionsForTopicandclosedo. A missing topic or version rejects withFailed to get schema info: <result>. A topic that isn't a string, or a version that isn't a non-negative safe integer, throws aTypeErrorrather than being coerced to a different version.src/SchemaInfo.h,src/SchemaInfo.cc: convert apulsar::SchemaInfointo the JSSchemaInfoobject (schemaType,name,schema,properties) using the existingschemaTypename table. A schema type with no entry in the table rejects instead of returning a name the typings don't allow.schemais returned as aBufferof the raw schema data, as in the C++ (std::string) and Java (byte[]) clients.KeyValueschema data is binary, and a UTF-8 string would replace its length prefixes withU+FFFD.schemaoption of producers and consumers now also accepts aBuffer, so a fetched schema can be passed back as-is. Strings work as before.__proto__key is kept.index.d.ts: declaregetSchemaInfo(topic: string, version: number): Promise<SchemaInfo & { schema: Buffer }>onClient, and widenSchemaInfo.schematostring | Buffer.tstest.ts: add a typing check for the new method.tests/client.test.js: add thegetSchemaInfotests.Verifying this change
This change added tests and can be verified as follows:
Client/getSchemaInfo. It:getSchemaInfowith each received message'sgetLongSchemaVersion(), and asserts it returnsschemaType: 'Avro'and the matching schema definition;Failed to get schema info:.KeyValue schema. It registers aKeyValueschema whose key schema is longer than 127 bytes through the admin API, fetches it, and asserts the binary layout decodes to the original key and value schemas. It then creates a producer with the fetched schema and asserts the broker registers no new version.Schema properties, which asserts properties, including a__proto__key, come back as registered.Invalid arguments, which asserts a missing version,0.5,-1,NaN,Infinity,2 ** 53and'0'throw.Locally,
Client/getSchemaInfofails if the binding ignoresversionand always fetches version 0, andSchema propertiesfails if properties are assigned withSet.Documentation
doc-requireddoc-not-needed(New
Clientmethod, documented inindex.d.ts, which generates the API reference. The client feature matrix has no row for this.)docdoc-completeCreated with Claude Opus 5.5 in Claude Code.