Docs@skmtc/core

OpenAPI document

CLASS

OasDocument

@skmtc/core 0.63.4

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:

  1. The parser walks paths./users.get.responses.200.content...schema and sees $ref: '#/components/schemas/User'.
  2. It constructs an OasRef that resolves through the in-flight oasDocument - but at this moment oasDocument.components.schemas hasn't been populated yet (components are parsed in a later pass, or the same pass at a different node).
  3. 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 OasDocument

Examples

Basic document access
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
Iterating over 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}`);
  }
}
Working with components
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.

fields

Optional document fields (usually set later during parsing)

Properties

oasType, 'openapi'

Static identifier property for OasDocument

openapi, string

OpenAPI specification version

$self, string | undefined

The document's own URI (OpenAPI 3.2 $self)

info, OasInfo

API metadata

servers, OasServer[] | undefined
operations, OasOperation[]

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.

webhooks, OasWebhook[]

List of all 3.1 webhooks for the API (kept separate from operations)

components, OasComponents | undefined

Container object for re-usable schema items within the API

tags, OasTag[] | undefined

List of tags used by API with additional metadata

security, OasSecurityRequirement[] | undefined

List of security requirements for the API

extensionFields, Record<string, unknown> | undefined

Specification Extension fields

externalDocs, OasExternalDocs | undefined

External documentation for the API

Methods

toRefTarget

toRefTarget(pointer: string): ComponentsEntry | undefined

The parsed target of a ref that does not point at a component, by its whole pointer.

setRefTarget

setRefTarget(pointer: string, target: ComponentsEntry): void

Keeps the parsed target of a ref that does not point at a component, under its whole pointer.

removeRefTarget

removeRefTarget(pointer: string): ComponentsEntry | undefined

Removes a ref target, as removeItem removes a component, and returns it.

toJSON

toJSON(): object

Converts 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.

Returns

A JSON-serializable object representing the OpenAPI document