From 0d8a544975f72724db931778d7e3ace8a12b6cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20G=C3=B3mez=20Morales?= Date: Thu, 30 Jan 2025 15:15:03 +0100 Subject: [PATCH] fix(editor): SchemaView renders duplicate structures properly (#12943) --- .../src/composables/useDataSchema.test.ts | 23 +++++++++++++++++++ .../src/composables/useDataSchema.ts | 8 ++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/editor-ui/src/composables/useDataSchema.test.ts b/packages/editor-ui/src/composables/useDataSchema.test.ts index 212f767675..b98290e135 100644 --- a/packages/editor-ui/src/composables/useDataSchema.test.ts +++ b/packages/editor-ui/src/composables/useDataSchema.test.ts @@ -684,4 +684,27 @@ describe('useFlattenSchema', () => { }).length, ).toBe(3); }); + + it('items ids should be unique', () => { + const { flattenSchema } = useFlattenSchema(); + const schema: Schema = { + path: '', + type: 'object', + value: [ + { + key: 'index', + type: 'number', + value: '0', + path: '.index', + }, + ], + }; + const node1 = { name: 'First Node', type: 'any' }; + const node2 = { name: 'Second Node', type: 'any' }; + + const node1Schema = flattenSchema({ schema, node: node1, depth: 1 }); + const node2Schema = flattenSchema({ schema, node: node2, depth: 1 }); + + expect(node1Schema[0].id).not.toBe(node2Schema[0].id); + }); }); diff --git a/packages/editor-ui/src/composables/useDataSchema.ts b/packages/editor-ui/src/composables/useDataSchema.ts index 8896f8dd7e..9b06485713 100644 --- a/packages/editor-ui/src/composables/useDataSchema.ts +++ b/packages/editor-ui/src/composables/useDataSchema.ts @@ -282,6 +282,8 @@ export const useFlattenSchema = () => { path: schema.path, }); + const id = `${node.name}-${expression}`; + if (Array.isArray(schema.value)) { const items: RenderItem[] = []; @@ -293,14 +295,14 @@ export const useFlattenSchema = () => { depth, level, icon: getIconBySchemaType(schema.type), - id: expression, + id, collapsable: true, nodeType: node.type, type: 'item', }); } - if (closedNodes.value.has(expression)) { + if (closedNodes.value.has(id)) { return items; } @@ -327,7 +329,7 @@ export const useFlattenSchema = () => { level, depth, value: shorten(schema.value, 600, 0), - id: expression, + id, icon: getIconBySchemaType(schema.type), collapsable: false, nodeType: node.type,