Skip to content

Commit

Permalink
Fix: Prevent undefined properties at the root level
Browse files Browse the repository at this point in the history
Fixes: rjsf-team#3424 by preventing the inclusion of undefined properties at the root level
- Updated `@rjsf/utils`, making `computDefaults` helper in `getDefaultFormState()` to skip adding undefined values when `excludeObjectChildren` is set.
  - This basically supports adding an empty object
  - Updated the tests accordingly
- Updated the `CHANGELOG.md` accordingly
  • Loading branch information
heath-freenome committed Feb 4, 2023
1 parent c4f76ca commit cd32668
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ should change the heading of the (upcoming) version to include a major version b
## @rjsf/chakra-ui
- Fixed the `SelectWidget` to allow the proper display of the selected value, fixing [#3422](https://github.com/rjsf-team/react-jsonschema-form/issues/3422)

## @rjsf/utils
- Fixed `computeDefaults` to not put undefined values into an object when `excludeObjectChildren` is provided, fixing [#3424](https://github.com/rjsf-team/react-jsonschema-form/issues/3424)

# 5.0.2

## @rjsf/utils
Expand Down
3 changes: 3 additions & 0 deletions packages/utils/src/schema/getDefaultFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ export function computeDefaults<
: includeUndefinedValues
);
if (includeUndefinedValues) {
// When "excludeObjectChildren" don't assign undefined values
// if (includeUndefinedValues === true || computedDefault !== undefined) {
acc[key] = computedDefault;
// }
} else if (isObject(computedDefault)) {
// Store computedDefault if it's a non-empty object (e.g. not {})
if (!isEmpty(computedDefault)) {
Expand Down
9 changes: 5 additions & 4 deletions packages/utils/test/schema/getDefaultFormStateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ export default function getDefaultFormStateTest(
const schema: RJSFSchema = {
type: "object",
properties: {
optionalProperty: {
optionalNumberProperty: {
type: "number",
},
optionalObjectProperty: {
type: "object",
properties: {
nestedRequiredProperty: {
Expand Down Expand Up @@ -145,9 +148,7 @@ export default function getDefaultFormStateTest(
"excludeObjectChildren"
)
).toEqual({
optionalProperty: {
nestedRequiredProperty: undefined,
},
optionalObjectProperty: {},
requiredProperty: "foo",
});
});
Expand Down

0 comments on commit cd32668

Please sign in to comment.