Merge pull request #22584 from backstage/blam/scaffolder-schema

Fix issue with `ui:schema` being replaced to empty object
This commit is contained in:
Ben Lambert
2024-01-30 09:34:09 +01:00
committed by GitHub
3 changed files with 119 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-react': patch
---
Fix issue where `ui:schema` was replaced with an empty object if `dependencies` is defined
@@ -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',
@@ -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);
}
}