Skip to content

Commit

Permalink
feat: add support conditional operators
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk committed May 31, 2021
1 parent 7243b37 commit b108ff6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 28 additions & 1 deletion src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
pluralizeType,
sortByField,
sortByRequired,
mergeObjects,
} from '../../utils/';

import { l } from '../Labels';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions src/types/open-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ export interface OpenAPISchema {
minProperties?: number;
enum?: any[];
example?: any;

if?: OpenAPISchema;
else?: OpenAPISchema;
then?: OpenAPISchema;
const?: string;
}

Expand Down

0 comments on commit b108ff6

Please sign in to comment.