OpenAPI document
StackTrail
A position in a run, as a list of frames from the outermost to the innermost. While the parser reads a schema, they can be components, schemas and User. toString joins them: components:schemas:User.
append and remove change the trail. To keep a position, use clone. To add frames only while a function runs, use trace, which gives the function its own trail.
Signature
import { StackTrail } from '@skmtc/core'
class StackTrailConstructor
constructor
constructor(stack?: readonly string[])Properties
The frames of the trail as a new read-only array, built on each read. Change the trail with append, remove or trace, not through this array. The trail itself is live: trace removes its frame after fn returns, so read the frames inside fn to keep them.
Methods
StackTrail.empty
StackTrail.empty(): StackTrailThe canonical empty trail - used for nodes that were synthesized programmatically rather than parsed from a source document (they have no position). Distinct from any parsed position: no parsed OasBase node ever sits at the empty trail.
isEmpty
isEmpty(): booleanTrue when the trail holds no frames (a synthetic / positionless node).
clone
clone(): StackTrailCreates an independent trail with the same frames. The frames are shared, not copied.
A new StackTrail instance with the same frames
append
append(frame: string | string[]): StackTrailAppends frame(s) to the end of the trail.
Single frame string or array of frames to append
This StackTrail instance for chaining
When frame is not a string or string array
trace
trace<T>(key: string | string[], fn: (st: StackTrail) => T): TRuns fn on a copy of the trail with key appended: one frame for a string, one frame per element for an array.
When fn returns, key is removed and must be the last frame. When fn throws, the copy goes back to the frames it started with and the error from fn is rethrown: a frame check there would replace it.
traceAsync
traceAsync<T>(
key: string | string[],
fn: (st: StackTrail) => Promise<T>
): Promise<T>Async form of trace.
toJsonPointer
toJsonPointer(): JsonPointerConverts the trail to an RFC 6901 JSON Pointer (URI fragment form).
Unlike toStackRef, this works for any visitor path - not just components-rooted references. Used by the gen-maps system to attach a location field to every parsed OAS schema, recording exactly where in the document the schema lives.
Special characters in stack frames (/, ~) are escaped per RFC 6901.
A JSON Pointer string starting with #/
toSchemaPointer
toSchemaPointer(): JsonPointerThe document-relative JSON Pointer for a parsed node, with the run's operational prefix stripped - the resolvable form used as a schema pointer in gen-maps.
A parsed node's trail is seeded with operational frames before any document traversal: the worker pushes [traceId, spanId] and CoreContext.toArtifacts wraps each phase in .trace('parse', …) / .trace('generate', …) etc. The document path (components/schemas/Pet/properties/name) is appended on top. Unlike toJsonPointer - which serializes the whole trail and so does NOT resolve against the input document - this drops everything up to and including the phase frame, leaving only the document-relative path.
The remaining frames are the input document's own keys (components, paths, …), never an oas: scheme.
Falls back to the full trail when no phase frame is present (a trail seeded empty - e.g. in tests - is already document-relative). Returns #/ (document root) for the empty trail.
toStackRef
toStackRef(): string | undefinedremove
remove(frame: string | string[]): StackTrailRemoves frame(s) from the end of the trail.
Single frame string or array of frames to remove
This StackTrail instance for chaining
When trying to remove a frame that doesn't match the last frame
When frame is not a string or string array
toJSON
toJSON(): stringConverts the trail to JSON representation.
Used for serialization in JSON contexts. Returns the same as toString() method.
String representation of the trail
toString
toString(): stringConverts the trail to a colon-separated string.
Inside a frame, % is escaped as %25 and : as %3A, so StackTrail.fromString reads the same frames back.
StackTrail.fromString
StackTrail.fromString(value: string): StackTrailReads a string written by StackTrail.toString back into a trail.
Splits on : and decodes %25 and %3A in each frame. The empty string gives the empty trail, so a trail of one empty frame is the one trail that does not round-trip.