openapi-ts-json-schema
Version:
Generate TypeScript-first JSON schemas from OpenAPI definitions
39 lines (38 loc) • 1.39 kB
JavaScript
import mapObject from 'map-obj';
import { idToPlaceholder } from '../index.js';
import { getId } from './getId.js';
/**
* 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 = getId(node);
if (id === undefined) {
return node;
}
return idToPlaceholder(id);
}
/**
* Iterate any object (in reality 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_")
*/
export function replaceInlinedRefsWithStringPlaceholder(schema) {
if (getId(schema)) {
return replaceInlinedSchemaWithPlaceholder(schema);
}
return mapObject(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 });
}