Skip to content

Commit

Permalink
fix: discriminator fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Feb 7, 2018
1 parent ee822f6 commit ff3bb24
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
67 changes: 67 additions & 0 deletions src/services/__tests__/fixtures/discriminator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"openapi": "3.0.0",
"servers": [],
"info": {
"title": "Broken Redoc Discriminator",
"version": ""
},
"paths": {
"/foo": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/FooTopLevel"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"JsonApiResource": {
"type": "object",
"description": "A related resource.",
"required": [
"type"
],
"discriminator": {
"propertyName": "type"
},
"properties": {
"type": {
"type": "string",
"description": "The type of object this resource represents."
}
}
},
"FooTopLevel": {
"type": "object",
"required": [
"data"
],
"properties": {
"data": {
"$ref": "#/components/schemas/Foo"
}
}
},
"Foo": {
"allOf": [
{
"type": "object"
},
{
"$ref": "#/components/schemas/JsonApiResource"
}
]
}
}
}
}
19 changes: 19 additions & 0 deletions src/services/__tests__/models/Schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { SchemaModel } from '../../models/Schema';
import { OpenAPIParser } from '../../OpenAPIParser';
import { RedocNormalizedOptions } from '../../RedocNormalizedOptions';

const opts = new RedocNormalizedOptions({});

describe('Models', () => {
describe('Schema', () => {
let parser;

test('discriminator with one field', () => {
const spec = require('../fixtures/discriminator.json');
parser = new OpenAPIParser(spec, undefined, opts);
const schema = new SchemaModel(parser, spec.components.schemas.Foo, '', opts);
expect(schema.oneOf).toHaveLength(1);
expect(schema.discriminatorProp).toEqual('type');
});
});
});
4 changes: 2 additions & 2 deletions src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ export class SchemaModel {

private initDiscriminator(
schema: OpenAPISchema & {
namedParents?: string[];
parentRefs?: string[];
},
parser: OpenAPIParser,
) {
this.discriminatorProp = schema.discriminator!.propertyName;
const derived = parser.findDerived([...(schema.namedParents || []), this._$ref]);
const derived = parser.findDerived([...(schema.parentRefs || []), this._$ref]);

if (schema.oneOf) {
for (const variant of schema.oneOf) {
Expand Down

0 comments on commit ff3bb24

Please sign in to comment.