Skip to content

Commit f21f043

Browse files
authored
Fixed issue with default value not being prefilled when object with if/then is nested inside another object (#4889)
1 parent bb64e56 commit f21f043

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ should change the heading of the (upcoming) version to include a major version b
2020

2121
## @rjsf/utils
2222

23+
- Fixed issue with default value not being prefilled when object with if/then is nested inside another object, fixing [#4222](https://github.com/rjsf-team/react-jsonschema-form/issues/4222)
2324
- Fixed issue with schema array with nested dependent fixed-length, fixing [#3754](https://github.com/rjsf-team/react-jsonschema-form/issues/3754)
2425

2526

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
CONST_KEY,
99
DEFAULT_KEY,
1010
DEPENDENCIES_KEY,
11+
IF_KEY,
1112
ONE_OF_KEY,
1213
PROPERTIES_KEY,
1314
REF_KEY,
@@ -478,12 +479,16 @@ export function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSche
478479
{
479480
const formData: T = (isObject(rawFormData) ? rawFormData : {}) as T;
480481
const schema: S = rawSchema;
481-
// This is a custom addition that fixes this issue:
482-
// https://github.com/rjsf-team/react-jsonschema-form/issues/3832
483-
const retrievedSchema =
484-
experimental_defaultFormStateBehavior?.allOf === 'populateDefaults' && ALL_OF_KEY in schema
485-
? retrieveSchema<T, S, F>(validator, schema, rootSchema, formData, experimental_customMergeAllOf)
486-
: schema;
482+
// Retrieve the schema:
483+
// - If schema contains `allOf` AND `experimental_defaultFormStateBehavior.allOf` is set to `populateDefaults`
484+
// - OR if schema contains an 'if' AND `emptyObjectFields` is not set to `skipEmptyDefaults`
485+
// This ensures we compute defaults correctly for schemas with these keywords.
486+
const shouldRetrieveSchema =
487+
(experimental_defaultFormStateBehavior?.allOf === 'populateDefaults' && ALL_OF_KEY in schema) ||
488+
(experimental_defaultFormStateBehavior?.emptyObjectFields !== 'skipEmptyDefaults' && IF_KEY in schema);
489+
const retrievedSchema = shouldRetrieveSchema
490+
? retrieveSchema<T, S, F>(validator, schema, rootSchema, formData, experimental_customMergeAllOf)
491+
: schema;
487492
const parentConst = retrievedSchema[CONST_KEY];
488493
const objectDefaults = Object.keys(retrievedSchema.properties || {}).reduce(
489494
(acc: GenericObjectType, key: string) => {

packages/utils/test/schema/getDefaultFormStateTest.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,83 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
16411641
).toEqual(expected);
16421642
});
16431643
});
1644+
1645+
describe('an nested object with if/then condition', () => {
1646+
const schema: RJSFSchema = {
1647+
type: 'object',
1648+
properties: {
1649+
wrappingObject: {
1650+
type: 'object',
1651+
properties: {
1652+
checkbox: {
1653+
type: 'boolean',
1654+
},
1655+
},
1656+
if: {
1657+
properties: {
1658+
checkbox: {
1659+
const: true,
1660+
},
1661+
},
1662+
required: ['checkbox'],
1663+
},
1664+
then: {
1665+
properties: {
1666+
foo: {
1667+
type: 'string',
1668+
default: 'foo value',
1669+
},
1670+
},
1671+
required: ['foo'],
1672+
},
1673+
},
1674+
},
1675+
};
1676+
const rawFormData = {
1677+
wrappingObject: {
1678+
checkbox: true,
1679+
},
1680+
};
1681+
const expected = {
1682+
wrappingObject: {
1683+
checkbox: true,
1684+
foo: 'foo value',
1685+
},
1686+
};
1687+
test('getDefaultFormState', () => {
1688+
expect(getDefaultFormState(testValidator, schema, rawFormData, schema)).toEqual(expected);
1689+
});
1690+
1691+
test('computeDefaults', () => {
1692+
expect(
1693+
computeDefaults(testValidator, schema, {
1694+
rootSchema: schema,
1695+
rawFormData,
1696+
shouldMergeDefaultsIntoFormData: true,
1697+
}),
1698+
).toEqual(expected);
1699+
});
1700+
1701+
test('getDefaultBasedOnSchemaType', () => {
1702+
expect(
1703+
getDefaultBasedOnSchemaType(testValidator, schema, {
1704+
rootSchema: schema,
1705+
rawFormData,
1706+
shouldMergeDefaultsIntoFormData: true,
1707+
}),
1708+
).toEqual(expected);
1709+
});
1710+
1711+
test('getObjectDefaults', () => {
1712+
expect(
1713+
getObjectDefaults(testValidator, schema, {
1714+
rootSchema: schema,
1715+
rawFormData,
1716+
shouldMergeDefaultsIntoFormData: true,
1717+
}),
1718+
).toEqual(expected);
1719+
});
1720+
});
16441721
});
16451722

16461723
describe('array schemas', () => {

0 commit comments

Comments
 (0)