scaffolder: extract ui:* fields from conditional fields.

With this change the `ui:*` schema config is extracted also for the `then` and `else` branch of a conditional schema

Signed-off-by: Andreas Berger <andreas@berger-ecommerce.com>
This commit is contained in:
Andreas Berger
2023-05-10 15:23:29 +02:00
parent d7dfeeb5ee
commit cf34311cdb
3 changed files with 103 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-react': patch
---
Extract `ui:*` fields from conditional `then` and `else` schema branches.
@@ -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,
});
});
});
@@ -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);
}
}
/**