Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
4 tasks done
pjhh88 opened this issue Nov 28, 2024 · 2 comments · Fixed by #4425
Closed
4 tasks done

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

pjhh88 opened this issue Nov 28, 2024 · 2 comments · Fixed by #4425

Comments

@pjhh88
Copy link

pjhh88 commented Nov 28, 2024

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

@pjhh88 pjhh88 added bug needs triage Initial label given, to be assigned correct labels and assigned labels Nov 28, 2024
@heath-freenome
Copy link
Member

@abdalla-rko Seems like someone found another issue in getDefaultFormState() :(. Is this something you'd like to tackle next?

@heath-freenome heath-freenome added help wanted and removed needs triage Initial label given, to be assigned correct labels and assigned labels Dec 13, 2024
@nickgros
Copy link
Contributor

Playground link

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
3 participants