Skip to content

Commit deaa262

Browse files
committed
Not able to route double slash in URI path routing jooby-project#1409
- we don`t change paths on developer end anymore (excpet for some minor massage on `/`)
1 parent 2b6e094 commit deaa262

7 files changed

Lines changed: 63 additions & 132 deletions

File tree

‎examples/pom.xml‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,6 @@
5656
<artifactId>jooby-netty</artifactId>
5757
<version>${jooby.version}</version>
5858
</dependency>
59-
<dependency>
60-
<groupId>io.jooby</groupId>
61-
<artifactId>jooby-pac4j</artifactId>
62-
<version>${jooby.version}</version>
63-
</dependency>
64-
<!-- https://mvnrepository.com/artifact/org.pac4j/pac4j-oauth -->
65-
<dependency>
66-
<groupId>org.pac4j</groupId>
67-
<artifactId>pac4j-oauth</artifactId>
68-
<version>${pac4j.version}</version>
69-
</dependency>
70-
<!-- https://mvnrepository.com/artifact/org.pac4j/pac4j-oidc -->
71-
<dependency>
72-
<groupId>org.pac4j</groupId>
73-
<artifactId>pac4j-oidc</artifactId>
74-
<version>${pac4j.version}</version>
75-
</dependency>
7659
<dependency>
7760
<groupId>io.jooby</groupId>
7861
<artifactId>jooby-hikari</artifactId>

‎examples/src/main/java/examples/Pac4jApp.java‎

Lines changed: 0 additions & 92 deletions
This file was deleted.

‎jooby/src/main/java/io/jooby/internal/RouterImpl.java‎

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,27 @@
5454
import java.util.stream.IntStream;
5555
import java.util.stream.Stream;
5656

57-
import static io.jooby.Router.noTrailingSlash;
58-
5957
public class RouterImpl implements Router {
6058

6159
private static class PathBuilder {
62-
private StringBuilder buffer = new StringBuilder();
63-
private String previous;
60+
private StringBuilder buffer;
61+
62+
public PathBuilder(String... path) {
63+
Stream.of(path).forEach(this::append);
64+
}
6465

6566
public PathBuilder append(String path) {
66-
if (previous == null) {
67-
buffer.append(path);
68-
previous = path;
69-
} else if (!path.equals("/")) {
67+
if (!path.equals("/")) {
68+
if (buffer == null) {
69+
buffer = new StringBuilder();
70+
}
7071
buffer.append(path);
71-
previous = path;
7272
}
73-
7473
return this;
7574
}
7675

7776
@Override public String toString() {
78-
return buffer.toString();
77+
return buffer == null ? "/" : buffer.toString();
7978
}
8079
}
8180

@@ -226,7 +225,7 @@ public Router use(@Nonnull Predicate<Context> predicate, @Nonnull Router router)
226225
}
227226
predicateMap.put(predicate, tree);
228227
for (Route route : router.getRoutes()) {
229-
Route newRoute = defineRoute(route.getMethod(), route.getPattern(), route.getHandler(),
228+
Route newRoute = newRoute(route.getMethod(), route.getPattern(), route.getHandler(),
230229
tree);
231230
copy(route, newRoute);
232231
}
@@ -235,20 +234,17 @@ public Router use(@Nonnull Predicate<Context> predicate, @Nonnull Router router)
235234

236235
@Nonnull @Override public Router use(@Nonnull String path, @Nonnull Router router) {
237236
String prefix = Router.path(path);
238-
if (prefix.equals("/")) {
239-
prefix = "";
240-
}
241237
for (Route route : router.getRoutes()) {
242-
String routePattern = prefix + route.getPattern();
243-
Route newRoute = defineRoute(route.getMethod(), routePattern, route.getHandler(), chi);
238+
String routePattern = new PathBuilder(prefix, route.getPattern()).toString();
239+
Route newRoute = newRoute(route.getMethod(), routePattern, route.getHandler(), chi);
244240
copy(route, newRoute);
245241
}
246242
return this;
247243
}
248244

249245
@Nonnull @Override
250246
public Router use(@Nonnull Router router) {
251-
return use("", router);
247+
return use("/", router);
252248
}
253249

254250
@Nonnull @Override public Router mvc(@Nonnull Object router) {
@@ -375,10 +371,10 @@ public Route ws(@Nonnull String pattern, @Nonnull WebSocket.Initializer handler)
375371
@Override
376372
public Route route(@Nonnull String method, @Nonnull String pattern,
377373
@Nonnull Route.Handler handler) {
378-
return defineRoute(method, pattern, handler, chi);
374+
return newRoute(method, pattern, handler, chi);
379375
}
380376

381-
private Route defineRoute(@Nonnull String method, @Nonnull String pattern,
377+
private Route newRoute(@Nonnull String method, @Nonnull String pattern,
382378
@Nonnull Route.Handler handler, RouteTree tree) {
383379
/** Pattern: */
384380
PathBuilder pathBuilder = new PathBuilder();
@@ -420,7 +416,9 @@ private Route defineRoute(@Nonnull String method, @Nonnull String pattern,
420416
routeExecutor.put(route, stack.executor);
421417
}
422418

423-
String finalPattern = basePath == null ? safePattern : basePath + safePattern;
419+
String finalPattern = basePath == null
420+
? safePattern
421+
: new PathBuilder(basePath, safePattern).toString();
424422

425423
if (route.getMethod().equals(WS)) {
426424
tree.insert(GET, finalPattern, route);

‎modules/jooby-apt/src/main/java/io/jooby/apt/JoobyProcessor.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.jooby.apt;
77

88
import io.jooby.MvcFactory;
9+
import io.jooby.Router;
910
import io.jooby.SneakyThrows;
1011
import io.jooby.internal.apt.FactoryCompiler;
1112
import io.jooby.internal.apt.HandlerCompiler;
@@ -156,7 +157,7 @@ private List<String> path(TypeElement method, ExecutableElement exec) {
156157
return prefix.isEmpty() ? Collections.singletonList("/") : prefix;
157158
}
158159
return prefix.stream()
159-
.flatMap(root -> methodPath.stream().map(p -> root + p))
160+
.flatMap(root -> methodPath.stream().map(p -> root.equals("/") ? p : root + p))
160161
.distinct()
161162
.collect(Collectors.toList());
162163
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package source;
2+
3+
import io.jooby.Route;
4+
import io.jooby.annotations.GET;
5+
import io.jooby.annotations.Path;
6+
7+
@Path("/")
8+
public class RouteContextPath {
9+
10+
@GET
11+
public Route route(Route route) {
12+
return route;
13+
}
14+
15+
@GET("/subpath")
16+
public Route subpath(Route route) {
17+
return route;
18+
}
19+
}

‎modules/jooby-apt/src/test/java/tests/HandlerCompilerTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import source.NoPathRoute;
2020
import source.NullRoutes;
2121
import source.Provisioning;
22+
import source.RouteContextPath;
2223
import source.RouteInjection;
2324

2425
import java.io.InputStream;
@@ -458,6 +459,20 @@ public void routeParam() throws Exception {
458459
;
459460
}
460461

462+
@Test
463+
public void contextPath() throws Exception {
464+
new MvcHandlerCompilerRunner(new RouteContextPath())
465+
.compile("/", handler -> {
466+
Route route = mock(Route.class);
467+
assertEquals(route, handler.apply(MockContextHelper.mockContext().setRoute(route)));
468+
})
469+
.compile("/subpath", handler -> {
470+
Route route = mock(Route.class);
471+
assertEquals(route, handler.apply(MockContextHelper.mockContext().setRoute(route)));
472+
})
473+
;
474+
}
475+
461476
private Context mockContext(String method, String path) {
462477
Context ctx = mock(Context.class);
463478
when(ctx.getMethod()).thenReturn(method);

‎tests/src/test/java/io/jooby/FeaturedTest.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,12 +1080,19 @@ public void completableFuture(ServerTestRunner runner) {
10801080
}
10811081

10821082
@ServerTest
1083-
public void basePath(ServerTestRunner runner) {
1083+
public void contextPath(ServerTestRunner runner) {
10841084
runner.define(app -> {
10851085
app.setContextPath("/foo");
1086+
1087+
app.get("/", ctx -> ctx.pathString());
1088+
10861089
app.get("/bar", ctx -> ctx.pathString());
10871090

10881091
}).ready(client -> {
1092+
client.get("/foo", rsp -> {
1093+
assertEquals("/foo", rsp.body().string());
1094+
});
1095+
10891096
client.get("/foo/bar", rsp -> {
10901097
assertEquals("/foo/bar", rsp.body().string());
10911098
});

0 commit comments

Comments
 (0)