From 883b2ece7368388d76b083bf28827acde4332690 Mon Sep 17 00:00:00 2001 From: Ishank307 Date: Sun, 5 Oct 2025 11:24:49 +0530 Subject: [PATCH] Add circular type dependency detection When creating custom types that reference each other (like type_1 has a field of type_2, and type_2 has a field of type_1), the app should show an error in the issues panel. This was missing before - only table relationships were checked for circular dependencies, not custom type field references. Changes: - Added checkCircularTypes function to detect type reference loops - Added 'circular_type_dependency' translation key in English and Spanish - Now shows proper error: 'Circular type dependency involving type X' Fixes the issue where users could create invalid type structures without any warning. --- src/i18n/locales/en.js | 1 + src/i18n/locales/es.js | 1 + src/utils/issues.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/i18n/locales/en.js b/src/i18n/locales/en.js index a961c056e..0598c7f27 100644 --- a/src/i18n/locales/en.js +++ b/src/i18n/locales/en.js @@ -200,6 +200,7 @@ const en = { "Duplicate type fields by name '{{fieldName}}' in type '{{typeName}}'", duplicate_reference: "Duplicate reference by the name '{{refName}}'", circular_dependency: "Circular dependency involving table '{{refName}}'", + circular_type_dependency: "Circular type dependency involving type '{{typeName}}'", timeline: "Timeline", priority: "Priority", none: "None", diff --git a/src/i18n/locales/es.js b/src/i18n/locales/es.js index 5f8373134..814f6fd4c 100644 --- a/src/i18n/locales/es.js +++ b/src/i18n/locales/es.js @@ -195,6 +195,7 @@ const es = { "Campos de tipo duplicados por nombre '{{fieldName}}' en el tipo '{{typeName}}'", duplicate_reference: "Referencia duplicada con el nombre '{{refName}}'", circular_dependency: "Dependencia circular involucrando la tabla '{{refName}}'", + circular_type_dependency: "Dependencia circular de tipos involucrando el tipo '{{typeName}}'", timeline: "Linea del tiempo", priority: "Prioridad", none: "Ninguno", diff --git a/src/utils/issues.js b/src/utils/issues.js index 0553bb495..f4fd52d24 100644 --- a/src/utils/issues.js +++ b/src/utils/issues.js @@ -202,6 +202,7 @@ export function getIssues(diagram) { } }); + // Check for circular table relationships const visitedTables = new Set(); function checkCircularRelationships(tableId, visited = []) { @@ -230,5 +231,38 @@ export function getIssues(diagram) { } }); + // Check for circular type dependencies + function checkCircularTypes(typeName, visited = []) { + if (visited.includes(typeName)) { + issues.push( + i18n.t("circular_type_dependency", { + typeName: typeName, + }), + ); + return; + } + + const currentType = diagram.types.find((t) => t.name === typeName); + if (!currentType) return; + + visited.push(typeName); + + currentType.fields.forEach((field) => { + // Check if this field references another custom type + const referencedType = diagram.types.find((t) => t.name === field.type); + if (referencedType && field.type !== typeName) { + checkCircularTypes(field.type, [...visited]); + } + }); + } + + const visitedTypes = new Set(); + diagram.types.forEach((type) => { + if (!visitedTypes.has(type.name)) { + visitedTypes.add(type.name); + checkCircularTypes(type.name); + } + }); + return issues; }