From 58a4a2e12cefe73fc87a889a907f07446556b10d Mon Sep 17 00:00:00 2001 From: Brad Sullivan Date: Fri, 22 Nov 2024 13:30:23 +1000 Subject: [PATCH 1/2] Correctly match oneOf mappings when generating example values for oneOf's with discriminators --- .../plugins/json-schema-5-samples/fn/index.js | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/core/plugins/json-schema-5-samples/fn/index.js b/src/core/plugins/json-schema-5-samples/fn/index.js index e1fcf8623fe..3a51628a8ca 100644 --- a/src/core/plugins/json-schema-5-samples/fn/index.js +++ b/src/core/plugins/json-schema-5-samples/fn/index.js @@ -351,13 +351,24 @@ 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 && + Object.prototype.hasOwnProperty.call(schema, "oneOf") && + schema.oneOf && schema.discriminator.propertyName === propName) { - for (let pair in schema.discriminator.mapping){ - if (schema.$$ref.search(schema.discriminator.mapping[pair]) !== -1) { - res[propName] = pair - break + + for (let option of schema.oneOf) { + if (Object.prototype.hasOwnProperty.call(option, "$$ref")) { + let found = false + for (let pair in schema.discriminator.mapping){ + if (option.$$ref.search(schema.discriminator.mapping[pair]) !== -1) { + res[propName] = pair + found = true + break + } + } + + if (found) { + break + } } } } else { From bd4af3e2dd3f1820a3d7c3911c5eaecf149bba10 Mon Sep 17 00:00:00 2001 From: "Brad S." Date: Fri, 22 Nov 2024 13:39:50 +1000 Subject: [PATCH 2/2] Added missing property check --- src/core/plugins/json-schema-5-samples/fn/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/plugins/json-schema-5-samples/fn/index.js b/src/core/plugins/json-schema-5-samples/fn/index.js index 3a51628a8ca..f4fa033427f 100644 --- a/src/core/plugins/json-schema-5-samples/fn/index.js +++ b/src/core/plugins/json-schema-5-samples/fn/index.js @@ -356,7 +356,7 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und schema.discriminator.propertyName === propName) { for (let option of schema.oneOf) { - if (Object.prototype.hasOwnProperty.call(option, "$$ref")) { + if (Object.prototype.hasOwnProperty.call(option, "$$ref") && option.$$ref) { let found = false for (let pair in schema.discriminator.mapping){ if (option.$$ref.search(schema.discriminator.mapping[pair]) !== -1) {