feat: support x-discriminator for OpenAPI 2

related to #496
This commit is contained in:
Roman Hotsiy 2018-05-29 17:46:06 +03:00
parent 6ea2b7b0e6
commit aaff311a3e
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 10 additions and 7 deletions

View File

@ -111,7 +111,7 @@ export class SchemaModel {
return;
}
if (!isChild && schema.discriminator !== undefined) {
if (!isChild && getDiscriminator(schema) !== undefined) {
this.initDiscriminator(schema, parser);
return;
}
@ -171,7 +171,8 @@ export class SchemaModel {
},
parser: OpenAPIParser,
) {
this.discriminatorProp = schema.discriminator!.propertyName;
const discriminator = getDiscriminator(schema)!;
this.discriminatorProp = discriminator.propertyName;
const derived = parser.findDerived([...(schema.parentRefs || []), this._$ref]);
if (schema.oneOf) {
@ -184,7 +185,7 @@ export class SchemaModel {
}
}
const mapping = schema.discriminator!.mapping || {};
const mapping = discriminator.mapping || {};
for (const name in mapping) {
derived[mapping[name]] = name;
}
@ -257,3 +258,7 @@ function buildFields(
return fields;
}
function getDiscriminator(schema: OpenAPISchema): OpenAPISchema['discriminator'] {
return schema.discriminator || schema['x-discriminator'];
}

View File

@ -1,5 +1,3 @@
export * from './open-api';
export type Diff<T extends string, U extends string> = ({ [P in T]: P } &
{ [P in U]: never } & { [x: string]: never })[T];
export type Omit<T, K extends keyof T> = { [P in Diff<keyof T, K>]: T[P] };
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

View File

@ -1,4 +1,4 @@
import { Omit } from './';
import { Omit } from './index';
export interface OpenAPISpec {
openapi: string;