feat: add support conditional operators

This commit is contained in:
Alex Varchuk 2021-05-26 13:00:09 +03:00
parent 7243b376eb
commit b108ff6d12
3 changed files with 44 additions and 3 deletions

View File

@ -246,12 +246,22 @@ export class OpenAPIParser {
subSchema.type !== undefined
) {
console.warn(
`Incompatible types in allOf at "${$ref}": "${receiver.type}" and "${subSchema.type}"`,
`Incompatible types in allOf at "${$ref}": "${receiver.type}" and "${subSchema.type}"`, //check maybe need delete
);
}
if (subSchema.type !== undefined) {
receiver.type = subSchema.type;
if (Array.isArray(subSchema.type) && receiver.type)
receiver.type = receiver.type?.concat(...subSchema.type)
else
receiver.type = subSchema.type;
}
if (subSchema.enum !== undefined) {
if (Array.isArray(subSchema.enum) && receiver.enum)
receiver.enum = receiver.enum?.concat(...subSchema.enum)
else
receiver.enum = subSchema.enum;
}
if (subSchema.properties !== undefined) {

View File

@ -17,6 +17,7 @@ import {
pluralizeType,
sortByField,
sortByRequired,
mergeObjects,
} from '../../utils/';
import { l } from '../Labels';
@ -136,12 +137,16 @@ export class SchemaModel {
else this.type = [this.type, 'null'];
}
this.displayType = Array.isArray(this.type) ? this.type.join(' or ') : this.type;
this.displayType = Array.isArray(this.type) ? this.type.join(' or ') : this.type; // null problem here
if (this.isCircular) {
return;
}
if (schema.if || schema.then || schema.else) {
this.initConditionalOperators(schema, parser);
}
if (!isChild && getDiscriminator(schema) !== undefined) {
this.initDiscriminator(schema, parser);
return;
@ -346,6 +351,28 @@ export class SchemaModel {
return innerSchema;
});
}
private initConditionalOperators(schema: OpenAPISchema, parser: OpenAPIParser) {
const { if: ifOperator, else: elseOperator, then: thenOperator, ...clearSchema} = schema;
if ((!ifOperator && !thenOperator) || (!ifOperator && !elseOperator)) return;
const groupedOperators = [mergeObjects({}, clearSchema, { allOf: [ifOperator, thenOperator] }), mergeObjects({}, clearSchema, elseOperator)]
this.oneOf = groupedOperators.map((variant, idx) => {
const merged = parser.mergeAllOf(parser.deref(variant || {}), this.pointer + '/oneOf/' + idx);
const title = merged.title || this.title;
const result = new SchemaModel(
parser,
{
...merged,
title,
} as OpenAPISchema,
this.pointer + '/oneOf/' + idx,
this.options,
);
return result;
})
}
}
function buildFields(

View File

@ -146,6 +146,10 @@ export interface OpenAPISchema {
minProperties?: number;
enum?: any[];
example?: any;
if?: OpenAPISchema;
else?: OpenAPISchema;
then?: OpenAPISchema;
const?: string;
}