Skip to content

Commit 5fe3362

Browse files
joerg1985diemol
andauthored
[grid] flatten combined routes to improve routing (#13856)
Co-authored-by: Diego Molina <[email protected]>
1 parent 17d927b commit 5fe3362

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

java/src/org/openqa/selenium/remote/http/Route.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import static org.openqa.selenium.remote.http.HttpMethod.POST;
2828
import static org.openqa.selenium.remote.http.UrlPath.ROUTE_PREFIX_KEY;
2929

30+
import java.util.ArrayList;
3031
import java.util.Collections;
31-
import java.util.LinkedList;
3232
import java.util.List;
3333
import java.util.Map;
3434
import java.util.function.Function;
@@ -292,7 +292,7 @@ private HttpRequest transform(HttpRequest request) {
292292
// Don't forget to register our prefix
293293
Object rawPrefixes = request.getAttribute(ROUTE_PREFIX_KEY);
294294
if (!(rawPrefixes instanceof List)) {
295-
rawPrefixes = new LinkedList<>();
295+
rawPrefixes = Collections.emptyList();
296296
}
297297
List<String> prefixes =
298298
Stream.concat(((List<?>) rawPrefixes).stream(), Stream.of(prefix))
@@ -321,7 +321,21 @@ private static class CombinedRoute extends Route {
321321
private CombinedRoute(Stream<Routable> routes) {
322322
// We want later routes to have a greater chance of being called so that we can override
323323
// routes as necessary.
324-
List<Routable> routables = routes.collect(Collectors.toList());
324+
List<Routable> routables =
325+
routes
326+
.flatMap(
327+
route -> {
328+
// flatten a nested CombinedRoute
329+
if (route instanceof CombinedRoute) {
330+
List<Routable> nestedRoutes =
331+
new ArrayList<>(((CombinedRoute) route).allRoutes);
332+
// reverse to have the identical behaviour like not flattened
333+
Collections.reverse(nestedRoutes);
334+
return nestedRoutes.stream();
335+
}
336+
return Stream.of(route);
337+
})
338+
.collect(Collectors.toList());
325339
Collections.reverse(routables);
326340
allRoutes = List.copyOf(routables);
327341
Require.stateCondition(!allRoutes.isEmpty(), "At least one route must be specified.");

java/test/org/openqa/selenium/remote/http/RouteTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,11 @@ void laterRoutesOverrideEarlierRoutesToFacilitateOverridingRoutes() {
152152
HttpHandler handler =
153153
Route.combine(
154154
Route.get("/hello").to(() -> req -> new HttpResponse().setContent(utf8String("world"))),
155-
Route.get("/hello")
156-
.to(() -> req -> new HttpResponse().setContent(utf8String("buddy"))));
155+
Route.combine(
156+
Route.get("/hello")
157+
.to(() -> req -> new HttpResponse().setContent(utf8String("world"))),
158+
Route.get("/hello")
159+
.to(() -> req -> new HttpResponse().setContent(utf8String("buddy")))));
157160

158161
HttpResponse response = handler.execute(new HttpRequest(GET, "/hello"));
159162
assertThat(string(response)).isEqualTo("buddy");

0 commit comments

Comments
 (0)