Skip to content

feat: #2324 publish Avro binary mock messages on the NATS binding - #2325

Merged
lbroudoux merged 1 commit into
microcks:masterfrom
drazen-nikolic:feat/nats-avro-mock-publishing
Sep 24, 2026
Merged

lbroudoux merged 1 commit into
microcks:masterfrom
drazen-nikolic:feat/nats-avro-mock-publishing

Conversation

@drazen-nikolic

Copy link
Copy Markdown
Contributor

Closes #2324

What this PR does

Mock messages declared with an Avro binary content type (avro/binary, application/avro, application/octet-stream) were published on NATS as the rendered JSON text of their example, so Avro consumers failed to decode them. Only the Kafka binding converted examples to Avro.

This PR makes the NATS binding encode those messages as Avro binary too, reusing the Kafka schema lookup:

  • ProducerManager: the Avro schema lookup is extracted from produceKafkaAvroMockMessage into a binding-agnostic retrieveAvroSchema(AsyncMockDefinition). produceNatsMockMessage now branches on Constants.AVRO_BINARY_CONTENT_TYPES and delegates to a new produceNatsAvroMockMessage.
  • NATSProducerManager: new publishMessage(String, byte[], Headers) overload; both overloads share a single publish path.

Behaviour notes:

  • NATS has no schema registry integration, so raw Avro binary is the only encoding produced (no equivalent of the Kafka REGISTRY encoding).
  • When no Avro schema can be found, nothing is published and a warning is logged, same as the Kafka binding, rather than falling back to JSON text on a channel declared as Avro.
  • Non-Avro content types on NATS are unchanged. Kafka behaviour is unchanged (refactor only).

Together with #2320, this makes both directions work for an AsyncAPI 3.0 document with Avro payloads over NATS: implementation messages can be validated against the contract, and the contract's examples can be mocked back.

Tests

  • ProducerManagerTest: Avro vs. text routing on NATS and the missing-schema case.
  • NATSProducerManagerIT: testProduceNatsAvroMockMessagesFromAsyncAPI3MultiFormatSchema publishes Avro from an AsyncAPI 3.0 Multi Format Schema document against a real NATS broker and decodes it on the consumer side (new resource user-signedup-avro-multiformat-asyncapi-3.0.yaml).

Verified locally:

  • mvn clean install from the root: all modules and unit tests pass.
  • mvn -Pit test in minions/async: 28 ITs pass.
  • A microcks-uber-async-minion image built from this branch, used with microcks-uber:1.15.0 in a downstream project's Testcontainers suite, passes its Avro-over-NATS mocking ITs (the minion publishes Avro, the consumer decodes it) and its contract ITs.

AI disclosure

This change was developed with the help of an AI assistant (Claude Code), in line with the AI Contribution Policy. I have reviewed, tested and take responsibility for the code.

🤖 Generated with Claude Code

…ding

Mock messages declared with an Avro binary content type were published on NATS
as the rendered text of their example, so a consumer decoding Avro read UTF-8
JSON and failed. Only the Kafka binding converted the example to Avro.

Move the Avro schema lookup out of produceKafkaAvroMockMessage into a
binding-agnostic retrieveAvroSchema, and branch produceNatsMockMessage on
Constants.AVRO_BINARY_CONTENT_TYPES the way the Kafka one does.
NATSProducerManager gains a byte[] publishMessage overload; both overloads now
share a single publish path.

NATS has no schema registry integration, so raw Avro binary is the only
encoding produced here - there is no equivalent of the Kafka REGISTRY fork.
When no schema can be found nothing is published, rather than falling back to
the JSON text on a channel the document declares as Avro.

Together with the preceding commit this makes both directions work for an
AsyncAPI 3.0 document with Avro payloads over NATS: the implementation's
messages can be verified against the contract, and the contract's examples can
be mocked back to the implementation.
testProduceNatsAvroMockMessagesFromAsyncAPI3MultiFormatSchema covers that
combination end to end against a real broker - it needs the Multi Format Schema
Object unwrapping to resolve the schema and this commit to encode the bytes.

Signed-off-by: Drazen Nikolic <[email protected]>
@github-actions

Copy link
Copy Markdown

👋 @drazen-nikolic

Welcome to the Microcks community! 💖

Thanks and congrats 🎉 for opening your first pull request here! Be sure to follow the pull request template or please update it accordingly.

Hope you have a great time there!

@lbroudoux

Copy link
Copy Markdown
Member

A microcks-uber-async-minion image built from this branch, used with microcks-uber:1.15.0 in a downstream project's Testcontainers suite, passes its Avro-over-NATS mocking ITs (the minion publishes Avro, the consumer decodes it) and its contract ITs.

It's awesome you also did this comprehensive test! Thanks!

payload:
fullName: Laurent Broudoux
email: "[email protected]"
age: 41

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch! This comes from a long time ago ... I'm way older now 😢

@lbroudoux lbroudoux left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks all good to me. Thanks for taking the time to push this improvement! I'll merge it once the build finished (just to check it's green)

@lbroudoux
lbroudoux merged commit 384fc30 into microcks:master Sep 24, 2026
13 checks passed
@github-actions

Copy link
Copy Markdown

🎉 @drazen-nikolic

You are now a Microcks community contributor! 💖

Thanks and congrats 🚀 on merging your first pull request! We are delighted and very proud of you! 👏

📢 If you're using Microcks in your organization, please add your company name to this list. 🙏 It really helps the project to gain momentum and credibility. It's a small contribution back to the project with a big impact.

If you need to know why and how to add yourself to the list, please read the blog post "Join the Microcks Adopters list and Empower the vibrant open source Community 🙌"

Kudos and please keep going, we need you 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support publishing Avro binary mock messages on the NATS binding

2 participants