mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-22 16:46:34 +03:00
fix: discriminator fix
This commit is contained in:
parent
ee822f6ebe
commit
ff3bb2461a
67
src/services/__tests__/fixtures/discriminator.json
Normal file
67
src/services/__tests__/fixtures/discriminator.json
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
19
src/services/__tests__/models/Schema.test.ts
Normal file
19
src/services/__tests__/models/Schema.test.ts
Normal 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');
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user