openapi-ts-request
Version:
Swagger2/OpenAPI3/Apifox to TypeScript/JavaScript, request client(support any client), request mock service, enum and enum translation, react-query/vue-query, type field label, JSON Schemas
51 lines (50 loc) • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.patchSchema = patchSchema;
const lodash_1 = require("lodash");
const util_1 = require("./util");
function patchSchema(schema, schemas) {
if ((0, lodash_1.isEmpty)(schema)) {
return {};
}
if ((0, lodash_1.has)(schema, 'allOf')) {
return (0, lodash_1.reduce)(schema.allOf, (last, s) => {
const f = Object.assign({}, (last || {}));
const next = patchSchema(s, schemas);
(0, lodash_1.forEach)(next, (v, k) => {
switch (k) {
case 'properties':
f[k] = Object.assign(Object.assign({}, f[k]), (v || {}));
break;
case 'required':
f[k] = (0, lodash_1.uniq)((f[k] || []).concat(v));
break;
default: {
f[k] = v;
}
}
});
return f;
}, (0, lodash_1.omit)(schema, 'allOf'));
}
if ((0, util_1.isReferenceObject)(schema) &&
(0, lodash_1.startsWith)(schema.$ref, '#/components/schemas/')) {
const refId = (0, lodash_1.replace)(schema.$ref, '#/components/schemas/', '') ||
schema['x-id'];
if (schemas[refId]) {
return Object.assign(Object.assign(Object.assign({}, schema), patchSchema(schemas[refId], schemas)), { 'x-id': refId, $ref: undefined });
}
}
if ((0, util_1.isNonArraySchemaObject)(schema)) {
const patchedProperties = (0, lodash_1.mapValues)(schema.properties, (propSchema) => patchSchema(propSchema, schemas));
if ((0, lodash_1.isObject)(schema.additionalProperties)) {
const additionalProperties = patchSchema(schema.additionalProperties, schemas);
return Object.assign(Object.assign({}, schema), { additionalProperties });
}
return Object.assign(Object.assign({}, schema), { properties: patchedProperties });
}
if ((0, util_1.isArraySchemaObject)(schema)) {
return Object.assign(Object.assign({}, schema), { items: patchSchema(schema.items, schemas) });
}
return schema;
}