UNPKG

openapi-ts-json-schema

Version:

OpenAPI to JSON schema generator with TypeScript in mind

39 lines (38 loc) 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeCircularRefReplacer = void 0; const getId_1 = require("./getId"); /** * JSON.stringify replacer * Replace circular references with {} * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#circular_references */ function makeCircularRefReplacer() { const ancestors = []; return function (key, value) { if (typeof value !== 'object' || value === null) { return value; } // `this` is the object that value is contained in, // i.e., its direct parent. while (ancestors.length > 0 && ancestors.at(-1) !== this) { ancestors.pop(); } // @NOTE Should we make recursion depth configurable? if (ancestors.includes(value)) { const id = (0, getId_1.getId)(value); return { // Drop an inline comment about recursion interruption [Symbol.for('before')]: [ { type: 'LineComment', value: ` Circular recursion interrupted. Schema id: "${id}"`, }, ], }; } ancestors.push(value); return value; }; } exports.makeCircularRefReplacer = makeCircularRefReplacer;