diff --git a/src/utils.js b/src/utils.js index 0b943949bf..2099c0412e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -184,7 +184,7 @@ function computeDefaults( defaults = schema.default; } - switch (schema.type) { + switch (getSchemaType(schema)) { // We need to recur for object schema inner default values. case "object": return Object.keys(schema.properties || {}).reduce((acc, key) => { diff --git a/test/anyOf_test.js b/test/anyOf_test.js index b5aba8d839..aea2ca9a5b 100644 --- a/test/anyOf_test.js +++ b/test/anyOf_test.js @@ -85,6 +85,36 @@ describe("anyOf", () => { expect(comp.state.formData).eql({ foo: "defaultbar" }); }); + it("should assign a default value and set defaults on option change with 'type': 'object' missing", () => { + const { comp, node } = createFormComponent({ + schema: { + type: "object", + anyOf: [ + { + properties: { + foo: { type: "string", default: "defaultfoo" }, + }, + }, + { + properties: { + foo: { type: "string", default: "defaultbar" }, + }, + }, + ], + }, + }); + + expect(comp.state.formData).eql({ foo: "defaultfoo" }); + + const $select = node.querySelector("select"); + + Simulate.change($select, { + target: { value: $select.options[1].value }, + }); + + expect(comp.state.formData).eql({ foo: "defaultbar" }); + }); + it("should render a custom widget", () => { const schema = { type: "object", diff --git a/test/oneOf_test.js b/test/oneOf_test.js index 386ae59b10..14cf77049d 100644 --- a/test/oneOf_test.js +++ b/test/oneOf_test.js @@ -85,6 +85,36 @@ describe("oneOf", () => { expect(comp.state.formData).eql({ foo: "defaultbar" }); }); + it("should assign a default value and set defaults on option change with 'type': 'object' missing", () => { + const { comp, node } = createFormComponent({ + schema: { + type: "object", + oneOf: [ + { + properties: { + foo: { type: "string", default: "defaultfoo" }, + }, + }, + { + properties: { + foo: { type: "string", default: "defaultbar" }, + }, + }, + ], + }, + }); + + expect(comp.state.formData).eql({ foo: "defaultfoo" }); + + const $select = node.querySelector("select"); + + Simulate.change($select, { + target: { value: $select.options[1].value }, + }); + + expect(comp.state.formData).eql({ foo: "defaultbar" }); + }); + it("should render a custom widget", () => { const schema = { type: "object", diff --git a/test/utils_test.js b/test/utils_test.js index 0da762e490..56ed93ad03 100644 --- a/test/utils_test.js +++ b/test/utils_test.js @@ -359,6 +359,23 @@ describe("utils", () => { }); }); + it("should populate defaults for oneOf when 'type': 'object' is missing", () => { + const schema = { + type: "object", + oneOf: [ + { + properties: { name: { type: "string", default: "a" } }, + }, + { + properties: { id: { type: "number", default: 13 } }, + }, + ], + }; + expect(getDefaultFormState(schema, {})).eql({ + name: "a", + }); + }); + it("should populate nested default values for oneOf", () => { const schema = { type: "object",