Architecture
This page is for contributors and for anyone deciding whether to depend on the internals. The public contract is the decorator surface and the emitted document; everything below is implementation.
The pipeline
Section titled “The pipeline”api.tsp └─ tsp compile ├─ decorators (src/decorators.ts, src/minimal-decorators.ts) │ └─ state (src/state.ts) ├─ schema generation (src/schema-generator.ts) │ └─ AsyncAPISchemaEmitter (src/schema-emitter.ts) ├─ document assembly (src/document-builder.ts) │ └─ src/builders/* (channels, operations, messages, servers, …) └─ write output (src/emitter.ts, src/schema-splitter.ts)- Decorators validate their configuration at the boundary and write into a per-program state map. Validation failures become library diagnostics.
- Schema generation walks the type graph through
@typespec/asset-emitterand produces JSON Schema fragments pluscomponents.schemasdeclarations. - Document assembly reads decorator state and schema results, then builds channels, operations, messages, servers, security schemes, tags, and reusable components.
- Output serializes YAML or JSON. With
split-schemas, each schema is written to its own file and every$refis rewritten.
Two boundaries that matter
Section titled “Two boundaries that matter”The schema seam. @typespec/asset-emitter is imported by exactly three files: src/schema-generator.ts, src/schema-emitter.ts, and src/extract-value.ts. ESLint enforces that containment. The eleven document builders consume only @typespec/compiler. This is deliberate: it keeps an eventual direct-AST rewrite local instead of touching every builder.
State, not global singletons. Decorators communicate with the builder through src/state.ts and the TypeSpec state map (src/state-compatibility.ts). Multi-namespace programs rely on this isolation, so state keys are scoped per program.
Schema generation
Section titled “Schema generation”AsyncAPISchemaEmitter extends the framework’s TypeEmitter<JsonSchema, AsyncAPIEmitterOptions>. Only methods that are actual asset-emitter dispatch keys are overridden — an override named after a non-key method is dead code.
Notable behaviors:
- Named models, enums, and unions become
components.schemasdeclarations and are referenced with$ref. - Inheritance uses
allOf; the derived schema contains only its own properties. - Unions of models use
oneOf; mixed unions useanyOf; string-literal unions useenum. - Generic instantiations get argument-derived names (
Page<User>becomesPageUser);Record<K, V>inlines asadditionalProperties. - Constraints and metadata are table-driven in
src/constraint-mapper.ts. Validation keywords are skipped on$refschemas; annotation keywords are allowed as$refsiblings. - Framework-interned values are never mutated in place. Property schemas are cloned before constraints are applied, because the asset-emitter may share one value object across every use of a type.
Binding validation
Section titled “Binding validation”src/constants/generated-bindings.ts is generated from @asyncapi/specs/bindings/. It feeds:
- valid versions per protocol, with a small override map for schema-restricted protocols;
- the placement matrix (channel, operation, message) per protocol;
- field-level value validation.
src/validation/binding-validator.ts normalizes binding keys, injects missing bindingVersion, checks placement, and warns on fields that cannot be placed. A binding is never emitted as a version-only shell.
Testing layers
Section titled “Testing layers”| Layer | What it protects |
|---|---|
| Compliance | AJV validation against the official AsyncAPI 3.1.0 schema, plus $ref and shape assertions |
| Golden | Byte-level locks on representative documents |
| Property | fast-check invariants over generated specs |
| Integration | Mixed emitters, multi-namespace isolation, CLI behavior |
| Examples | Every repository example compiles clean and validates in CI |
Coverage runs through Bun’s native runtime coverage because the compiler loads dist/ through a virtual filesystem that vitest’s transform never sees.
Where to go next
Section titled “Where to go next”- Contributing — build, verify, and release workflow
- Diagnostics — the full code list
- Decorators — the public surface