Skip to content

Commit

Permalink
feat: support x-discriminator for OpenAPI 2
Browse files Browse the repository at this point in the history
related to #496
  • Loading branch information
RomanHotsiy committed May 29, 2018
1 parent 6ea2b7b commit aaff311
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class SchemaModel {
return;
}

if (!isChild && schema.discriminator !== undefined) {
if (!isChild && getDiscriminator(schema) !== undefined) {
this.initDiscriminator(schema, parser);
return;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -257,3 +258,7 @@ function buildFields(

return fields;
}

function getDiscriminator(schema: OpenAPISchema): OpenAPISchema['discriminator'] {
return schema.discriminator || schema['x-discriminator'];
}
4 changes: 1 addition & 3 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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>>;
2 changes: 1 addition & 1 deletion src/types/open-api.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Omit } from './';
import { Omit } from './index';

export interface OpenAPISpec {
openapi: string;
Expand Down

0 comments on commit aaff311

Please sign in to comment.