OpenAPI document
collateExamples
Recursively collates and builds example values from OpenAPI schemas.
This function traverses OpenAPI schema structures and generates comprehensive example values based on the schema definitions and any explicit examples provided. It handles complex nested structures including objects, arrays, unions, and references. The part of a schema past the depth limit of 15 is left out, and the rest of the example is kept.
The function prioritizes explicit examples when available and falls back to generating examples from nested schemas. It's particularly useful for creating realistic test data, API documentation examples, and mock responses.
Signature
import { collateExamples } from '@skmtc/core'
collateExamples(args: CollatedExampleArgs): unknownParameters
Configuration for example collation
The OpenAPI schema or reference to extract examples from
Current recursion depth. A schema deeper than 15 has no example.
The $ref pointers already entered on THIS path, used to cut reference cycles. Callers start collation without it; the function threads it.
Path-scoped rather than global on purpose. A global set would also stop a cycle, and would additionally drop the example for the SECOND of two sibling uses of the same schema - silently emptying samples on every document that reuses a type, which is most of them.
import { collateExamples } from '@skmtc/core';
// String schema with example
const stringSchema = new OasString({ example: 'john.doe@example.com' });
const example = collateExamples({ objectSchema: stringSchema, depth: 0 });
console.log(example); // 'john.doe@example.com'
// Number schema with example
const numberSchema = new OasNumber({ example: 42 });
const numExample = collateExamples({ objectSchema: numberSchema, depth: 0 });
console.log(numExample); // 42// Object schema with nested properties
const userSchema = new OasObject({
properties: {
id: new OasInteger({ example: 123 }),
name: new OasString({ example: 'John Doe' }),
email: new OasString({ example: 'john@example.com' }),
age: new OasInteger({ example: 30 })
}
});
const userExample = collateExamples({ objectSchema: userSchema, depth: 0 });
console.log(userExample);
// {
// id: 123,
// name: 'John Doe',
// email: 'john@example.com',
// age: 30
// }// Array of objects
const usersArraySchema = new OasArray({
items: new OasObject({
properties: {
id: new OasInteger({ example: 1 }),
name: new OasString({ example: 'Jane Smith' })
}
})
});
const arrayExample = collateExamples({ objectSchema: usersArraySchema, depth: 0 });
console.log(arrayExample);
// [
// {
// id: 1,
// name: 'Jane Smith'
// }
// ]// Union of string and number
const unionSchema = new OasUnion({
members: [
new OasString({ example: 'text-value' }),
new OasNumber({ example: 99 })
]
});
const unionExample = collateExamples({ objectSchema: unionSchema, depth: 0 });
console.log(unionExample); // 'text-value' (first member with example)// Schema with reference to another schema
const addressRef = new OasRef({
$ref: '#/components/schemas/Address',
resolve: () => new OasObject({
properties: {
street: new OasString({ example: '123 Main St' }),
city: new OasString({ example: 'Springfield' })
}
})
});
const refExample = collateExamples({ objectSchema: addressRef, depth: 0 });
console.log(refExample);
// {
// street: '123 Main St',
// city: 'Springfield'
// }// `id` is at depth 15 and `name` is at depth 16, past the limit
const deepSchema = new OasObject({
properties: {
id: new OasInteger({ example: 123 }),
owner: new OasObject({ properties: { name: new OasString({ example: 'Jane' }) } })
}
});
const deepExample = collateExamples({ objectSchema: deepSchema, depth: 14 });
console.log(deepExample); // { id: 123 }
// A missing schema has no example
const emptyExample = collateExamples({ objectSchema: undefined, depth: 0 });
console.log(emptyExample); // undefinedclass ApiDocGenerator {
generateExampleResponse(responseSchema: OasSchema) {
const example = collateExamples({ objectSchema: responseSchema, depth: 0 });
if (example) {
return {
description: 'Example response',
value: example
};
}
return { description: 'No example available' };
}
}Returns
Collated example value matching the schema structure, or undefined if no examples