Reader contract
This page defines the reader stage of asyncapi-generator-core: what it takes
in, what it hands to the parser, and what it refuses. The reader is internal to
core. Frontends reach it only through AsyncApiGenerator.
The reader converts YAML or JSON files into a format-independent document tree.
It selects the reader by file extension, enforces resource limits, and keeps
source locations so later stages can point diagnostics at a line in the right
file. It lives in the source package.
Contract
- Input: a file with the extension
.yaml,.yml, or.json, or content that is already loaded. - Output: an
InputDocument, which holds theDocumentSource(file, content, format) and the rootDocumentNodeof the tree. SeeInputDocument.kt. - Entry point: the
SourceReaderobject.read(File)selects the format from the extension, reads the content within the byte limit, and delegates toread(DocumentSource), which runs the YAML or JSON reader.
Invariants:
- Every node keeps its source file, path, line, and column.
- Object members keep the location of the key apart from the location of the value. A validator can tell a missing key from a key with an invalid value.
- Document order is kept for object members and array elements.
- An explicit
nullis aDocumentNull, distinct from an absent member.
The reader does not interpret AsyncAPI. It does not check structure, resolve
$ref, or give meaning to keys. A syntactically valid document may have an
object, array, scalar, or null root; the parser requires an object.
Node types
Every node carries a SourceLocation: file, NodePath (such as
root.channels.userEvents, root.servers[0], or root["channel.name"]),
one-based line, and one-based column. The node types are in
DocumentNode.kt.
| Node | Plain runtime value |
|---|---|
DocumentObject |
Map<String, Any?>, members in document order |
DocumentArray |
List<Any?> |
DocumentString |
String |
DocumentNumber |
Int, Long, BigInteger, Double, or BigDecimal |
DocumentBoolean |
Boolean |
DocumentNull |
null |
A SourceId identifies a document within one load. The parser combines it with
a NodePath to address a node across all documents of the load.
Syntax rules
Both formats produce the same tree, so a contract means the same in YAML and JSON.
- File input is decoded as strict UTF-8. Malformed bytes are rejected, not replaced. A UTF-8 BOM is accepted, and CRLF input keeps correct locations.
- Object member names are always strings. JSON enforces this in its grammar.
A YAML mapping key must resolve as a string; collections, numbers, booleans,
and null are rejected. Quoted keys such as
"true"and"42"stay strings. - Duplicate keys are rejected in both formats.
- YAML merge keys (
<<), custom tags, and unsupported node kinds are malformed input. - Only JSON-compatible number spellings are accepted. YAML-only octal forms and non-finite numbers are rejected. Timestamp-looking values stay strings.
- Plain
yes,no,on, andoffstay strings. Onlytrueandfalsebecome booleans. Quoted numbers and booleans stay strings. - Integers use
IntorLongwhen they fit andBigIntegerotherwise. Decimals stayDoublewhen the value survives the conversion exactly and useBigDecimalotherwise. YAML and JSON use the same representation. - Content after the root document, such as a second YAML document, is rejected.
- Quoting and block style do not survive as data.
Resource limits
SourceLimits holds the limits of one load. The readers enforce the first five
for each document. SourceBudget enforces the rest across the root document,
its external documents, and native schema assets. They protect against
oversized, deeply nested, or endlessly referencing input.
| Limit | Default | Scope |
|---|---|---|
| Max document bytes | 20 MiB | Per document |
| Max document characters | 20 MiB | Per document |
| Max nesting depth | 100 | Per document |
| Max collection aliases | 50 | Per document |
| Max number characters | 1,000 | Per document |
| Max source documents | 256 | Whole load |
| Max reference targets | 4,096 | Whole load |
| Max external reference depth | 64 | Whole load |
| Max aggregate source bytes | 64 MiB | Whole load |
| Max native schema asset size | 20 MiB | Whole load |
A document that exceeds a per-document limit fails with
ResourceLimitExceeded. A whole-load limit is reported as the
parser.load-resource-limit-exceeded diagnostic, with the limit, the maximum,
the observed value, and the location of the reference that caused it. Each
canonical source and each target counts once, so cycles end without using the
budget again. An unrecognized SnakeYAML exception is MalformedDocument, not a
limit failure.
Referenced local files are trusted build input. The loader has no project-root sandbox.
Errors
Every failure is a DocumentReadException that names the file and, when known,
the location. Library, decoding, and file-access exceptions do not escape as the
top-level failure; the original cause is kept.
| Exception | When |
|---|---|
MissingDocument |
The file does not exist |
NotAFile |
The path exists but is not a regular file |
UnsupportedFormat |
The extension is not .yaml, .yml, or .json |
UnreadableDocument |
The file cannot be read (I/O or security error) |
EmptyDocument |
The content is empty or only whitespace |
ResourceLimitExceeded |
The document exceeds a per-document limit |
MalformedDocument |
The content is not valid UTF-8, YAML, or JSON, or uses a construct the reader rejects, such as a recursive YAML alias |
InvalidMappingKey |
A YAML mapping key is not a string |
DuplicateKey |
An object declares the same key twice |
The exception exposes structured fields independently of its message: file on
every subtype, location when the failure has a source mark, memberName on
DuplicateKey, format on UnsupportedFormat, and limit and maximum on
ResourceLimitExceeded.
Known limitations
- The whole document must fit in memory; there is no streaming input.
- SnakeYAML decides how YAML 1.1 and 1.2 differ. The reader does not expose a version choice.
Tests
DocumentReaderContractTest holds the behaviour both readers share. The format
readers and SourceReader have their own tests next to it, under
asyncapi-generator-core/src/test/kotlin/dev/banking/asyncapi/generator/core/source.