Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 025ab2e

Browse files
committed
route prefix doesn't work after route is created and in MVC routes fix jooby-project#426
1 parent 8afe64e commit 025ab2e

File tree

5 files changed

+88
-15
lines changed

5 files changed

+88
-15
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.Jooby;
4+
import org.jooby.Route;
5+
import org.jooby.mvc.GET;
6+
import org.jooby.mvc.Path;
7+
import org.jooby.test.ServerFeature;
8+
import org.junit.Test;
9+
10+
public class Issue187 extends ServerFeature {
11+
12+
public static class RouteBean1 {
13+
14+
@GET
15+
@Path("/1/foo")
16+
public String name(final Route route) {
17+
return route.name();
18+
}
19+
}
20+
21+
public static class RouteBean2 {
22+
23+
@GET
24+
@Path("/2/foo")
25+
public String name(final Route route) {
26+
return route.name();
27+
}
28+
}
29+
30+
public static class App extends Jooby {
31+
32+
public App(final String prefix) {
33+
super(prefix);
34+
35+
get("/foo", req -> req.route().name())
36+
.name("foo");
37+
38+
use(RouteBean2.class);
39+
}
40+
41+
}
42+
43+
public Issue187() {
44+
super("187");
45+
46+
use(new App("bar"));
47+
48+
use(RouteBean1.class);
49+
}
50+
51+
@Test
52+
public void renameRoutesViaPrefix() throws Exception {
53+
request().get("/foo")
54+
.expect("/bar/foo");
55+
56+
request().get("/1/foo")
57+
.expect("/187/RouteBean1.name");
58+
59+
request().get("/2/foo")
60+
.expect("/bar/RouteBean2.name");
61+
}
62+
63+
}

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,12 @@ private static class MvcClass implements Route.Props<MvcClass> {
524524

525525
private Mapper<?> mapper;
526526

527-
public MvcClass(final Class<?> routeClass, final String path) {
527+
private String prefix;
528+
529+
public MvcClass(final Class<?> routeClass, final String path, final String prefix) {
528530
this.routeClass = routeClass;
529531
this.path = path;
532+
this.prefix = prefix;
530533
}
531534

532535
@Override
@@ -570,6 +573,9 @@ public Route.Definition apply(final Route.Definition route) {
570573
if (name != null) {
571574
route.name(name);
572575
}
576+
if (prefix != null) {
577+
route.name(prefix + "/" + route.name());
578+
}
573579
if (consumes != null) {
574580
route.consumes(consumes);
575581
}
@@ -710,7 +716,7 @@ private Jooby use(final Optional<String> path, final Jooby app) {
710716
} else if (it instanceof Route.Group) {
711717
((Route.Group) it).routes().forEach(r -> this.bag.add(rewrite.apply(r)));
712718
} else if (it instanceof MvcClass) {
713-
Object routes = path.<Object> map(p -> new MvcClass(((MvcClass) it).routeClass, p))
719+
Object routes = path.<Object> map(p -> new MvcClass(((MvcClass) it).routeClass, p, prefix))
714720
.orElse(it);
715721
this.bag.add(routes);
716722
} else {
@@ -3235,7 +3241,7 @@ public Route.Definition assets(final String path, final AssetHandler handler) {
32353241
@Override
32363242
public Route.Collection use(final Class<?> routeClass) {
32373243
requireNonNull(routeClass, "Route class is required.");
3238-
MvcClass mvc = new MvcClass(routeClass, "");
3244+
MvcClass mvc = new MvcClass(routeClass, "", prefix);
32393245
bag.add(mvc);
32403246
return new Route.Collection(mvc);
32413247
}
@@ -3247,9 +3253,9 @@ public Route.Collection use(final Class<?> routeClass) {
32473253
* @return The same route definition.
32483254
*/
32493255
private Route.Definition appendDefinition(final Route.Definition route) {
3250-
if (prefix != null) {
3251-
route.name(prefix + "/" + route.name());
3252-
}
3256+
route.prefix = prefix;
3257+
// reset name will update the name if prefix != null
3258+
route.name(route.name());
32533259
bag.add(route);
32543260
return route;
32553261
}
@@ -3745,13 +3751,8 @@ private static List<Object> normalize(final List<Object> services, final Env env
37453751
MvcClass mvcRoute = ((MvcClass) candidate);
37463752
Class<?> mvcClass = mvcRoute.routeClass;
37473753
String path = ((MvcClass) candidate).path;
3748-
MvcRoutes.routes(env, classInfo, path, mvcClass).forEach(route -> {
3749-
mvcRoute.apply(route);
3750-
if (prefix != null) {
3751-
route.name(prefix + "/" + route.name());
3752-
}
3753-
result.add(route);
3754-
});
3754+
MvcRoutes.routes(env, classInfo, path, mvcClass)
3755+
.forEach(route -> result.add(mvcRoute.apply(route)));
37553756
} else {
37563757
result.add(candidate);
37573758
}

jooby/src/main/java/org/jooby/Route.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,8 @@ class Definition implements Props<Definition> {
10561056

10571057
private String declaringClass;
10581058

1059+
String prefix;
1060+
10591061
/**
10601062
* Creates a new route definition.
10611063
*
@@ -1293,7 +1295,7 @@ public String name() {
12931295
@Override
12941296
public Definition name(final String name) {
12951297
checkArgument(!Strings.isNullOrEmpty(name), "A route's name is required.");
1296-
this.name = normalize(name);
1298+
this.name = normalize(prefix != null ? prefix + "/" + name : name);
12971299
return this;
12981300
}
12991301

jooby/src/test/java/org/jooby/test/JoobyRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private void prepare(final Class<?> klass, final Class<?> server) throws Initial
8080
if (!Jooby.class.isAssignableFrom(appClass)) {
8181
throw new InitializationError("Invalid jooby app: " + appClass);
8282
}
83-
int processors = Math.max(1,Runtime.getRuntime().availableProcessors());
83+
int processors = Math.max(1, Runtime.getRuntime().availableProcessors());
8484
// required by Jetty (processors * 2, 1(http), 1(https), 1(request)
8585
int maxThreads = processors * 2 + 3;
8686
Config config = ConfigFactory.empty("test-config")

jooby/src/test/java/org/jooby/test/ServerFeature.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public abstract class ServerFeature extends Jooby {
3030

3131
private Client server = null;
3232

33+
public ServerFeature(final String prefix) {
34+
super(prefix);
35+
}
36+
37+
public ServerFeature() {
38+
}
39+
3340
@Before
3441
public void debug() {
3542
if (DEBUG) {

0 commit comments

Comments
 (0)