From e7a770511abec7f1ef21b80189347037053993c0 Mon Sep 17 00:00:00 2001 From: Nicolai Bjerre Pedersen Date: Wed, 10 Mar 2021 21:09:02 +0100 Subject: [PATCH 1/6] fix: title from refs schema before fallback value --- packages/core/src/components/fields/MultiSchemaField.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/core/src/components/fields/MultiSchemaField.js b/packages/core/src/components/fields/MultiSchemaField.js index 11ec9afe81..aefd90c9cf 100644 --- a/packages/core/src/components/fields/MultiSchemaField.js +++ b/packages/core/src/components/fields/MultiSchemaField.js @@ -132,7 +132,10 @@ class AnyOfField extends Component { } const enumOptions = options.map((option, index) => ({ - label: option.title || `Option ${index + 1}`, + label: + option.title || + retrieveSchema(option, registry.rootSchema).title || + `Option ${index + 1}`, value: index, })); From 50d92d3f5f0aa4ad43e0cabea36c7d2e2b203a06 Mon Sep 17 00:00:00 2001 From: Nicolai Bjerre Pedersen Date: Wed, 10 Mar 2021 21:37:55 +0100 Subject: [PATCH 2/6] Added test for #2270 --- packages/core/test/anyOf_test.js | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/packages/core/test/anyOf_test.js b/packages/core/test/anyOf_test.js index a7bc43e388..5a4349456b 100644 --- a/packages/core/test/anyOf_test.js +++ b/packages/core/test/anyOf_test.js @@ -639,6 +639,52 @@ describe("anyOf", () => { }); }); + it("should use title from refs schema before using fallback generated value as title", () => { + const schema = { + definitions: { + address: { + title: "Address", + type: "object", + properties: { + street: { + title: "Street", + type: "string", + }, + }, + }, + person: { + title: "Person", + type: "object", + properties: { + name: { + title: "Name", + type: "string", + }, + }, + }, + nested: { + $ref: "#/definitions/person", + }, + }, + anyOf: [ + { + $ref: "#/definitions/address", + }, + { + $ref: "#/definitions/nested", + }, + ], + }; + + const { node } = createFormComponent({ + schema, + }); + + let options = node.querySelectorAll("option"); + expect(options[0].firstChild.nodeValue).eql("Address"); + expect(options[1].firstChild.nodeValue).eql("Person"); + }); + describe("Arrays", () => { it("should correctly render form inputs for anyOf inside array items", () => { const schema = { From 0a0c4c4dcc2668d2b828ad1eb4ea289a1b023191 Mon Sep 17 00:00:00 2001 From: Nicolai Bjerre Pedersen Date: Thu, 11 Mar 2021 10:10:37 +0100 Subject: [PATCH 3/6] After review --- .../core/src/components/fields/MultiSchemaField.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/core/src/components/fields/MultiSchemaField.js b/packages/core/src/components/fields/MultiSchemaField.js index aefd90c9cf..b989dd83a5 100644 --- a/packages/core/src/components/fields/MultiSchemaField.js +++ b/packages/core/src/components/fields/MultiSchemaField.js @@ -108,7 +108,6 @@ class AnyOfField extends Component { onBlur, onChange, onFocus, - options, registry, uiSchema, schema, @@ -120,6 +119,10 @@ class AnyOfField extends Component { const { widget = "select", ...uiOptions } = getUiOptions(uiSchema); const Widget = getWidget({ type: "number" }, widget, widgets); + // get the dereference schemas + const options = this.props.options.map(option => + retrieveSchema(option, registry.rootSchema) + ); const option = options[selectedOption] || null; let optionSchema; @@ -132,10 +135,7 @@ class AnyOfField extends Component { } const enumOptions = options.map((option, index) => ({ - label: - option.title || - retrieveSchema(option, registry.rootSchema).title || - `Option ${index + 1}`, + label: option.title || `Option ${index + 1}`, value: index, })); From ac7f42819106f6c75e1044377a79537a4e627fd1 Mon Sep 17 00:00:00 2001 From: Nicolai Bjerre Pedersen Date: Thu, 11 Mar 2021 10:52:09 +0100 Subject: [PATCH 4/6] Add formData when dereferencing --- packages/core/src/components/fields/MultiSchemaField.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/components/fields/MultiSchemaField.js b/packages/core/src/components/fields/MultiSchemaField.js index b989dd83a5..c14bbb9489 100644 --- a/packages/core/src/components/fields/MultiSchemaField.js +++ b/packages/core/src/components/fields/MultiSchemaField.js @@ -121,7 +121,7 @@ class AnyOfField extends Component { // get the dereference schemas const options = this.props.options.map(option => - retrieveSchema(option, registry.rootSchema) + retrieveSchema(option, registry.rootSchema, formData) ); const option = options[selectedOption] || null; let optionSchema; From bd50850f7142cd4ceb68f9fabd8fd8482085dcde Mon Sep 17 00:00:00 2001 From: Nicolai Bjerre Pedersen Date: Fri, 12 Mar 2021 12:46:55 +0100 Subject: [PATCH 5/6] Moved logic to SchemaField, so props.options are already dereferenced --- packages/core/src/components/fields/MultiSchemaField.js | 4 +--- packages/core/src/components/fields/SchemaField.js | 8 ++++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/core/src/components/fields/MultiSchemaField.js b/packages/core/src/components/fields/MultiSchemaField.js index c14bbb9489..f998fb92a7 100644 --- a/packages/core/src/components/fields/MultiSchemaField.js +++ b/packages/core/src/components/fields/MultiSchemaField.js @@ -108,6 +108,7 @@ class AnyOfField extends Component { onBlur, onChange, onFocus, + options, registry, uiSchema, schema, @@ -120,9 +121,6 @@ class AnyOfField extends Component { const Widget = getWidget({ type: "number" }, widget, widgets); // get the dereference schemas - const options = this.props.options.map(option => - retrieveSchema(option, registry.rootSchema, formData) - ); const option = options[selectedOption] || null; let optionSchema; diff --git a/packages/core/src/components/fields/SchemaField.js b/packages/core/src/components/fields/SchemaField.js index 526c008b28..5f4087be23 100644 --- a/packages/core/src/components/fields/SchemaField.js +++ b/packages/core/src/components/fields/SchemaField.js @@ -362,7 +362,9 @@ function SchemaFieldRender(props) { onBlur={props.onBlur} onChange={props.onChange} onFocus={props.onFocus} - options={schema.anyOf} + options={schema.anyOf.map(_schema => + retrieveSchema(_schema, rootSchema, formData) + )} baseType={schema.type} registry={registry} schema={schema} @@ -380,7 +382,9 @@ function SchemaFieldRender(props) { onBlur={props.onBlur} onChange={props.onChange} onFocus={props.onFocus} - options={schema.oneOf} + options={schema.oneOf.map(_schema => + retrieveSchema(_schema, rootSchema, formData) + )} baseType={schema.type} registry={registry} schema={schema} From 18874be60afd082794f995d642d3e15c2ff08b80 Mon Sep 17 00:00:00 2001 From: Nicolai Bjerre Pedersen Date: Fri, 12 Mar 2021 12:50:26 +0100 Subject: [PATCH 6/6] Removed wrong comment --- packages/core/src/components/fields/MultiSchemaField.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/core/src/components/fields/MultiSchemaField.js b/packages/core/src/components/fields/MultiSchemaField.js index f998fb92a7..11ec9afe81 100644 --- a/packages/core/src/components/fields/MultiSchemaField.js +++ b/packages/core/src/components/fields/MultiSchemaField.js @@ -120,7 +120,6 @@ class AnyOfField extends Component { const { widget = "select", ...uiOptions } = getUiOptions(uiSchema); const Widget = getWidget({ type: "number" }, widget, widgets); - // get the dereference schemas const option = options[selectedOption] || null; let optionSchema;