openapi-modifier
Version:
This package allows you to automate the process of modifying OpenAPI specifications by applying a set of predefined rules
162 lines (161 loc) • 9.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.forEachSchema = void 0;
const openapi_models_1 = require("../../openapi-models");
const refs_1 = require("../refs");
const array_schema_1 = require("../array-schema");
// TODO iterator creator
const forEachSchema = (openAPIFile, callback) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
const stack = [];
// forEach - components.schemas[name]
Object.keys(((_b = (_a = openAPIFile.document) === null || _a === void 0 ? void 0 : _a.components) === null || _b === void 0 ? void 0 : _b.schemas) || {}).forEach((name) => {
var _a, _b, _c;
const schema = (_c = (_b = (_a = openAPIFile.document) === null || _a === void 0 ? void 0 : _a.components) === null || _b === void 0 ? void 0 : _b.schemas) === null || _c === void 0 ? void 0 : _c[name];
stack.push(schema);
});
// forEach - components.parameters[name].schema
Object.keys(((_d = (_c = openAPIFile.document) === null || _c === void 0 ? void 0 : _c.components) === null || _d === void 0 ? void 0 : _d.parameters) || {}).forEach((name) => {
var _a, _b, _c;
const parameterSchema = (_c = (_b = (_a = openAPIFile.document) === null || _a === void 0 ? void 0 : _a.components) === null || _b === void 0 ? void 0 : _b.parameters) === null || _c === void 0 ? void 0 : _c[name];
if ((0, refs_1.checkIsRefSchema)(parameterSchema)) {
stack.push(parameterSchema);
}
else {
stack.push(parameterSchema === null || parameterSchema === void 0 ? void 0 : parameterSchema.schema);
}
});
// forEach - components.requestBodies[name].content.[contentType].schema
Object.keys(((_f = (_e = openAPIFile.document) === null || _e === void 0 ? void 0 : _e.components) === null || _f === void 0 ? void 0 : _f.requestBodies) || {}).forEach((name) => {
var _a, _b, _c;
const requestBodySchema = (_c = (_b = (_a = openAPIFile.document) === null || _a === void 0 ? void 0 : _a.components) === null || _b === void 0 ? void 0 : _b.requestBodies) === null || _c === void 0 ? void 0 : _c[name];
if ((0, refs_1.checkIsRefSchema)(requestBodySchema)) {
stack.push(requestBodySchema);
}
else {
Object.keys((requestBodySchema === null || requestBodySchema === void 0 ? void 0 : requestBodySchema.content) || {}).forEach((contentType) => {
var _a, _b;
const responseContentSchema = (_b = (_a = requestBodySchema === null || requestBodySchema === void 0 ? void 0 : requestBodySchema.content) === null || _a === void 0 ? void 0 : _a[contentType]) === null || _b === void 0 ? void 0 : _b.schema;
stack.push(responseContentSchema);
});
}
});
// forEach - components.responses[code].content.[contentType].schema
Object.keys(((_h = (_g = openAPIFile.document) === null || _g === void 0 ? void 0 : _g.components) === null || _h === void 0 ? void 0 : _h.responses) || {}).forEach((code) => {
var _a, _b, _c;
const responsesCodeSchema = (_c = (_b = (_a = openAPIFile.document) === null || _a === void 0 ? void 0 : _a.components) === null || _b === void 0 ? void 0 : _b.responses) === null || _c === void 0 ? void 0 : _c[code];
if ((0, refs_1.checkIsRefSchema)(responsesCodeSchema)) {
stack.push(responsesCodeSchema);
}
else {
Object.keys((responsesCodeSchema === null || responsesCodeSchema === void 0 ? void 0 : responsesCodeSchema.content) || {}).forEach((contentType) => {
var _a, _b;
const responseContentSchema = (_b = (_a = responsesCodeSchema === null || responsesCodeSchema === void 0 ? void 0 : responsesCodeSchema.content) === null || _a === void 0 ? void 0 : _a[contentType]) === null || _b === void 0 ? void 0 : _b.schema;
stack.push(responseContentSchema);
});
}
});
Object.keys(((_j = openAPIFile.document) === null || _j === void 0 ? void 0 : _j.paths) || {}).forEach((pathName) => {
var _a, _b;
const pathObjSchema = (_b = (_a = openAPIFile.document) === null || _a === void 0 ? void 0 : _a.paths) === null || _b === void 0 ? void 0 : _b[pathName];
// forEach - paths[name].parameters[].schema
((pathObjSchema === null || pathObjSchema === void 0 ? void 0 : pathObjSchema.parameters) || []).forEach((parameter) => {
if ((0, refs_1.checkIsRefSchema)(parameter)) {
stack.push(parameter);
}
else {
stack.push(parameter === null || parameter === void 0 ? void 0 : parameter.schema);
}
});
const methods = Object.keys(pathObjSchema || {}).filter(openapi_models_1.checkIsHttpMethod);
methods.forEach((method) => {
var _a, _b, _c;
const methodSchema = (_c = (_b = (_a = openAPIFile.document) === null || _a === void 0 ? void 0 : _a.paths) === null || _b === void 0 ? void 0 : _b[pathName]) === null || _c === void 0 ? void 0 : _c[method];
// forEach - paths[name][method].parameters[].schema
const parameters = (methodSchema === null || methodSchema === void 0 ? void 0 : methodSchema.parameters) || [];
parameters.forEach((parameter) => {
if ((0, refs_1.checkIsRefSchema)(parameter)) {
stack.push(parameter);
}
else {
stack.push(parameter === null || parameter === void 0 ? void 0 : parameter.schema);
}
});
// forEach - paths[name][method].responses[code].content[contentType].schema
const responses = (methodSchema === null || methodSchema === void 0 ? void 0 : methodSchema.responses) || {};
Object.keys(responses).forEach((code) => {
const responseSchema = responses[code];
if ((0, refs_1.checkIsRefSchema)(responseSchema)) {
stack.push(responseSchema);
}
else {
Object.keys((responseSchema === null || responseSchema === void 0 ? void 0 : responseSchema.content) || {}).forEach((contentType) => {
var _a, _b;
const responseContentSchema = (_b = (_a = responseSchema === null || responseSchema === void 0 ? void 0 : responseSchema.content) === null || _a === void 0 ? void 0 : _a[contentType]) === null || _b === void 0 ? void 0 : _b.schema;
stack.push(responseContentSchema);
});
}
});
// forEach - paths[name][method].requestBody.content[contentType].schema
const requestBody = methodSchema === null || methodSchema === void 0 ? void 0 : methodSchema.requestBody;
if ((0, refs_1.checkIsRefSchema)(requestBody)) {
stack.push(requestBody);
}
else {
Object.keys((requestBody === null || requestBody === void 0 ? void 0 : requestBody.content) || {}).forEach((contentType) => {
var _a, _b;
const requestBodyContentSchema = (_b = (_a = requestBody === null || requestBody === void 0 ? void 0 : requestBody.content) === null || _a === void 0 ? void 0 : _a[contentType]) === null || _b === void 0 ? void 0 : _b.schema;
stack.push(requestBodyContentSchema);
});
}
});
});
while (stack.length) {
const item = stack.pop();
if (item) {
callback(item);
}
if (!(0, refs_1.checkIsRefSchema)(item) && item) {
if ((0, array_schema_1.checkIsArraySchema)(item)) {
stack.push(item.items);
}
if (item.type === 'object' && item.properties) {
Object.keys(item.properties).forEach((propertyKey) => {
var _a;
const property = (_a = item.properties) === null || _a === void 0 ? void 0 : _a[propertyKey];
stack.push(property);
});
}
if (item.type === 'object' && item.additionalProperties) {
if ((0, refs_1.checkIsRefSchema)(item.additionalProperties)) {
stack.push(item.additionalProperties);
}
if ((0, array_schema_1.checkIsArraySchema)(item.additionalProperties)) {
stack.push(item.additionalProperties);
}
// TODO check need?
// if (checkIsObjectSchema(item.additionalProperties)) {
// stack.push(item.additionalProperties);
// }
}
(_k = item.oneOf) === null || _k === void 0 ? void 0 : _k.forEach((schema) => {
stack.push(schema);
});
(_l = item.allOf) === null || _l === void 0 ? void 0 : _l.forEach((schema) => {
stack.push(schema);
});
(_m = item.anyOf) === null || _m === void 0 ? void 0 : _m.forEach((schema) => {
stack.push(schema);
});
const discriminatorMapping = ((_o = item.discriminator) === null || _o === void 0 ? void 0 : _o.mapping) || null;
if (discriminatorMapping) {
Object.values(discriminatorMapping).forEach(ref => {
stack.push({
'$ref': ref
});
});
}
}
}
};
exports.forEachSchema = forEachSchema;