From 2cb40f2a89433fe39a67b461671160e2a04f5adb Mon Sep 17 00:00:00 2001 From: Sergey Alimov Date: Thu, 18 Aug 2022 16:32:03 +0400 Subject: [PATCH] fix(@rjsf/core): Nested into items schema allOf blocks with multiple if/then/else statements fail to render correctly --- packages/utils/src/schema/retrieveSchema.ts | 34 +----------- .../utils/test/schema/retrieveSchemaTest.ts | 52 ++++++++++++++++--- 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/packages/utils/src/schema/retrieveSchema.ts b/packages/utils/src/schema/retrieveSchema.ts index d120a5ca11..91c5a1cf19 100644 --- a/packages/utils/src/schema/retrieveSchema.ts +++ b/packages/utils/src/schema/retrieveSchema.ts @@ -1,6 +1,6 @@ import get from "lodash/get"; import set from "lodash/set"; -import mergeAllOf from "json-schema-merge-allof"; +import mergeAllOf, { Options } from "json-schema-merge-allof"; import { ADDITIONAL_PROPERTIES_KEY, @@ -242,40 +242,10 @@ export default function retrieveSchema< } const formData: GenericObjectType = rawFormData || {}; - // For each level of the dependency, we need to recursively determine the appropriate resolved schema given the current state of formData. - // Otherwise, nested allOf subschemas will not be correctly displayed. - if (resolvedSchema.properties) { - const properties: GenericObjectType = {}; - - Object.entries(resolvedSchema.properties).forEach((entries) => { - const propName = entries[0]; - const propSchema = entries[1] as S; - const rawPropData = formData[propName]; - const propData = isObject(rawPropData) ? rawPropData : {}; - const resolvedPropSchema = retrieveSchema( - validator, - propSchema, - rootSchema, - propData - ); - - properties[propName] = resolvedPropSchema; - - if ( - propSchema !== resolvedPropSchema && - resolvedSchema.properties !== properties - ) { - resolvedSchema = { ...resolvedSchema, properties }; - } - }); - } if (ALL_OF_KEY in schema) { try { - resolvedSchema = mergeAllOf({ - ...resolvedSchema, - allOf: resolvedSchema.allOf, - }) as S; + resolvedSchema = mergeAllOf( resolvedSchema, { deep: false } as Options) as S; } catch (e) { console.warn("could not merge subschemas in allOf:\n" + e); const { allOf, ...resolvedSchemaWithoutAllOf } = resolvedSchema; diff --git a/packages/utils/test/schema/retrieveSchemaTest.ts b/packages/utils/test/schema/retrieveSchemaTest.ts index eacdfd74b1..adbccfa2fc 100644 --- a/packages/utils/test/schema/retrieveSchemaTest.ts +++ b/packages/utils/test/schema/retrieveSchemaTest.ts @@ -1205,14 +1205,52 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) { title: "Breed name", type: "string", }, - Spots: { - default: "small", - enum: ["large", "small"], - title: "Spots", - type: "string", - }, }, - required: ["BreedName", "Spots"], + allOf: [ + { + if: { + required: ["BreedName"], + properties: { + BreedName: { + const: "Alsatian", + }, + }, + }, + then: { + properties: { + Fur: { + default: "brown", + enum: ["black", "brown"], + title: "Fur", + type: "string", + }, + }, + required: ["Fur"], + }, + }, + { + if: { + required: ["BreedName"], + properties: { + BreedName: { + const: "Dalmation", + }, + }, + }, + then: { + properties: { + Spots: { + default: "small", + enum: ["large", "small"], + title: "Spots", + type: "string", + }, + }, + required: ["Spots"], + }, + }, + ], + required: ["BreedName"], title: "Breed", }, },