From a8b0c802b0904f1c5b0dc6785829aab91f81ae03 Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 29 Jan 2024 14:12:36 +0100 Subject: [PATCH] chore: make sure to keep the existing uiSchema when parsing dependencies Signed-off-by: blam --- .../src/next/lib/schema.test.ts | 107 ++++++++++++++++++ .../scaffolder-react/src/next/lib/schema.ts | 10 +- 2 files changed, 114 insertions(+), 3 deletions(-) diff --git a/plugins/scaffolder-react/src/next/lib/schema.test.ts b/plugins/scaffolder-react/src/next/lib/schema.test.ts index 4dc6cda603..5bfd65133c 100644 --- a/plugins/scaffolder-react/src/next/lib/schema.test.ts +++ b/plugins/scaffolder-react/src/next/lib/schema.test.ts @@ -412,6 +412,113 @@ describe('extractSchemaFromStep', () => { }); }); + it('doesnt override existing uiSchema with things from dependencies', () => { + const inputSchema: JsonObject = { + type: 'object', + title: + "Field 2 depend on field 1, field 1 is a radio button but it's visible as a field", + required: ['exampleField1'], + properties: { + exampleField0: { + title: 'Radio button that is not a dependency', + type: 'string', + enum: ['foo', 'bar'], + 'ui:widget': 'radio', + }, + exampleField1: { + title: 'Radio button input that is a dependency', + type: 'string', + enum: ['visible', 'hidden'], + 'ui:widget': 'radio', + }, + }, + dependencies: { + exampleField1: { + oneOf: [ + { + properties: { + exampleField1: { + enum: ['visible'], + }, + exampleField2: { + title: 'FIELD 2', + type: 'string', + description: 'Explanation', + }, + }, + }, + { + properties: { + exampleField1: { + enum: ['hidden'], + }, + }, + }, + ], + }, + }, + }; + + const expectedSchema = { + type: 'object', + title: + "Field 2 depend on field 1, field 1 is a radio button but it's visible as a field", + required: ['exampleField1'], + properties: { + exampleField0: { + title: 'Radio button that is not a dependency', + type: 'string', + enum: ['foo', 'bar'], + }, + exampleField1: { + title: 'Radio button input that is a dependency', + type: 'string', + enum: ['visible', 'hidden'], + }, + }, + dependencies: { + exampleField1: { + oneOf: [ + { + properties: { + exampleField1: { + enum: ['visible'], + }, + exampleField2: { + title: 'FIELD 2', + type: 'string', + description: 'Explanation', + }, + }, + }, + { + properties: { + exampleField1: { + enum: ['hidden'], + }, + }, + }, + ], + }, + }, + }; + + const expectedUiSchema = { + exampleField0: { + 'ui:widget': 'radio', + }, + exampleField1: { + 'ui:widget': 'radio', + }, + exampleField2: {}, + }; + + expect(extractSchemaFromStep(inputSchema)).toEqual({ + schema: expectedSchema, + uiSchema: expectedUiSchema, + }); + }); + it('transforms conditional schema', () => { const inputSchema: JsonObject = { type: 'object', diff --git a/plugins/scaffolder-react/src/next/lib/schema.ts b/plugins/scaffolder-react/src/next/lib/schema.ts index b7c1364f41..2ca2a6fa7b 100644 --- a/plugins/scaffolder-react/src/next/lib/schema.ts +++ b/plugins/scaffolder-react/src/next/lib/schema.ts @@ -58,9 +58,13 @@ function extractUiSchema(schema: JsonObject, uiSchema: JsonObject) { if (!isObject(schemaNode)) { continue; } - const innerUiSchema = {}; - uiSchema[propName] = innerUiSchema; - extractUiSchema(schemaNode, innerUiSchema); + + if (!isObject(uiSchema[propName])) { + const innerUiSchema = {}; + uiSchema[propName] = innerUiSchema; + } + + extractUiSchema(schemaNode, uiSchema[propName] as JsonObject); } }