diff --git a/src/services/OpenAPIParser.ts b/src/services/OpenAPIParser.ts index b0d3a8f446..ca47fd41aa 100644 --- a/src/services/OpenAPIParser.ts +++ b/src/services/OpenAPIParser.ts @@ -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) { diff --git a/src/services/models/Schema.ts b/src/services/models/Schema.ts index 0dd75abc9f..81c4861ac2 100644 --- a/src/services/models/Schema.ts +++ b/src/services/models/Schema.ts @@ -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( diff --git a/src/types/open-api.d.ts b/src/types/open-api.d.ts index d55e44b13b..050e6faf67 100644 --- a/src/types/open-api.d.ts +++ b/src/types/open-api.d.ts @@ -146,6 +146,10 @@ export interface OpenAPISchema { minProperties?: number; enum?: any[]; example?: any; + + if?: OpenAPISchema; + else?: OpenAPISchema; + then?: OpenAPISchema; const?: string; }