openapi-ts-json-schema
Version:
OpenAPI to JSON schema generator with TypeScript in mind
46 lines (45 loc) • 1.78 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceInlinedRefsWithStringPlaceholder = void 0;
const map_obj_1 = __importDefault(require("map-obj"));
const __1 = require("..");
const getId_1 = require("./getId");
/**
* Get any JSON schema node and:
* - Return id placeholder if the entity is an inlined ref schema objects (with SCHEMA_ID_SYMBOL prop)
* - Return provided node in all other cases
*/
function replaceInlinedSchemaWithPlaceholder(node) {
const id = (0, getId_1.getId)(node);
if (id === undefined) {
return node;
}
return (0, __1.idToPlaceholder)(id);
}
/**
* Iterate a JSON schema to replace inlined ref schema objects
* (marked with a SCHEMA_ID_SYMBOL property holding the original $ref value)
* with a string placeholder with a reference to their internal id ("_OTJS-START_/id/value_OTJS-END_")
*/
function replaceInlinedRefsWithStringPlaceholder(schema) {
if ((0, getId_1.getId)(schema)) {
return replaceInlinedSchemaWithPlaceholder(schema);
}
return (0, map_obj_1.default)(schema, (key, value) => {
/**
* @NOTE map-obj transforms only arrays entries which are objects
* @NOTE map-obj only processes object values separately
*/
if (Array.isArray(value)) {
return [
key,
value.map((entry) => replaceInlinedSchemaWithPlaceholder(entry)),
];
}
return [key, replaceInlinedSchemaWithPlaceholder(value)];
}, { deep: true });
}
exports.replaceInlinedRefsWithStringPlaceholder = replaceInlinedRefsWithStringPlaceholder;