From 58a4a2e12cefe73fc87a889a907f07446556b10d Mon Sep 17 00:00:00 2001 From: Brad Sullivan Date: Fri, 22 Nov 2024 13:30:23 +1000 Subject: [PATCH] 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 {