Skip to content

Commit

Permalink
fix(@rjsf/core): Nested into items schema allOf blocks with multiple …
Browse files Browse the repository at this point in the history
…if/then/else statements fail to render correctly
  • Loading branch information
AlimovSV authored and nickgros committed Nov 4, 2022
1 parent 914f0a1 commit 2cb40f2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
34 changes: 2 additions & 32 deletions 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,
Expand Down Expand Up @@ -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<T, S>(
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;
Expand Down
52 changes: 45 additions & 7 deletions packages/utils/test/schema/retrieveSchemaTest.ts
Expand Up @@ -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",
},
},
Expand Down

0 comments on commit 2cb40f2

Please sign in to comment.