diff --git a/.github/playwright/impact-map.json b/.github/playwright/impact-map.json index cf9dbc6ba205..fbfd370b37b8 100644 --- a/.github/playwright/impact-map.json +++ b/.github/playwright/impact-map.json @@ -88,6 +88,18 @@ "openmetadata-ui/src/main/resources/ui/vite/**" ], "mappings": [ + { + "_comment": "Task Form designer: field builder, schema round-trip, and the custom forms a task workflow renders from it.", + "sources": [ + "openmetadata-ui/src/main/resources/ui/src/pages/TaskFormSettingsPage/**", + "openmetadata-ui/src/main/resources/ui/src/utils/TaskFormDesignerUtils*" + ], + "projects": ["chromium"], + "specs": [ + "playwright/e2e/Pages/TaskFormSettings.spec.ts", + "playwright/e2e/Features/Tasks/TaskCustomFormWorkflow.spec.ts" + ] + }, { "_comment": "MCP administrator controls and client revocation.", "sources": [ diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/TaskFormSettingsPage/components/TaskFormBuilderSection.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/TaskFormSettingsPage/components/TaskFormBuilderSection.tsx index 5cba973062e5..7efd65d00f1a 100644 --- a/openmetadata-ui/src/main/resources/ui/src/pages/TaskFormSettingsPage/components/TaskFormBuilderSection.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/pages/TaskFormSettingsPage/components/TaskFormBuilderSection.tsx @@ -45,21 +45,10 @@ import { createEmptyDesignerField, getDesignerPreviewPayload, TaskFormDesignerField, + withFieldType, } from '../../../utils/TaskFormDesignerUtils'; import TaskPayloadSchemaFields from '../../TasksPage/shared/TaskPayloadSchemaFields'; -/** Switching field type keeps options only for selects and defaults JSON to an object. */ -const withFieldType = ( - field: TaskFormDesignerField, - type: TaskFormDesignerField['type'] -): TaskFormDesignerField => ({ - ...field, - type, - schemaType: type === 'json' ? field.schemaType ?? 'object' : field.schemaType, - options: - type === 'singleSelect' || type === 'multiSelect' ? field.options : [], -}); - const withoutOption = ( field: TaskFormDesignerField, key: Key diff --git a/openmetadata-ui/src/main/resources/ui/src/utils/TaskFormDesignerUtils.test.ts b/openmetadata-ui/src/main/resources/ui/src/utils/TaskFormDesignerUtils.test.ts index d8af85b1728c..3b712c637922 100644 --- a/openmetadata-ui/src/main/resources/ui/src/utils/TaskFormDesignerUtils.test.ts +++ b/openmetadata-ui/src/main/resources/ui/src/utils/TaskFormDesignerUtils.test.ts @@ -24,17 +24,72 @@ * limitations under the License. */ +import { JsonSchemaObject } from '../rest/taskFormSchemasAPI'; import { buildDesignerSchema, buildStageMappings, buildTransitionForms, + createEmptyDesignerField, getDesignerPreviewPayload, parseSchemaToDesignerFields, parseStageMappings, parseTransitionForms, + withFieldType, } from './TaskFormDesignerUtils'; describe('TaskFormDesignerUtils', () => { + const reloadField = (property: JsonSchemaObject) => + parseSchemaToDesignerFields({ + type: 'object', + properties: { payload: property }, + })[0]; + + it.each([ + ['a new field', createEmptyDesignerField(), 'object'], + ['a reloaded text field', reloadField({ type: 'string' }), 'object'], + [ + 'a reloaded multi-select', + reloadField({ type: 'array', items: { type: 'string', enum: ['a'] } }), + 'array', + ], + ])( + 'saves %s switched to JSON and reloads it as JSON', + (_, field, expectedType) => { + const jsonField = withFieldType({ ...field, name: 'payload' }, 'json'); + const { formSchema, uiSchema } = buildDesignerSchema([jsonField]); + const saved = (formSchema.properties as Record).payload; + + expect(jsonField.schemaType).toBe(expectedType); + expect(saved).toEqual( + expectedType === 'array' + ? expect.objectContaining({ + type: 'array', + items: { type: 'string' }, + }) + : expect.objectContaining({ + type: 'object', + additionalProperties: true, + }) + ); + expect(parseSchemaToDesignerFields(formSchema, uiSchema)[0].type).toBe( + 'json' + ); + } + ); + + it('saves a JSON field as an object even when it arrives with a string schema type', () => { + const { formSchema } = buildDesignerSchema([ + { ...createEmptyDesignerField(), name: 'payload', type: 'json' }, + ]); + + expect(formSchema.properties).toEqual({ + payload: expect.objectContaining({ + type: 'object', + additionalProperties: true, + }), + }); + }); + it('builds schemas from designer fields while preserving ui metadata', () => { const { formSchema, uiSchema } = buildDesignerSchema( [ diff --git a/openmetadata-ui/src/main/resources/ui/src/utils/TaskFormDesignerUtils.ts b/openmetadata-ui/src/main/resources/ui/src/utils/TaskFormDesignerUtils.ts index 6f793ea026f2..d11d63fbc689 100644 --- a/openmetadata-ui/src/main/resources/ui/src/utils/TaskFormDesignerUtils.ts +++ b/openmetadata-ui/src/main/resources/ui/src/utils/TaskFormDesignerUtils.ts @@ -24,7 +24,7 @@ * limitations under the License. */ -import { cloneDeep } from 'lodash'; +import { cloneDeep, omit } from 'lodash'; import { JsonSchemaObject, TaskFormSchema } from '../rest/taskFormSchemasAPI'; export type TaskFormDesignerFieldType = @@ -147,6 +147,10 @@ type FieldPropertyBuilder = ( field: TaskFormDesignerField ) => void; +/** A JSON field is only ever an object or an array; anything else (e.g. a text field's 'string') defaults to object. */ +export const toJsonSchemaType = (schemaType: unknown): 'object' | 'array' => + schemaType === 'array' ? 'array' : 'object'; + const applyStringProperty: FieldPropertyBuilder = (property) => { property.type = 'string'; delete property.enum; @@ -182,13 +186,15 @@ const FIELD_PROPERTY_BUILDERS: Partial< delete property.enum; }, json: (property, field) => { - property.type = field.schemaType ?? property.type ?? 'object'; + property.type = toJsonSchemaType(field.schemaType ?? property.type); delete property.enum; if (property.type === 'array') { - property.items = (property.items as JsonSchemaObject | undefined) ?? { - type: 'string', - }; + // A leftover items.enum would make the field reload as a multi-select. + property.items = omit( + (property.items as JsonSchemaObject | undefined) ?? { type: 'string' }, + 'enum' + ); } else if ( property.type === 'object' && !Object.prototype.hasOwnProperty.call(property, 'properties') && @@ -287,6 +293,19 @@ const buildUiSchema = ( export const stringifyDesignerJson = (value: unknown) => JSON.stringify(value ?? {}, null, 2); +/** Switching field type keeps options only for selects and defaults JSON to an object. */ +export const withFieldType = ( + field: TaskFormDesignerField, + type: TaskFormDesignerField['type'] +): TaskFormDesignerField => ({ + ...field, + type, + schemaType: + type === 'json' ? toJsonSchemaType(field.schemaType) : field.schemaType, + options: + type === 'singleSelect' || type === 'multiSelect' ? field.options : [], +}); + export const createEmptyDesignerField = (): TaskFormDesignerField => ({ key: createDesignerKey('field'), name: '',