From 907945ea149b9df261a0e694a8f4ff6a47fb393e Mon Sep 17 00:00:00 2001 From: Rani Date: Tue, 3 May 2022 08:23:48 -0700 Subject: [PATCH] fix: omit computedDefault of empty objects --- packages/core/src/utils.js | 3 ++- packages/core/test/utils_test.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/core/src/utils.js b/packages/core/src/utils.js index c313515eaa..80d54df4dc 100644 --- a/packages/core/src/utils.js +++ b/packages/core/src/utils.js @@ -3,6 +3,7 @@ import * as ReactIs from "react-is"; import mergeAllOf from "json-schema-merge-allof"; import fill from "core-js-pure/features/array/fill"; import union from "lodash/union"; +import isEmpty from "lodash/isEmpty"; import jsonpointer from "jsonpointer"; import fields from "./components/fields"; import widgets from "./components/widgets"; @@ -244,7 +245,7 @@ function computeDefaults( (formData || {})[key], includeUndefinedValues ); - if (includeUndefinedValues || computedDefault !== undefined) { + if (includeUndefinedValues || !isEmpty(computedDefault)) { acc[key] = computedDefault; } return acc; diff --git a/packages/core/test/utils_test.js b/packages/core/test/utils_test.js index a9854c2147..53bbdaae93 100644 --- a/packages/core/test/utils_test.js +++ b/packages/core/test/utils_test.js @@ -1093,6 +1093,31 @@ describe("utils", () => { }); }); + it("should not create empty objects a default value", () => { + const schema = { + type: "object", + required: [], + properties: { + value: { + type: "object", + required: ["a"], + properties: { + a: { + type: "string", + }, + b: { + type: "string", + }, + c: { + type: "string", + }, + }, + }, + }, + }; + expect(getDefaultFormState(schema, {})).eql({}); + }); + it("should not crash for defaults for nested dependencies when formData passed to computeDefaults is null", () => { const schema = { type: "object",