fix: discriminator fix

This commit is contained in:
Roman Hotsiy 2018-02-08 01:10:15 +02:00
parent ee822f6ebe
commit ff3bb2461a
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 88 additions and 2 deletions

View File

@ -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"
}
]
}
}
}
}

View File

@ -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');
});
});
});

View File

@ -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) {