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); } }