Skip to content

How to get the schema subject from a message? #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Matan-Yadaev-Forter opened this issue Apr 8, 2025 · 4 comments
Open

How to get the schema subject from a message? #297

Matan-Yadaev-Forter opened this issue Apr 8, 2025 · 4 comments

Comments

@Matan-Yadaev-Forter
Copy link

I have a service that publishes messages to a queue (not Kafka), and these messages are encoded using a schema from the Schema Registry.

On the consumer side, I want to validate that the message was encoded using a schema subject that the consumer knows and supports.

Extracting the schema ID is straightforward (bytes 1-4 of the message), but I couldn't find an API that allows retrieving the subject associated with a given schema ID.

Is there a recommended way to achieve this? Any ideas or best practices?

Thanks!

@joyrexus
Copy link

joyrexus commented Apr 9, 2025

I have the same need.

In our case, we're processing events with multiple event types sent on a single kafka topic. In other words, we're receiving events on a given topic that were encoded with varying schemas.

When processing these events, we'd like to determine the schema subject of the schema used to encode the received event so we can handle it appropriately.

As @Matan-Yadaev-Forter indicated, it's possible to get the id of the schema used to decode the incoming event message.

It'd be nice if the library exposed a convenience method for retrieving the schema subject:

const schemaSubject = getSchemaSubjectFromMessage(buffer);

@joyrexus
Copy link

joyrexus commented Apr 9, 2025

Looking at the decode method, looks like it'd be possible to fetch a message's schema by using the wireDecoder (to get the registryId) and the existing getSchema method:

Image

@Matan-Yadaev-Forter
Copy link
Author

@joyrexus You can get the schema this way, but I didn't find how I can extract the subject out of it.

@joyrexus
Copy link

joyrexus commented Apr 10, 2025

@Matan-Yadaev-Forter You should be able to use the registry API directly to get the schema subject for each version: GET /schemas/ids/{int: id}/versions

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants