From 7e79143079b824f290750750b4bacd4f8b203479 Mon Sep 17 00:00:00 2001 From: Bane Stojkovic Date: Wed, 11 Feb 2026 16:59:16 +0100 Subject: [PATCH] Ensure no duplicate IDs when loading --- .../serialization/ProjectFileManager.java | 22 +++++++++++++++++++ .../diylc-core/src/main/resources/update.xml | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/diylc/diylc-core/src/main/java/org/diylc/serialization/ProjectFileManager.java b/diylc/diylc-core/src/main/java/org/diylc/serialization/ProjectFileManager.java index 00874444..704a0b91 100644 --- a/diylc/diylc-core/src/main/java/org/diylc/serialization/ProjectFileManager.java +++ b/diylc/diylc-core/src/main/java/org/diylc/serialization/ProjectFileManager.java @@ -36,6 +36,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.UUID; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -54,6 +55,7 @@ import com.thoughtworks.xstream.mapper.MapperWrapper; import com.thoughtworks.xstream.security.AnyTypePermission; import org.diylc.common.EventType; +import org.diylc.core.IDIYComponent; import org.diylc.core.Project; import org.diylc.presenter.Presenter; import org.diylc.test.DIYTest; @@ -279,9 +281,29 @@ public class ProjectFileManager { LOG.warn("The opened file is missing the following properties: " + String.join(", ", missingFields)); warnings.add("The project references unknown component properties, most likely because it was created with a newer version of DIYLC."); } + ensureUniqueComponentIds(project); fis.close(); return project; } + + /** + * Ensures no two components share the same identifier. Assigns a new UUID to any + * component that has a null id or an id already seen (duplicate). + */ + private void ensureUniqueComponentIds(Project project) { + if (project == null || project.getComponents() == null) { + return; + } + Set seenIds = new HashSet(); + for (IDIYComponent component : project.getComponents()) { + UUID id = component.getId(); + if (id == null || seenIds.contains(id)) { + component.setId(UUID.randomUUID()); + LOG.warn("Detected duplicated componentId: " + id); + } + seenIds.add(component.getId()); + } + } private VersionNumber readV3Version(String fileName) throws Exception { File fXmlFile = new File(fileName); diff --git a/diylc/diylc-core/src/main/resources/update.xml b/diylc/diylc-core/src/main/resources/update.xml index 03a56268..522d4c30 100644 --- a/diylc/diylc-core/src/main/resources/update.xml +++ b/diylc/diylc-core/src/main/resources/update.xml @@ -357,6 +357,10 @@ IMPROVEMENT Consolidate a few keyboard shortcuts for consistency + + BUG_FIX + Using "Duplicate Selection" results in duplicates of component ids, breaking Project Explorer panel and causing other issues downstream + BUG_FIX Calculated length disappeared from Hookup Wire and similar curved components