From 9f61028fa14272020acb02acad549f0629b2bccc Mon Sep 17 00:00:00 2001 From: Gwendal Daniel Date: Thu, 20 Jun 2024 16:12:06 +0200 Subject: [PATCH] [3650] Fix potential NPE in DiagramNavigator and Node Bug: https://github.com/eclipse-sirius/sirius-web/issues/3650 Signed-off-by: Gwendal Daniel --- CHANGELOG.adoc | 2 +- .../tests/navigation/DiagramNavigator.java | 5 +++++ .../tests/navigation/NavigatorCache.java | 17 ++++++++++++----- .../sirius/components/diagrams/Node.java | 6 +++++- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 77af219a77..af84c9640e 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -63,7 +63,7 @@ More existing APIs will be migrated to this new common pattern. - https://github.com/eclipse-sirius/sirius-web/issues/3616[#3616] [diagram] Fix potential exceptions due to duplicate keys in diagram event processing - https://github.com/eclipse-sirius/sirius-web/issues/3624[#3624] [diagram] Fix an issue where the header separator does not fill the entire width of the node - https://github.com/eclipse-sirius/sirius-web/issues/3531[#3531] [diagram] Fix unnecessary edges label re render - +- https://github.com/eclipse-sirius/sirius-web/issues/3650[#3650] [diagram] Fix potential NPE in DiagramNavigator and Node toString method === New Features diff --git a/packages/diagrams/backend/sirius-components-diagrams-tests/src/main/java/org/eclipse/sirius/components/diagrams/tests/navigation/DiagramNavigator.java b/packages/diagrams/backend/sirius-components-diagrams-tests/src/main/java/org/eclipse/sirius/components/diagrams/tests/navigation/DiagramNavigator.java index ef3bea1932..57caa782b9 100644 --- a/packages/diagrams/backend/sirius-components-diagrams-tests/src/main/java/org/eclipse/sirius/components/diagrams/tests/navigation/DiagramNavigator.java +++ b/packages/diagrams/backend/sirius-components-diagrams-tests/src/main/java/org/eclipse/sirius/components/diagrams/tests/navigation/DiagramNavigator.java @@ -13,6 +13,7 @@ package org.eclipse.sirius.components.diagrams.tests.navigation; import java.text.MessageFormat; +import java.util.Collection; import java.util.List; import java.util.Objects; @@ -105,6 +106,10 @@ public EdgeNavigator edgeWithEdgeDescriptionId(String edgeDescriptionId) { return new EdgeNavigator(edges.get(0), this.cache); } + public Collection findAllNodes() { + return this.cache.getIdToNode().values(); + } + public int findDiagramNodeCount() { return this.cache.getDiagramNodeCount(); } diff --git a/packages/diagrams/backend/sirius-components-diagrams-tests/src/main/java/org/eclipse/sirius/components/diagrams/tests/navigation/NavigatorCache.java b/packages/diagrams/backend/sirius-components-diagrams-tests/src/main/java/org/eclipse/sirius/components/diagrams/tests/navigation/NavigatorCache.java index 6493040da3..f632dbaf28 100644 --- a/packages/diagrams/backend/sirius-components-diagrams-tests/src/main/java/org/eclipse/sirius/components/diagrams/tests/navigation/NavigatorCache.java +++ b/packages/diagrams/backend/sirius-components-diagrams-tests/src/main/java/org/eclipse/sirius/components/diagrams/tests/navigation/NavigatorCache.java @@ -123,8 +123,14 @@ private void cacheNode(Node node, List ancestors) { this.idToNode.put(node.getId(), node); List nodesWithDescription = this.nodeDescriptionIdToNodes.computeIfAbsent(node.getDescriptionId(), k -> new ArrayList<>()); nodesWithDescription.add(node); - List nodesWithLabel = this.labelToNodes.computeIfAbsent(node.getInsideLabel().getText(), k -> new ArrayList<>()); - nodesWithLabel.add(node); + if (node.getInsideLabel() != null) { + List nodesWithLabel = this.labelToNodes.computeIfAbsent(node.getInsideLabel().getText(), k -> new ArrayList<>()); + nodesWithLabel.add(node); + } + node.getOutsideLabels().forEach(outsideLabel -> { + List nodesWithLabel = this.labelToNodes.computeIfAbsent(outsideLabel.text(), k -> new ArrayList<>()); + nodesWithLabel.add(node); + }); List nodesWithTargetObjectLabel = this.targetObjectLabelToNodes.computeIfAbsent(node.getTargetObjectLabel(), k -> new ArrayList<>()); nodesWithTargetObjectLabel.add(node); List nodesWithTargetObjectId = this.targetObjectIdToNodes.computeIfAbsent(node.getTargetObjectId(), k -> new ArrayList<>()); @@ -150,9 +156,10 @@ private void cacheEdge(Edge edge) { edgesWithTargetObjectLabel.add(edge); List edgesWithTargetObjectId = this.targetObjectIdToEdges.computeIfAbsent(edge.getTargetObjectId(), k -> new ArrayList<>()); edgesWithTargetObjectId.add(edge); - - List edgesWithLabel = this.labelToEdges.computeIfAbsent(edge.getCenterLabel().getText(), k -> new ArrayList<>()); - edgesWithLabel.add(edge); + if (edge.getCenterLabel() != null) { + List edgesWithLabel = this.labelToEdges.computeIfAbsent(edge.getCenterLabel().getText(), k -> new ArrayList<>()); + edgesWithLabel.add(edge); + } } } diff --git a/packages/diagrams/backend/sirius-components-diagrams/src/main/java/org/eclipse/sirius/components/diagrams/Node.java b/packages/diagrams/backend/sirius-components-diagrams/src/main/java/org/eclipse/sirius/components/diagrams/Node.java index 2659cefc44..a245a71445 100644 --- a/packages/diagrams/backend/sirius-components-diagrams/src/main/java/org/eclipse/sirius/components/diagrams/Node.java +++ b/packages/diagrams/backend/sirius-components-diagrams/src/main/java/org/eclipse/sirius/components/diagrams/Node.java @@ -190,9 +190,13 @@ public boolean isPinned() { @Override public String toString() { + String insideLabelText = ""; + if (this.insideLabel != null) { + insideLabelText = this.insideLabel.getText(); + } String pattern = "{0} '{'id: {1}, targetObjectId: {2}, targetObjectKind: {3}, targetObjectLabel: {4}, descriptionId: {5}, state: {6}, label: {7}, styleType: {8}, borderNodeCount: {9}, childNodeCount: {10}'}'"; return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.targetObjectId, this.targetObjectKind, this.targetObjectLabel, this.descriptionId, this.state, - this.insideLabel.getText(), this.style.getClass().getSimpleName(), this.borderNodes.size(), this.childNodes.size()); + insideLabelText, this.style.getClass().getSimpleName(), this.borderNodes.size(), this.childNodes.size()); } /**