From 62f339728f4042ecbca1e4f98febf78cc10c130d Mon Sep 17 00:00:00 2001 From: Appie Date: Fri, 8 Nov 2024 21:35:27 +0100 Subject: [PATCH] Bug: Issue with array schema defaults not applying properly when formData is an empty array (#4359) * Fixed issue with array schema defaults not applying properly when formData is an empty array. * improvement based on feedback * fixed docs error --------- Co-authored-by: Abdallah Al-Soqatri Co-authored-by: Heath C <51679588+heath-freenome@users.noreply.github.com> --- CHANGELOG.md | 6 ++- .../docs/docs/api-reference/form-props.md | 2 +- .../utils/src/schema/getDefaultFormState.ts | 18 +++++--- .../test/schema/getDefaultFormStateTest.ts | 43 +++++++++++++++++++ 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b1c73b0a5..0265353174 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ should change the heading of the (upcoming) version to include a major version b # 5.22.4 +## @rjsf/utils + +- Fixed issue with array schema defaults not applying properly when formData is an empty array, fixing [#4335](https://github.com/rjsf-team/react-jsonschema-form/issues/4335). + ## Dev / docs / playground - Fix issue 'Maximum call stack size exceeded' with playground share with large content. @@ -26,7 +30,7 @@ should change the heading of the (upcoming) version to include a major version b ## @rjsf/utils -- Fixed deep nested dependencies issue with assigning values to formData, fixing [[#4334](https://github.com/rjsf-team/react-jsonschema-form/issues/4334)] +- Fixed deep nested dependencies issue with assigning values to formData, fixing [#4334](https://github.com/rjsf-team/react-jsonschema-form/issues/4334) # 5.22.2 diff --git a/packages/docs/docs/api-reference/form-props.md b/packages/docs/docs/api-reference/form-props.md index 7cda5b464b..e3aa52215a 100644 --- a/packages/docs/docs/api-reference/form-props.md +++ b/packages/docs/docs/api-reference/form-props.md @@ -181,7 +181,7 @@ render( schema={schema} validator={validator} experimental_defaultFormStateBehavior={{ - arrayMinItems: { populate: 'requiredOnly' }, + emptyObjectFields: 'populateRequiredDefaults', }} />, document.getElementById('app') diff --git a/packages/utils/src/schema/getDefaultFormState.ts b/packages/utils/src/schema/getDefaultFormState.ts index b82462fb99..b765da2fab 100644 --- a/packages/utils/src/schema/getDefaultFormState.ts +++ b/packages/utils/src/schema/getDefaultFormState.ts @@ -432,11 +432,14 @@ export function getArrayDefaults false); const isSkipEmptyDefaults = experimental_defaultFormStateBehavior?.emptyObjectFields === 'skipEmptyDefaults'; - const computeSkipPopulate = - experimental_defaultFormStateBehavior?.arrayMinItems?.computeSkipPopulate ?? (() => false); const emptyDefault = isSkipEmptyDefaults ? undefined : []; @@ -460,7 +463,7 @@ export function getArrayDefaults { + const itemDefaults = rawFormData.map((item: T, idx: number) => { return computeDefaults(validator, schemaItem, { rootSchema, _recurseList, @@ -470,6 +473,11 @@ export function getArrayDefaults { + const schema: RJSFSchema = { + type: 'array', + minItems: 4, + default: ['Raphael', 'Michaelangelo'], + items: { + type: 'string', + default: 'Unknown', + }, + }; + + expect( + computeDefaults(testValidator, schema, { + rootSchema: schema, + includeUndefinedValues: 'excludeObjectChildren', + }) + ).toEqual(['Raphael', 'Michaelangelo', 'Unknown', 'Unknown']); + }); + it('test an array with defaults with empty array as formData', () => { + const schema: RJSFSchema = { + type: 'array', + minItems: 4, + default: ['Raphael', 'Michaelangelo'], + items: { + type: 'string', + default: 'Unknown', + }, + }; + + expect( + computeDefaults(testValidator, schema, { + rootSchema: schema, + rawFormData: [], + includeUndefinedValues: 'excludeObjectChildren', + experimental_defaultFormStateBehavior: { + arrayMinItems: { + mergeExtraDefaults: true, + populate: 'all', + }, + }, + }) + ).toEqual(['Raphael', 'Michaelangelo', 'Unknown', 'Unknown']); + }); it('test computeDefaults handles an invalid property schema', () => { const schema: RJSFSchema = { type: 'object',