Skip to content

AsyncAPI 3.x: publish a message's example, some of the time - #1754

Draft
LautaroPetaccio wants to merge 1 commit into
feature/asyncapi-fitnessfrom
feature/asyncapi-examples
Draft

LautaroPetaccio wants to merge 1 commit into
feature/asyncapi-fitnessfrom
feature/asyncapi-examples

Conversation

@LautaroPetaccio

@LautaroPetaccio LautaroPetaccio commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator

Thirteenth in the AsyncAPI stack, on top of #1753. Draft, like the two below it.

Why

A message's examples are what the document's author knows the service accepts. A service that silently drops what it does not recognise — the normal behaviour of a Kafka consumer or a WebSocket handler faced with a malformed message — may never reply to a payload sampled from the schema alone, and a search that never sees a reply never gets to tell the reply variants apart. Publishing the author's example some of the time is the cheapest way to make sure the search starts from something that works.

What it does

With --probAsyncApiExamples p (@Experimental, @Probability, 0 by default), the first example of a message is offered as a whole value beside the schema-derived genes, chosen with probability p when the message is sampled. Same for a headers example, minus the field the correlation id is stamped into, which the headers gene does not have either.

How — no new machinery

REST already turns a schema's example into a ChoiceGene between the example and the schema-derived genes, at probUseExamples. So the example is put on the message's own copy of the payload schema before it goes to RestActionBuilderV3.createGeneForDTO, and that machinery does the rest. Ten lines in AsyncApiGeneBuilder, one line in options().

Two limits, both stated in the code:

  • Only the first example. The genes are built through an OpenAPI 3.0 wrapper, and swagger-parser drops the plural examples on a 3.0 schema — checked empirically, not assumed; the singular example survives. A TODO says where to pass them all once the builder can take them. In the corpus, every message with examples has exactly one.
  • Object payloads only. The builder attaches a deprecation warning to example on a non-object schema, and that would reach the user as a complaint about a document that is in order.

The TODO this resolves in AsyncApiSampler is removed.

Testing

AsyncApiGeneBuilderTest (+5): the Scalar PlanetCreated example published whole when asked for every time; nothing changing shape when not asked; a scalar payload left alone; a headers example losing the correlation field; only the first of two examples used. AsyncApiSamplerTest (+1): through the sampler with --probAsyncApiExamples=1.0, the payload of a sampled action is the author's example.

All AsyncAPI suites plus the config ones: 199 tests, 0 failures.

@LautaroPetaccio
LautaroPetaccio added this pull request to stack #1712 September 13, 2026 21:33
@LautaroPetaccio
LautaroPetaccio marked this pull request as ready for review September 13, 2026 21:34
@LautaroPetaccio
LautaroPetaccio marked this pull request as draft September 13, 2026 21:35
@arcuri82
arcuri82 force-pushed the feature/asyncapi-examples branch from f2b10c8 to 5d3566d Compare September 14, 2026 10:51
@LautaroPetaccio
LautaroPetaccio force-pushed the feature/asyncapi-examples branch from 5d3566d to 66440f3 Compare September 14, 2026 22:17
@LautaroPetaccio
LautaroPetaccio force-pushed the feature/asyncapi-examples branch from 66440f3 to 179e5d3 Compare September 15, 2026 16:29
@LautaroPetaccio
LautaroPetaccio force-pushed the feature/asyncapi-examples branch 2 times, most recently from 2495b28 to 82d1889 Compare September 16, 2026 01:16
@LautaroPetaccio
LautaroPetaccio force-pushed the feature/asyncapi-examples branch from 82d1889 to 18be100 Compare September 16, 2026 21:57
A document's message examples are what its author knows the service
accepts. A service that silently drops what it does not recognise may
never answer a sampled payload, so with --probAsyncApiExamples the first
example of a message is offered as a whole value beside the schema-derived
genes, at that probability. Off by default, like every new feature.

It reuses what REST already does with a schema's 'example': the example is
put on the message's own copy of the payload schema, and the gene builder
turns it into the same choice it offers REST. Only the first example is
used: the OpenAPI parser the genes go through reads the schemas as 3.0,
which drops the plural 'examples'. Scalars are left alone, as the builder
would complain about 'example' on them. A headers example loses the field
the correlation id is stamped into, as the headers gene did before it.
@LautaroPetaccio
LautaroPetaccio force-pushed the feature/asyncapi-examples branch from 18be100 to d945333 Compare September 16, 2026 22:01
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

Successfully merging this pull request may close these issues.

1 participant