mirror of
https://github.com/Redocly/redoc.git
synced 2025-08-08 06:04:56 +03:00
feat: add support conditional operators
This commit is contained in:
parent
7243b376eb
commit
b108ff6d12
|
@ -246,14 +246,24 @@ export class OpenAPIParser {
|
||||||
subSchema.type !== undefined
|
subSchema.type !== undefined
|
||||||
) {
|
) {
|
||||||
console.warn(
|
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) {
|
if (subSchema.type !== undefined) {
|
||||||
|
if (Array.isArray(subSchema.type) && receiver.type)
|
||||||
|
receiver.type = receiver.type?.concat(...subSchema.type)
|
||||||
|
else
|
||||||
receiver.type = subSchema.type;
|
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) {
|
if (subSchema.properties !== undefined) {
|
||||||
receiver.properties = receiver.properties || {};
|
receiver.properties = receiver.properties || {};
|
||||||
for (const prop in subSchema.properties) {
|
for (const prop in subSchema.properties) {
|
||||||
|
|
|
@ -17,6 +17,7 @@ import {
|
||||||
pluralizeType,
|
pluralizeType,
|
||||||
sortByField,
|
sortByField,
|
||||||
sortByRequired,
|
sortByRequired,
|
||||||
|
mergeObjects,
|
||||||
} from '../../utils/';
|
} from '../../utils/';
|
||||||
|
|
||||||
import { l } from '../Labels';
|
import { l } from '../Labels';
|
||||||
|
@ -136,12 +137,16 @@ export class SchemaModel {
|
||||||
else this.type = [this.type, 'null'];
|
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) {
|
if (this.isCircular) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (schema.if || schema.then || schema.else) {
|
||||||
|
this.initConditionalOperators(schema, parser);
|
||||||
|
}
|
||||||
|
|
||||||
if (!isChild && getDiscriminator(schema) !== undefined) {
|
if (!isChild && getDiscriminator(schema) !== undefined) {
|
||||||
this.initDiscriminator(schema, parser);
|
this.initDiscriminator(schema, parser);
|
||||||
return;
|
return;
|
||||||
|
@ -346,6 +351,28 @@ export class SchemaModel {
|
||||||
return innerSchema;
|
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(
|
function buildFields(
|
||||||
|
|
4
src/types/open-api.d.ts
vendored
4
src/types/open-api.d.ts
vendored
|
@ -146,6 +146,10 @@ export interface OpenAPISchema {
|
||||||
minProperties?: number;
|
minProperties?: number;
|
||||||
enum?: any[];
|
enum?: any[];
|
||||||
example?: any;
|
example?: any;
|
||||||
|
|
||||||
|
if?: OpenAPISchema;
|
||||||
|
else?: OpenAPISchema;
|
||||||
|
then?: OpenAPISchema;
|
||||||
const?: string;
|
const?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user