Repository: quarkusio/quarkus
Issue: Dependency Graph Walk leads to StackOverflowError

### Describe the bug

Our `ConsumerPactTest` throws `java.lang.StackOverflowError`. I tracked this issue down to the #26411.

### Expected behavior

No stack issues.

### Actual behavior

_No response_

### How to Reproduce?

Reproducer:

Steps to reproduce the behavior:
1. `git clone https://github.com/quarkus-qe/quarkus-test-suite`
2. `cd quarkus-test-suite/test-tooling/pact`
3. `mvn clean verify`

### Output of `uname -a` or `ver`

Linux fedora 5.18.5-200.fc36.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jun 16 14:51:11 UTC 2022 x86_64 x86_64 x86_64 GNU/Lin

### Output of `java -version`

openjdk version "17.0.3" 2022-04-19 OpenJDK Runtime Environment GraalVM CE 22.1.0 (build 17.0.3+7-jvmci-22.1-b06) OpenJDK 64-Bit Server VM GraalVM CE 22.1.0 (build 17.0.3+7-jvmci-22.1-b06, mixed mode, sharing)

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

999-SNAPSHOT; HEAD d94c5a1d0a55787a2349cbb103a1f42771d23c0a

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)

### Additional information

_No response_

Changed files:
independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/ApplicationDependencyTreeResolver.java

diff --git a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/ApplicationDependencyTreeResolver.java b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/ApplicationDependencyTreeResolver.java
index 3f830e35f70..6a5121dee0e 100644
--- a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/ApplicationDependencyTreeResolver.java
+++ b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/ApplicationDependencyTreeResolver.java
@@ -286,13 +286,14 @@ private DependencyNode resolveRuntimeDeps(CollectRequest request) throws AppMode
                 @Override
                 public DependencyNode transformGraph(DependencyNode node, DependencyGraphTransformationContext context)
                         throws RepositoryException {
+                    final Set<ArtifactKey> visited = new HashSet<>();
                     for (DependencyNode c : node.getChildren()) {
-                        walk(c);
+                        walk(c, visited);
                     }
                     return resolver.getSession().getDependencyGraphTransformer().transformGraph(node, context);
                 }
 
-                private void walk(DependencyNode node) {
+                private void walk(DependencyNode node, Set<ArtifactKey> visited) {
                     if (node.getChildren().isEmpty()) {
                         return;
                     }
@@ -300,8 +301,13 @@ private void walk(DependencyNode node) {
                             .computeIfAbsent(DependencyUtils.getCoords(node.getArtifact()),
                                     k -> new HashSet<>(node.getChildren().size()));
                     for (DependencyNode c : node.getChildren()) {
-                        deps.add(getKey(c.getArtifact()));
-                        walk(c);
+                        final ArtifactKey key = getKey(c.getArtifact());
+                        if (!visited.add(key)) {
+                            continue;
+                        }
+                        deps.add(key);
+                        walk(c, visited);
+                        visited.remove(key);
                     }
                 }
             });