Skip to content

Bug in Assigning Default Values to formData with Deeply Nested Required Properties #4399

@pjhh88

Description

@pjhh88

Prerequisites

What theme are you using?

core

Version

5.22.3

Current Behavior

const schema: RJSFSchema = {
          type: 'object',
          properties: {
            baseProperty: {
              type: 'object',
              properties: {
                optionalProperty: {
                  type: 'object',
                  properties: {
                    nestedRequiredProperty: {
                      type: 'string',
                      default: '',
                    },
                  },
                  required: ['nestedRequiredProperty'],
                },
                requiredProperty: {
                  type: 'string',
                  default: 'foo',
                },
              },
              required: ['requiredProperty'],
            },
          },
          required: ['baseProperty'],
        };

Under the setting
experimental_defaultFormStateBehavior: { emptyObjectFields: 'populateRequiredDefaults' },

the result is:

Object {
  "baseProperty": Object {
    "optionalProperty": Object {
      "nestedRequiredProperty": "",
    },
    "requiredProperty": "foo",
  },
}

Expected Behavior

{
  baseProperty: {
    requiredProperty: 'foo',
  }
}

It seems that while only requiredProperty is explicitly marked as required in the schema, optionalProperty is being populated because its child property, nestedRequiredProperty, is required and has a default value.

Looking at the existing test cases, such as this one:

it('test an object with an optional property that has a nested required property with default', () => {
const schema: RJSFSchema = {
type: 'object',
properties: {
optionalProperty: {
type: 'object',
properties: {
nestedRequiredProperty: {
type: 'string',
default: '',
},
},
required: ['nestedRequiredProperty'],
},
requiredProperty: {
type: 'string',
default: 'foo',
},
},
required: ['requiredProperty'],
};
expect(
computeDefaults(testValidator, schema, {
rootSchema: schema,
experimental_defaultFormStateBehavior: { emptyObjectFields: 'populateRequiredDefaults' },
})
).toEqual({ requiredProperty: 'foo' });
});

It appears that this behavior works correctly for 2-level nested structures, but a bug seems to occur when the structure goes 3 levels deep.

Steps To Reproduce

No response

Environment

- OS:
- Node:
- npm:

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions