From b010b558f1257a44883874c54eaf75e045380945 Mon Sep 17 00:00:00 2001 From: Guillaume Tassery Date: Wed, 26 Oct 2022 21:14:28 +0200 Subject: [PATCH] fix(ui): handle missing schema reference case for discriminant definitions (#8257) Co-authored-by: Tim Lai --- src/core/plugins/samples/fn.js | 2 + test/unit/core/plugins/samples/fn.js | 59 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js index 04fad9f1..a140dc4e 100644 --- a/src/core/plugins/samples/fn.js +++ b/src/core/plugins/samples/fn.js @@ -350,6 +350,8 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und schema.discriminator && Object.prototype.hasOwnProperty.call(schema.discriminator, "mapping") && schema.discriminator.mapping && + Object.prototype.hasOwnProperty.call(schema, "$$ref") && + schema.$$ref && schema.discriminator.propertyName === propName) { for (let pair in schema.discriminator.mapping){ if (schema.$$ref.search(schema.discriminator.mapping[pair]) !== -1) { diff --git a/test/unit/core/plugins/samples/fn.js b/test/unit/core/plugins/samples/fn.js index 7bb04184..b87d59c2 100644 --- a/test/unit/core/plugins/samples/fn.js +++ b/test/unit/core/plugins/samples/fn.js @@ -671,6 +671,65 @@ describe("sampleFromSchema", () => { expect(sampleFromSchema(definition)).toEqual(expected) }) + + it("should not throw if expected $$ref is missing, and should fallback to default behavior", () => { + let definition = { + "type": "array", + "items": { + "oneOf": [ + { + "required": [ + "type" + ], + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "TYPE1", + "TYPE2" + ] + } + }, + "discriminator": { + "propertyName": "type", + "mapping": { + "TYPE1": "#/components/schemas/FirstDto", + "TYPE2": "#/components/schemas/SecondDto" + } + }, + }, + { + "required": [ + "type" + ], + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "TYPE1", + "TYPE2" + ] + } + }, + "discriminator": { + "propertyName": "type", + "mapping": { + "TYPE1": "#/components/schemas/FirstDto", + "TYPE2": "#/components/schemas/SecondDto" + } + }, + } + ] + } + } + + expect(() => { + sampleFromSchema(definition) + }).not.toThrow() + }) + }) it("should use overrideExample when defined", () => {