From 5fe3362dfcbf62730fe7c63bab2d8f9e629845e9 Mon Sep 17 00:00:00 2001 From: joerg1985 <16140691+joerg1985@users.noreply.github.com> Date: Fri, 3 May 2024 23:47:13 +0200 Subject: [PATCH] [grid] flatten combined routes to improve routing (#13856) Co-authored-by: Diego Molina --- .../openqa/selenium/remote/http/Route.java | 20 ++++++++++++++++--- .../selenium/remote/http/RouteTest.java | 7 +++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/java/src/org/openqa/selenium/remote/http/Route.java b/java/src/org/openqa/selenium/remote/http/Route.java index 5503136befb43..047ed4247e7f8 100644 --- a/java/src/org/openqa/selenium/remote/http/Route.java +++ b/java/src/org/openqa/selenium/remote/http/Route.java @@ -27,8 +27,8 @@ import static org.openqa.selenium.remote.http.HttpMethod.POST; import static org.openqa.selenium.remote.http.UrlPath.ROUTE_PREFIX_KEY; +import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -292,7 +292,7 @@ private HttpRequest transform(HttpRequest request) { // Don't forget to register our prefix Object rawPrefixes = request.getAttribute(ROUTE_PREFIX_KEY); if (!(rawPrefixes instanceof List)) { - rawPrefixes = new LinkedList<>(); + rawPrefixes = Collections.emptyList(); } List prefixes = Stream.concat(((List) rawPrefixes).stream(), Stream.of(prefix)) @@ -321,7 +321,21 @@ private static class CombinedRoute extends Route { private CombinedRoute(Stream routes) { // We want later routes to have a greater chance of being called so that we can override // routes as necessary. - List routables = routes.collect(Collectors.toList()); + List routables = + routes + .flatMap( + route -> { + // flatten a nested CombinedRoute + if (route instanceof CombinedRoute) { + List nestedRoutes = + new ArrayList<>(((CombinedRoute) route).allRoutes); + // reverse to have the identical behaviour like not flattened + Collections.reverse(nestedRoutes); + return nestedRoutes.stream(); + } + return Stream.of(route); + }) + .collect(Collectors.toList()); Collections.reverse(routables); allRoutes = List.copyOf(routables); Require.stateCondition(!allRoutes.isEmpty(), "At least one route must be specified."); diff --git a/java/test/org/openqa/selenium/remote/http/RouteTest.java b/java/test/org/openqa/selenium/remote/http/RouteTest.java index 6d25f5542502d..799db1405efec 100644 --- a/java/test/org/openqa/selenium/remote/http/RouteTest.java +++ b/java/test/org/openqa/selenium/remote/http/RouteTest.java @@ -152,8 +152,11 @@ void laterRoutesOverrideEarlierRoutesToFacilitateOverridingRoutes() { HttpHandler handler = Route.combine( Route.get("/hello").to(() -> req -> new HttpResponse().setContent(utf8String("world"))), - Route.get("/hello") - .to(() -> req -> new HttpResponse().setContent(utf8String("buddy")))); + Route.combine( + Route.get("/hello") + .to(() -> req -> new HttpResponse().setContent(utf8String("world"))), + Route.get("/hello") + .to(() -> req -> new HttpResponse().setContent(utf8String("buddy"))))); HttpResponse response = handler.execute(new HttpRequest(GET, "/hello")); assertThat(string(response)).isEqualTo("buddy");