Bundler strategy
This page is for maintainers changing the bundler in asyncapi-generator-core,
package dev.banking.asyncapi.generator.core.bundler. The user-level behavior
is described in
Bundle multi-file documents.
Contract
- Input: the load result, whose document has been read, parsed, linked, and validated. Every reference is linked.
- Output: a
BundledAsyncApiDocument, the only input the generator accepts. - Invariants:
- The bundler is a pure transformation of the domain model. It does not read files, parse YAML or JSON, validate, or generate code, and it does not modify the loaded document.
- Every call starts with fresh state, so calls do not affect each other.
- Reference identity includes the file a reference was written in, so same-named files in different directories never share visited or recursion state.
Steps
The entry type is AsyncApiBundler. One call bundles, in this order, the info,
servers, channels, operations, and components of the root document. Each
member that is either inline or a $ref is bundled the same way:
- An inline object has its members bundled.
- A
$refnot yet followed on the current path becomes an inline copy of the bundled object it leads to. - A
$refalready followed on the current path stays a$ref, which ends recursion.
Schemas that are promoted along the way are added to components.schemas
after the walk. The order of the steps decides the order of the promoted
schemas.
Rules
The bundlers replace resolved references as follows:
- A reference to a non-schema object in another file is replaced by the bundled object.
- A reference to a non-schema object follows reference chains to the end: a
local reference to a component that is itself an external
$refis replaced by the fully resolved object, so no external$refremains. - References that point into the root document by position stay references and
are not inlined: an operation's
channelandmessages, and an operation reply'schannelandmessages. Only a reply'saddressis bundled. The targets are root channels, which are bundled themselves. - Binding schema fields (such as the Kafka message
key) are always bundled as schemas, so a local$refinside an external file is promoted or inlined instead of dangling. The bundled model'skafkaKeySchemaandprotocolBindingshold the bundled schemas. A message key that is an external reference is promoted tocomponents.schemasunder the last segment of its pointer (key-schemas.yaml#/myAccountKeybecomes#/components/schemas/myAccountKey), so the generated key type keeps the name it has when generated from the original files. itemsin array form (tuple items) are bundled and take part in recursion analysis like every other child schema.- A reference to a component schema of the root document stays a reference
(
#/components/schemas/<Name>). The component itself is bundled once with the root components. - A reference that leaves the root document and reaches a component schema,
or a recursive schema, is promoted: the schema is added to the bundled
components.schemasunder the last segment of its pointer and the reference is rewritten to point there. Only a pointer of exactly three segments (#/components/schemas/<Name>) names a component schema; a deeper pointer such as#/components/schemas/Order/properties/totalis an ordinary schema and is inlined. A second promoted schema with the same name and the same serialized content reuses the first; different content fails withAsyncApiBundlingException.PromotedSchemaNameCollision. - Other schema references are inlined. Visited references are tracked, so recursion through inlined schemas ends; recursive schemas are promoted instead of inlined.
Specification extensions (x-*) are part of the model of every extensible
object and are written to the bundle.
Keeping and promoting component schema names is what makes code generated
from a bundle identical to code generated from the original files: generated
type names come from schema names. The system tests assert this for every
scenario that writes an outputFile.
Tests
BundledDocumentApprovalTest protects complete bundled documents for the
multi-file, fragment, recursive, and component-catalog fixtures under
asyncapi-generator-core/src/test/resources/approvals/bundler. The
bundling and external-references system-test scenarios cover the same
behavior through every frontend.