Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ should change the heading of the (upcoming) version to include a major version b

## @rjsf/utils

- 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)
- Fixed issue with schema array with nested dependent fixed-length, fixing [#3754](https://github.com/rjsf-team/react-jsonschema-form/issues/3754)


Expand Down
17 changes: 11 additions & 6 deletions packages/utils/src/schema/getDefaultFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CONST_KEY,
DEFAULT_KEY,
DEPENDENCIES_KEY,
IF_KEY,
ONE_OF_KEY,
PROPERTIES_KEY,
REF_KEY,
Expand Down Expand Up @@ -478,12 +479,16 @@ export function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSche
{
const formData: T = (isObject(rawFormData) ? rawFormData : {}) as T;
const schema: S = rawSchema;
// This is a custom addition that fixes this issue:
// https://github.com/rjsf-team/react-jsonschema-form/issues/3832
const retrievedSchema =
experimental_defaultFormStateBehavior?.allOf === 'populateDefaults' && ALL_OF_KEY in schema
? retrieveSchema<T, S, F>(validator, schema, rootSchema, formData, experimental_customMergeAllOf)
: schema;
// Retrieve the schema:
// - If schema contains `allOf` AND `experimental_defaultFormStateBehavior.allOf` is set to `populateDefaults`
// - OR if schema contains an 'if' AND `emptyObjectFields` is not set to `skipEmptyDefaults`
// This ensures we compute defaults correctly for schemas with these keywords.
const shouldRetrieveSchema =
(experimental_defaultFormStateBehavior?.allOf === 'populateDefaults' && ALL_OF_KEY in schema) ||
(experimental_defaultFormStateBehavior?.emptyObjectFields !== 'skipEmptyDefaults' && IF_KEY in schema);
const retrievedSchema = shouldRetrieveSchema
? retrieveSchema<T, S, F>(validator, schema, rootSchema, formData, experimental_customMergeAllOf)
: schema;
const parentConst = retrievedSchema[CONST_KEY];
const objectDefaults = Object.keys(retrievedSchema.properties || {}).reduce(
(acc: GenericObjectType, key: string) => {
Expand Down
77 changes: 77 additions & 0 deletions packages/utils/test/schema/getDefaultFormStateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,83 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
).toEqual(expected);
});
});

describe('an nested object with if/then condition', () => {
const schema: RJSFSchema = {
type: 'object',
properties: {
wrappingObject: {
type: 'object',
properties: {
checkbox: {
type: 'boolean',
},
},
if: {
properties: {
checkbox: {
const: true,
},
},
required: ['checkbox'],
},
then: {
properties: {
foo: {
type: 'string',
default: 'foo value',
},
},
required: ['foo'],
},
},
},
};
const rawFormData = {
wrappingObject: {
checkbox: true,
},
};
const expected = {
wrappingObject: {
checkbox: true,
foo: 'foo value',
},
};
test('getDefaultFormState', () => {
expect(getDefaultFormState(testValidator, schema, rawFormData, schema)).toEqual(expected);
});

test('computeDefaults', () => {
expect(
computeDefaults(testValidator, schema, {
rootSchema: schema,
rawFormData,
shouldMergeDefaultsIntoFormData: true,
}),
).toEqual(expected);
});

test('getDefaultBasedOnSchemaType', () => {
expect(
getDefaultBasedOnSchemaType(testValidator, schema, {
rootSchema: schema,
rawFormData,
shouldMergeDefaultsIntoFormData: true,
}),
).toEqual(expected);
});

test('getObjectDefaults', () => {
expect(
getObjectDefaults(testValidator, schema, {
rootSchema: schema,
rawFormData,
shouldMergeDefaultsIntoFormData: true,
}),
).toEqual(expected);
});
});
});

describe('array schemas', () => {
Expand Down