Schemas
OasRef
Represents an OpenAPI reference ($ref) in the SKMTC OAS processing system.
The OasRef class handles OpenAPI JSON Reference Objects that point to reusable components within the same document. It provides type-safe reference resolution with support for chained references and circular reference detection.
Key Features
- Type Safety: Generic parameter ensures resolved types match the reference type
- Lazy Resolution: References are resolved on-demand, not during construction
- Chain Resolution: Handles references that point to other references
- Loop Detection: A chain of references that loops names nothing (
OasRef.toLoop) - Type Validation: Ensures resolved objects match expected reference types
Signature
import { OasRef } from '@skmtc/core'
class OasRef<T extends OasRefData['refType']> extends OasBaseThe type of component this reference points to
Examples
import { OasRef } from '@skmtc/core';
// Reference to a schema component
const userRef = new OasRef<'schema'>({
refType: 'schema',
$ref: '#/components/schemas/User'
}, document);
// Resolve the reference
const userSchema = userRef.resolve();
console.log(userSchema.properties); // Access resolved schema properties// Schema reference
const schemaRef = new OasRef<'schema'>({
refType: 'schema',
$ref: '#/components/schemas/Product'
}, document);
// Response reference
const responseRef = new OasRef<'response'>({
refType: 'response',
$ref: '#/components/responses/ErrorResponse'
}, document);
// Parameter reference
const paramRef = new OasRef<'parameter'>({
refType: 'parameter',
$ref: '#/components/parameters/PageSize'
}, document);function processSchemaOrRef(schema: OasSchema | OasRef<'schema'>) {
if (schema.isRef()) {
// Handle reference
const { refName, refPath } = schema;
console.log(`Processing reference: ${refName} in ${refPath}`);
// Resolve only when needed
const resolved = schema.resolve();
return processed(resolved);
} else {
// Handle direct schema
return process(schema);
}
}// References can point to other references
const chainedRef = new OasRef<'schema'>({
refType: 'schema',
$ref: '#/components/schemas/AliasToUser'
}, document);
// resolve() automatically follows the chain
const finalSchema = chainedRef.resolve(); // Follows chain to final schema
// resolveOnce() resolves only one step
const oneStep = chainedRef.resolveOnce(); // May still be a referenceConstructor
constructor
constructor(fields: RefFields<T>, context: RefContext)Creates a new OAS reference instance.
Reference field data including refType and $ref
The parse context, or what clone passes in its place: the document containing the referenced component (refs resolve through the document's components) and the current trail.
Properties
OAS type identifier
Type identifier
The kind of component the reference points to.
The JSON Reference, as written: a component (#/components/<bucket>/<name>) or any other place in the document (#/components/schemas/Pet/properties/tag).
The last segment of the $ref's pointer, unescaped: Pet for #/components/schemas/Pet, schema0 for #/components/schemas/__sharedInput/$defs/schema0. Empty for a $ref that ends in /, which names nothing.
The pointer before refName, with no percent escapes: #/components/schemas for a schema component, #/components/schemas/__sharedInput/$defs for the $defs entry above. A generator that names by refName alone may ignore it.
Use-site nullability (see SiblingKeywords.nullable). Assigned even when undefined, so 'nullable' in ref is always true and the value-function nullable read picks it up uniformly.
Use-site readOnly (see SiblingKeywords.readOnly).
Use-site writeOnly (see SiblingKeywords.writeOnly).
The type written next to this reference (see SiblingKeywords._type).
The title next to this reference (see SiblingKeywords).
The description next to this reference (see SiblingKeywords).
The deprecated next to this reference (see SiblingKeywords).
The example next to this reference (see SiblingKeywords).
The default next to this reference (see SiblingKeywords).
The enum next to this reference (see SiblingKeywords).
The extensions (x-*) next to this reference (see SiblingKeywords).
The format next to this reference (see SiblingKeywords).
The minLength next to this reference (see SiblingKeywords).
The maxLength next to this reference (see SiblingKeywords).
The pattern next to this reference (see SiblingKeywords).
The multipleOf next to this reference (see SiblingKeywords).
The minimum next to this reference (see SiblingKeywords).
The maximum next to this reference (see SiblingKeywords).
The exclusiveMinimum next to this reference (see SiblingKeywords).
The exclusiveMaximum next to this reference (see SiblingKeywords).
The minItems next to this reference (see SiblingKeywords).
The maxItems next to this reference (see SiblingKeywords).
The uniqueItems next to this reference (see SiblingKeywords).
The properties next to this reference (see SiblingKeywords).
The required next to this reference (see SiblingKeywords).
The additionalProperties next to this reference (see SiblingKeywords).
The minProperties next to this reference (see SiblingKeywords).
The maxProperties next to this reference (see SiblingKeywords).
Returns the parsed document this ref resolves through: the parent OasDocument, wrapped.
Methods
isRef
isRef(): this is OasRef<T>Type guard to check if this instance is a reference.
Always true for OasRef instances
resolve
resolve(): ResolvedRef<T>Recursively resolves this reference to its final target component.
Follows the chain of references until it reaches a component that is not a reference. A schema reference returns the schema at this site: when the keywords next to it, or next to a reference on the chain (SiblingKeywords), change the target, a copy of the target with them applied. When they change nothing, the shared target itself. resolveOnce() applies no siblings.
The result is shared, so do not modify it: the shared target is the model every reference to it sees, and a copy is built once, kept on this reference, and returned on every call.
The resolved component
when a reference on the chain names nothing, or the chain loops (toLoop), so it never reaches a component. The parser keeps either one only when parse.danglingRefs lists its kind.
Error when a reference on the chain names a component of another kind (see resolveOnce)
toLoop
toLoop(): OasRef<T>[] | undefinedThe references that form the loop this reference's chain comes back to, starting with the first one it comes back to, or undefined when the chain reaches a component. A a $ref to B, and B to A, form a loop: from A the chain goes to B, then to A again, and never reaches a component. A reference that leads into a loop is not part of it.
what resolveOnce throws, at any step of the chain
resolveOnce
resolveOnce(): OasRef<T> | ResolvedRef<T>Resolves this reference one level. It reads a component from document.components.<bucket>, and any other target from the targets the parser kept by pointer (OasDocument.toRefTarget).
Either the resolved component or another reference in the chain
when the reference names nothing in the document (kept when skmtc.config.json parse.danglingRefs lists its kind), including a $ref with no name, such as #/components/schemas/
Error when it names a component of another kind, such as a requestBody ref to #/components/schemas/…
isSchemaRef
isSchemaRef(): this is OasRef<'schema'>Narrows this reference to a schema reference.
True when this reference points at a schema component.
traverse
traverse(path: SchemaPath): OasSchema | OasRef<'schema'>Navigate an SchemaPath starting from this reference, resolving it to descend through. See traverseSchema.
Available on every OasRef so .traverse() works on the common OasSchema | OasRef<'schema'> value (an object property, array items). Schema paths only describe schemas for now, so it throws for non-schema refs (response/parameter/…); isSchemaRef narrows this, keeping the delegation cast-free.
The schema at the path (may be an unresolved $ref).
Error when called on a non-schema ref.
hasSiblings
hasSiblings(): booleanWhether this reference adds anything at its site: a use-site flag (nullable, readOnly, writeOnly) or another keyword next to it (see RefFields). resolve then returns a copy of the target with them applied, not the shared target.
clone
clone(ref?: OasRef<'schema'>): OasRef<T>A copy of this reference with what ref adds at its site merged in: its flags, and its keywords over this reference's own (mergeSiblingKeywords). It points at the same target. Each schema class's clone does the same for a schema, so one step along a chain of references can carry a site's keywords (resolveSchemaRefOnce). This reference does not change.
hasValueSiblings
hasValueSiblings(): booleanWhether the keywords next to this reference change which values it accepts (minLength next to a string, properties next to an object), not only how it is described (description, deprecated, x-*). No shared model accepts the same values as such a site, so a generator applies these keywords where it renders the reference by name. See changesValues.
toSkippedSiblings
toSkippedSiblings(): string[]The keywords next to this reference that resolve leaves out, by their names in the document: ones the target's type does not take (minLength next to an array), a type that disagrees, and values that do not fit. The parser cannot tell them while the reference is lazy, so a generator logs them when it renders the site. See toSkippedSiblings.
isReadOnly
isReadOnly(): booleanWhether the value is readOnly at this site, as Forge reads it: on this reference ({ allOf: [{ $ref }], readOnly: true }), or on what it points to, through any chain of references. A readOnly: false does not undo another's true. false for a reference to anything but a schema. In a loop (toLoop), which reaches no schema, only the references count.
isWriteOnly
isWriteOnly(): booleanWhether the value is writeOnly at this site: on this reference, or on what it points to, as isReadOnly reads readOnly.
toJsonSchema
toJsonSchema(
args: ToJsonSchemaOptions
): OpenAPIV3.ReferenceObject | ResolvedRefJsonType<T>The reference as JSON Schema. With resolve, the schema at this site, with the keywords next to the reference applied. Without it, only { $ref }: the keywords next to the reference are not written yet (skmtc/skmtc#338).
toJSON
toJSON(): object{ $ref } only, as toJsonSchema without resolve (skmtc/skmtc#338).