diff --git a/.changeset/silent-kangaroos-pay.md b/.changeset/silent-kangaroos-pay.md new file mode 100644 index 0000000000..f3da827a34 --- /dev/null +++ b/.changeset/silent-kangaroos-pay.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-react': patch +--- + +Extract `ui:*` fields from conditional `then` and `else` schema branches. diff --git a/plugins/scaffolder-react/src/next/lib/schema.test.ts b/plugins/scaffolder-react/src/next/lib/schema.test.ts index 01e6cea3cc..4dc6cda603 100644 --- a/plugins/scaffolder-react/src/next/lib/schema.test.ts +++ b/plugins/scaffolder-react/src/next/lib/schema.test.ts @@ -411,4 +411,84 @@ describe('extractSchemaFromStep', () => { uiSchema: expectedUiSchema, }); }); + + it('transforms conditional schema', () => { + const inputSchema: JsonObject = { + type: 'object', + properties: { + flag: { + type: 'boolean', + }, + }, + if: { + properties: { + flag: { + const: true, + }, + }, + }, + then: { + properties: { + user: { + type: 'string', + 'ui:field': 'EntityPicker', + 'ui:options': { + catalogFilter: [{ kind: 'User' }], + }, + }, + }, + }, + else: { + properties: { + email: { + type: 'string', + }, + }, + }, + }; + const expectedSchema = { + type: 'object', + properties: { + flag: { + type: 'boolean', + }, + }, + if: { + properties: { + flag: { + const: true, + }, + }, + }, + then: { + properties: { + user: { + type: 'string', + }, + }, + }, + else: { + properties: { + email: { + type: 'string', + }, + }, + }, + }; + const expectedUiSchema = { + flag: {}, + user: { + 'ui:field': 'EntityPicker', + 'ui:options': { + catalogFilter: [{ kind: 'User' }], + }, + }, + email: {}, + }; + + expect(extractSchemaFromStep(inputSchema)).toEqual({ + schema: expectedSchema, + uiSchema: expectedUiSchema, + }); + }); }); diff --git a/plugins/scaffolder-react/src/next/lib/schema.ts b/plugins/scaffolder-react/src/next/lib/schema.ts index e8b819be4b..c606d715e7 100644 --- a/plugins/scaffolder-react/src/next/lib/schema.ts +++ b/plugins/scaffolder-react/src/next/lib/schema.ts @@ -25,7 +25,16 @@ function extractUiSchema(schema: JsonObject, uiSchema: JsonObject) { return; } - const { properties, items, anyOf, oneOf, allOf, dependencies } = schema; + const { + properties, + items, + anyOf, + oneOf, + allOf, + dependencies, + then, + else: _else, + } = schema; for (const propName in schema) { if (!schema.hasOwnProperty(propName)) { @@ -96,6 +105,14 @@ function extractUiSchema(schema: JsonObject, uiSchema: JsonObject) { extractUiSchema(schemaNode, uiSchema); } } + + if (isObject(then)) { + extractUiSchema(then, uiSchema); + } + + if (isObject(_else)) { + extractUiSchema(_else, uiSchema); + } } /**