From ce30118cf04d4c081b01eaf4f99b26fb6b6843a4 Mon Sep 17 00:00:00 2001 From: bnechyporenko Date: Thu, 4 Jan 2024 15:18:03 +0100 Subject: [PATCH] Scaffolder: Fix for a step with no properties Signed-off-by: Bogdan Nechyporenko Signed-off-by: bnechyporenko --- .../src/next/hooks/useTemplateSchema.test.tsx | 112 ++++++++++++++++++ .../src/next/hooks/useTemplateSchema.ts | 4 +- 2 files changed, 114 insertions(+), 2 deletions(-) diff --git a/plugins/scaffolder-react/src/next/hooks/useTemplateSchema.test.tsx b/plugins/scaffolder-react/src/next/hooks/useTemplateSchema.test.tsx index ac55d73a04..d1d86ab977 100644 --- a/plugins/scaffolder-react/src/next/hooks/useTemplateSchema.test.tsx +++ b/plugins/scaffolder-react/src/next/hooks/useTemplateSchema.test.tsx @@ -263,6 +263,118 @@ describe('useTemplateSchema', () => { expect(first.schema).toEqual({ type: 'object', + properties: {}, + }); + }); + + it('should deal with dependencies and oneOf options', () => { + const firstStepDependencies = { + preconditions: { + oneOf: [ + { + title: 'About', + description: 'you have chosen option A', + properties: { + preconditions: { + enum: ['optionA'], + }, + }, + }, + { + title: 'About', + description: 'you have chosen option B', + properties: { + preconditions: { + enum: ['optionB'], + }, + }, + }, + ], + }, + }; + + const secondStepDependencies = { + preconditions: { + oneOf: [ + { + required: ['inputA'], + properties: { + preconditions: { + enum: ['optionA'], + }, + inputA: { + title: 'Input A', + type: 'string', + }, + }, + }, + { + required: ['inputB'], + properties: { + preconditions: { + enum: ['optionB'], + }, + inputA: { + title: 'Input B', + type: 'string', + }, + }, + }, + ], + }, + }; + + const manifest: TemplateParameterSchema = { + title: 'Test Template', + description: 'Test Template Description', + steps: [ + { + title: 'First step', + schema: { + type: 'object', + properties: { + preconditions: { + title: 'Preconditions', + type: 'string', + description: 'Choose an option', + enum: ['optionA', 'optionB'], + enumNames: ['Option A', 'Option B'], + }, + }, + dependencies: firstStepDependencies, + }, + }, + { + title: 'Second step', + schema: { + dependencies: secondStepDependencies, + }, + }, + ], + }; + + const { result } = renderHook(() => useTemplateSchema(manifest), { + wrapper: ({ children }: React.PropsWithChildren<{}>) => ( + false }]]} + > + {children} + + ), + }); + + const [first, second] = result.current.steps; + + expect(first.schema).toEqual({ + dependencies: firstStepDependencies, + properties: expect.anything(), + title: undefined, + type: 'object', + }); + + expect(second.schema).toEqual({ + dependencies: secondStepDependencies, + title: undefined, }); }); }); diff --git a/plugins/scaffolder-react/src/next/hooks/useTemplateSchema.ts b/plugins/scaffolder-react/src/next/hooks/useTemplateSchema.ts index aa5cefbaec..af004910be 100644 --- a/plugins/scaffolder-react/src/next/hooks/useTemplateSchema.ts +++ b/plugins/scaffolder-react/src/next/hooks/useTemplateSchema.ts @@ -69,9 +69,9 @@ export const useTemplateSchema = ( }, } as ParsedTemplateSchema; - if (step.schema?.properties) { + if (step.schema?.properties || !step.schema?.dependencies) { strippedSchema.schema.properties = Object.fromEntries( - Object.entries((step.schema?.properties ?? []) as JsonObject).filter( + Object.entries((step.schema?.properties ?? {}) as JsonObject).filter( ([key]) => { const stepFeatureFlag = step.uiSchema[key]?.['ui:backstage']?.featureFlag;