This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-tree.git


The following commit(s) were added to refs/heads/master by this push:
     new cc69b7d  Compare nodes by artifact in 
AncestorOrSelfDependencyNodeFilter
cc69b7d is described below

commit cc69b7d2141a32dfa74f23398fb44c72d10a5399
Author: Arturo Bernal <[email protected]>
AuthorDate: Mon Nov 9 19:58:12 2026 +0100

    Compare nodes by artifact in AncestorOrSelfDependencyNodeFilter
    
    DependencyNode implementations are not required to implement
    value-based equality, and DefaultDependencyNode does not override
    equals()/hashCode(), so AncestorOrSelfDependencyNodeFilter's reliance
    on DependencyNode#equals silently falls back to identity comparison.
    
    This breaks the filter whenever it is tested against a tree that was
    rebuilt from, but no longer shares node instances with, the tree it
    was built against (e.g. via BuildingDependencyNodeVisitor, a pattern
    used by consumers such as maven-dependency-plugin).
    
    Fixes #132
---
 .../filter/AncestorOrSelfDependencyNodeFilter.java |   7 +-
 .../AncestorOrSelfDependencyNodeFilterTest.java    | 103 +++++++++++++++++++++
 2 files changed, 109 insertions(+), 1 deletion(-)

diff --git 
a/src/main/java/org/apache/maven/shared/dependency/graph/filter/AncestorOrSelfDependencyNodeFilter.java
 
b/src/main/java/org/apache/maven/shared/dependency/graph/filter/AncestorOrSelfDependencyNodeFilter.java
index e96684d..e14e4af 100644
--- 
a/src/main/java/org/apache/maven/shared/dependency/graph/filter/AncestorOrSelfDependencyNodeFilter.java
+++ 
b/src/main/java/org/apache/maven/shared/dependency/graph/filter/AncestorOrSelfDependencyNodeFilter.java
@@ -20,6 +20,7 @@ package org.apache.maven.shared.dependency.graph.filter;
 
 import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 
 import org.apache.maven.shared.dependency.graph.DependencyNode;
 
@@ -74,6 +75,10 @@ public class AncestorOrSelfDependencyNodeFilter implements 
DependencyNodeFilter
 
     /**
      * Gets whether the first dependency node is an ancestor-or-self of the 
second.
+     * <p>
+     * Nodes are compared by artifact rather than by {@link 
DependencyNode#equals(Object)}, since
+     * {@link DependencyNode} implementations are not required to implement 
value-based equality and a node may be
+     * tested against a tree that was rebuilt from, but no longer shares 
instances with, the tree it originated from.
      *
      * @param ancestorNode the ancestor-or-self dependency node
      * @param descendantNode the dependency node to test
@@ -83,7 +88,7 @@ public class AncestorOrSelfDependencyNodeFilter implements 
DependencyNodeFilter
         boolean ancestor = false;
 
         while (!ancestor && descendantNode != null) {
-            ancestor = ancestorNode.equals(descendantNode);
+            ancestor = Objects.equals(ancestorNode.getArtifact(), 
descendantNode.getArtifact());
 
             descendantNode = descendantNode.getParent();
         }
diff --git 
a/src/test/java/org/apache/maven/shared/dependency/graph/filter/AncestorOrSelfDependencyNodeFilterTest.java
 
b/src/test/java/org/apache/maven/shared/dependency/graph/filter/AncestorOrSelfDependencyNodeFilterTest.java
new file mode 100644
index 0000000..2418a0c
--- /dev/null
+++ 
b/src/test/java/org/apache/maven/shared/dependency/graph/filter/AncestorOrSelfDependencyNodeFilterTest.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.shared.dependency.graph.filter;
+
+import java.util.ArrayList;
+import java.util.Collections;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import org.apache.maven.shared.dependency.graph.internal.DefaultDependencyNode;
+import 
org.apache.maven.shared.dependency.graph.traversal.BuildingDependencyNodeVisitor;
+import 
org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class AncestorOrSelfDependencyNodeFilterTest {
+
+    private final Artifact rootArtifact = new DefaultArtifact("g", "root", 
"1.0", "compile", "jar", "", null);
+    private final Artifact childArtifact = new DefaultArtifact("g", "child", 
"1.0", "compile", "jar", "", null);
+
+    @Test
+    void acceptsAncestorAndSelf() {
+        DefaultDependencyNode root =
+                new DefaultDependencyNode(null, rootArtifact, null, null, 
null, false, Collections.emptyList());
+        DefaultDependencyNode child =
+                new DefaultDependencyNode(root, childArtifact, null, null, 
null, false, Collections.emptyList());
+        root.setChildren(Collections.singletonList(child));
+
+        AncestorOrSelfDependencyNodeFilter filter = new 
AncestorOrSelfDependencyNodeFilter(child);
+
+        assertTrue(filter.accept(root));
+        assertTrue(filter.accept(child));
+    }
+
+    @Test
+    void acceptsAncestorAndSelfOnATreeRebuiltFromTheOriginal() {
+        // BuildingDependencyNodeVisitor is a common way to produce a new tree 
from an existing one (used e.g. by
+        // consumers combining a filter with FilteringDependencyNodeVisitor). 
The rebuilt tree is logically
+        // identical but made up of different DependencyNode instances than 
the tree the filter was built against.
+        DefaultDependencyNode root =
+                new DefaultDependencyNode(null, rootArtifact, null, null, 
null, false, Collections.emptyList());
+        DefaultDependencyNode child =
+                new DefaultDependencyNode(root, childArtifact, null, null, 
null, false, Collections.emptyList());
+        child.setChildren(new ArrayList<>());
+        root.setChildren(new ArrayList<>(Collections.singletonList(child)));
+
+        AncestorOrSelfDependencyNodeFilter filter = new 
AncestorOrSelfDependencyNodeFilter(child);
+
+        BuildingDependencyNodeVisitor builder = new 
BuildingDependencyNodeVisitor();
+        root.accept(new DependencyNodeVisitor() {
+            @Override
+            public boolean visit(DependencyNode node) {
+                return builder.visit(node);
+            }
+
+            @Override
+            public boolean endVisit(DependencyNode node) {
+                return builder.endVisit(node);
+            }
+        });
+        DependencyNode rebuiltRoot = builder.getDependencyTree();
+        DependencyNode rebuiltChild = rebuiltRoot.getChildren().get(0);
+
+        assertTrue(filter.accept(rebuiltRoot));
+        assertTrue(filter.accept(rebuiltChild));
+    }
+
+    @Test
+    void rejectsUnrelatedNode() {
+        DefaultDependencyNode root =
+                new DefaultDependencyNode(null, rootArtifact, null, null, 
null, false, Collections.emptyList());
+        DefaultDependencyNode child =
+                new DefaultDependencyNode(root, childArtifact, null, null, 
null, false, Collections.emptyList());
+        root.setChildren(Collections.singletonList(child));
+
+        Artifact unrelatedArtifact = new DefaultArtifact("g", "unrelated", 
"1.0", "compile", "jar", "", null);
+        DefaultDependencyNode unrelated =
+                new DefaultDependencyNode(null, unrelatedArtifact, null, null, 
null, false, Collections.emptyList());
+
+        AncestorOrSelfDependencyNodeFilter filter = new 
AncestorOrSelfDependencyNodeFilter(child);
+
+        assertFalse(filter.accept(unrelated));
+    }
+}

Reply via email to