OpenAPI document
OasDocument
Represents a complete OpenAPI v3 document in the SKMTC OAS processing system.
The OasDocument class is the root object in the OAS hierarchy, containing all the information needed to describe a complete REST API. It provides normalized access to document properties with built-in validation and error handling.
Key Features
- Normalized Structure: Operations are flattened from nested paths for easier processing
- Lazy Initialization: Fields are set after construction during parsing - see "Why empty-at-construction" below for the architectural reason.
- Type Safety: All properties are typed and validated on access
- Extensibility: Supports OpenAPI extension fields (x-* properties)
- JSON Serialization: Can be converted back to standard OpenAPI JSON format
Why empty-at-construction (forward-declared refs)
OasDocument is intentionally constructable without fields, with oasDocument.fields = ... assigned later by the parser. This is not a convenience for tests - it's load-bearing for ref resolution.
OasRef resolves through an OasDocument instance (by reaching into its components.schemas etc.). During parsing, refs are encountered before the targets they point at have been parsed:
- The parser walks
paths./users.get.responses.200.content...schemaand sees$ref: '#/components/schemas/User'. - It constructs an
OasRefthat resolves through the in-flightoasDocument- but at this momentoasDocument.components.schemashasn't been populated yet (components are parsed in a later pass, or the same pass at a different node). - The ref must still be constructable now; resolution can wait.
By creating an empty OasDocument in ParseContext's constructor and filling its fields after the walk completes, every OasRef constructed during parsing points at the same instance. Once parse() finishes and sets oasDocument.fields, every previously-issued ref becomes resolvable retroactively. If OasDocument were a value object populated at construction time, refs couldn't exist until every component had been parsed - but the components can't be parsed until the refs that point into them are constructable. Chicken-and-egg.
If you find yourself reaching for the same trick elsewhere, that's the pattern.
Consequence for code touching OasDocument: accessors throw when #fields is unset, on the assumption that no production code reads the document outside the parse flow. Test code that needs to inspect mid-parse state should call parse() first; defensive callers (e.g. removeItem) should either parse-then-inspect or check the field's setness.
Signature
import { OasDocument } from '@skmtc/core'
class OasDocumentExamples
import { OasDocument } from '@skmtc/core';
// Document is typically created during parsing
const document = new OasDocument();
document.fields = {
openapi: '3.0.0',
info: { title: 'My API', version: '1.0.0' },
operations: [
// ... parsed operations
],
components: {
schemas: {
User: { type: 'object', properties: { id: { type: 'string' } } }
}
}
};
console.log(document.info.title); // 'My API'
console.log(document.operations.length); // Number of operations// Process all operations in the document
for (const operation of document.operations) {
console.log(`${operation.method.toUpperCase()} ${operation.path}`);
if (operation.operationId) {
console.log(`Operation ID: ${operation.operationId}`);
}
}if (document.components?.schemas) {
const userSchema = document.components.schemas.get('User');
if (userSchema) {
console.log('User schema found:', userSchema);
}
}Constructor
constructor
constructor(fields?: DocumentFields)Creates a new OasDocument instance.
The document is typically created with undefined fields and populated later during the parsing process. This allows for lazy initialization and proper error handling during document processing.
Optional document fields (usually set later during parsing)
Properties
Static identifier property for OasDocument
OpenAPI specification version
The document's own URI (OpenAPI 3.2 $self)
API metadata
List of all operations for the API. In a run with onArtifact the parsed operations are let go before the file plan is built; the first time they are listed after that, each is built again and kept.
List of all 3.1 webhooks for the API (kept separate from operations)
Container object for re-usable schema items within the API
List of tags used by API with additional metadata
List of security requirements for the API
Specification Extension fields
External documentation for the API
Methods
toRefTarget
toRefTarget(pointer: string): ComponentsEntry | undefinedThe parsed target of a ref that does not point at a component, by its whole pointer.
setRefTarget
setRefTarget(pointer: string, target: ComponentsEntry): voidKeeps the parsed target of a ref that does not point at a component, under its whole pointer.
removeRefTarget
removeRefTarget(pointer: string): ComponentsEntry | undefinedRemoves a ref target, as removeItem removes a component, and returns it.
toJSON
toJSON(): objectConverts the document back to a JSON-serializable OpenAPI object.
This method serializes the document to a standard OpenAPI v3 format, which can be used for output, validation, or further processing. The resulting object follows the OpenAPI specification structure.
A JSON-serializable object representing the OpenAPI document