Skip to content

Commit

Permalink
fix: do not use discriminator when specific schema was referenced in …
Browse files Browse the repository at this point in the history
…oneOf or anyOf
  • Loading branch information
RomanHotsiy committed Sep 6, 2022
1 parent 8dc03eb commit 6b1c56b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
85 changes: 68 additions & 17 deletions src/services/__tests__/models/Schema.circular.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,23 +537,14 @@ describe('Models', () => {
);

expect(printSchema(schema, circularDetailsPrinter)).toMatchInlineSnapshot(`
pet: oneOf
Pet -> oneOf
cat ->
category:
name: <string>
status: <string>
friend: <object> !circular
petType*: <string>
huntingSkill: <string>
dog ->
category:
name: <string>
status: <string>
friend: <object> !circular
petType*: <string>
packSize: <integer>
`);
pet: oneOf
Pet ->
category:
name: <string>
status: <string>
friend: <object> !circular
petType*: <string>
`);
});

test('should detect and recursion with nested oneOf', () => {
Expand Down Expand Up @@ -595,5 +586,65 @@ describe('Models', () => {
Tag -> <object> !circular
`);
});

test('should not use discriminator for direct schemas refs in oneOf/anyOf', () => {
const spec = parseYaml(outdent`
openapi: 3.0.0
components:
schemas:
Parent:
type: object
discriminator:
propertyName: type
mapping:
foo: '#/components/schemas/Foo'
bar: '#/components/schemas/Bar'
baz: '#/components/schemas/Baz'
properties:
type:
type: string
Foo:
allOf:
- $ref: '#/components/schemas/Parent'
- type: object
Bar:
allOf:
- $ref: '#/components/schemas/Parent'
- type: object
Baz:
allOf:
- $ref: '#/components/schemas/Parent'
- type: object
properties:
nested:
anyOf:
- $ref: '#/components/schemas/Foo'
- $ref: '#/components/schemas/Bar'
`) as any;

parser = new OpenAPIParser(spec, undefined, opts);
const schema = new SchemaModel(
parser,
spec.components.schemas.Parent,
'#/components/schemas/Parent',
opts,
);

expect(printSchema(schema, circularDetailsPrinter)).toMatchInlineSnapshot(`
oneOf
foo ->
type: <string>
bar ->
type: <string>
baz ->
type: <string>
nested: oneOf
Foo ->
type: <string>
Bar ->
type: <string>
`);
});
});
});
1 change: 1 addition & 0 deletions src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export class SchemaModel {
...merged,
title,
allOf: [{ ...this.schema, oneOf: undefined, anyOf: undefined }],
discriminator: undefined, // if schemas are listed in oneOf/anyOf, they are not supposed to be discriminated
} as OpenAPISchema,
variant.$ref || this.pointer + '/oneOf/' + idx,
this.options,
Expand Down

0 comments on commit 6b1c56b

Please sign in to comment.