This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-4.10.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.10.x by this push: new 29c9ace0e92 CAMEL-22155: camel-main: Fix duplicate route id detection when loading mulitple resources where routes can be inlined in rest-dsl. 29c9ace0e92 is described below commit 29c9ace0e92135660b74e77f3b524c6e8d750370 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Jun 10 13:34:05 2025 +0200 CAMEL-22155: camel-main: Fix duplicate route id detection when loading mulitple resources where routes can be inlined in rest-dsl. --- .../apache/camel/FailedToCreateRouteException.java | 5 ++++ .../org/apache/camel/main/RoutesConfigurer.java | 34 +++++++++++++++++----- .../java/org/apache/camel/main/MainScan3Test.java | 6 ++-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/core/camel-api/src/main/java/org/apache/camel/FailedToCreateRouteException.java b/core/camel-api/src/main/java/org/apache/camel/FailedToCreateRouteException.java index 9a2a7b2d5e6..4f217beb9e6 100644 --- a/core/camel-api/src/main/java/org/apache/camel/FailedToCreateRouteException.java +++ b/core/camel-api/src/main/java/org/apache/camel/FailedToCreateRouteException.java @@ -25,6 +25,11 @@ public class FailedToCreateRouteException extends RuntimeCamelException { private final String routeId; + public FailedToCreateRouteException(String cause) { + super("Failed to create route because of " + cause); + this.routeId = null; + } + public FailedToCreateRouteException(String routeId, String route, String cause) { super("Failed to create route " + routeId + ": " + getRouteMessage(route) + " because of " + cause); this.routeId = routeId; diff --git a/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java b/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java index 16414ae1e37..71119f09802 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java @@ -18,12 +18,13 @@ package org.apache.camel.main; import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringJoiner; +import java.util.stream.Collectors; import org.apache.camel.CamelContext; import org.apache.camel.FailedToCreateRouteException; @@ -329,6 +330,9 @@ public class RoutesConfigurer extends ServiceSupport implements NonManagedServic // sort routes according to ordered routes.sort(OrderedComparator.get()); + // prepare duplicate route id detector + detector.clear(); + // first add the routes configurations as they are globally for all routes for (RoutesBuilder builder : routes) { try { @@ -370,6 +374,17 @@ public class RoutesConfigurer extends ServiceSupport implements NonManagedServic } } } + + // check for duplicate route ids + var ids = detector.getRouteIds(); + var dups = ids.stream() + .filter(i -> Collections.frequency(ids, i) > 1) + .collect(Collectors.toSet()); + if (!dups.isEmpty()) { + String id = String.join(",", dups); + throw new FailedToCreateRouteException( + "duplicate route ids detected: " + id + ". Please correct ids to be unique among all your routes."); + } } /** @@ -537,12 +552,16 @@ public class RoutesConfigurer extends ServiceSupport implements NonManagedServic private static class DuplicateRouteDetector extends ModelLifecycleStrategySupport { - private final Set<String> ids = new HashSet<>(); + private final List<String> ids = new ArrayList<>(); void clear() { ids.clear(); } + public List<String> getRouteIds() { + return ids; + } + @Override public void onAddRouteDefinition(RouteDefinition definition) { String id = definition.getRouteId(); @@ -550,16 +569,17 @@ public class RoutesConfigurer extends ServiceSupport implements NonManagedServic if (id == null || id.isEmpty()) { return; } + // skip inlined + if (definition.isInlined()) { + return; + } String prefix = definition.getNodePrefixId(); + if (prefix == null) { prefix = ""; } String key = id + prefix; - if (!ids.add(key)) { - throw new FailedToCreateRouteException( - definition.getId(), definition.toString(), - "duplicate route id detected " + id + ". Please correct ids to be unique among all your routes."); - } + ids.add(key); } } } diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainScan3Test.java b/core/camel-main/src/test/java/org/apache/camel/main/MainScan3Test.java index 3228ae68c6d..36bf3da9adb 100644 --- a/core/camel-main/src/test/java/org/apache/camel/main/MainScan3Test.java +++ b/core/camel-main/src/test/java/org/apache/camel/main/MainScan3Test.java @@ -20,7 +20,6 @@ import org.apache.camel.FailedToCreateRouteException; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; public class MainScan3Test { @@ -33,8 +32,9 @@ public class MainScan3Test { main.start(); fail(); } catch (FailedToCreateRouteException e) { - assertEquals("foo2", e.getRouteId()); - assertTrue(e.getMessage().contains("because of duplicate route id detected foo2")); + assertEquals( + "Failed to create route because of duplicate route ids detected: foo2. Please correct ids to be unique among all your routes.", + e.getMessage()); } main.stop();