Skip to content

Commit 69281b1

Browse files
committed
Extending routes fix jooby-project#365
1 parent b0aaa6f commit 69281b1

4 files changed

Lines changed: 72 additions & 30 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.mvc.GET;
4+
import org.jooby.mvc.Path;
5+
import org.jooby.test.ServerFeature;
6+
import org.junit.Test;
7+
8+
public class Issue365 extends ServerFeature {
9+
10+
public static class Base {
11+
12+
@GET
13+
public String list() {
14+
return "base.list";
15+
}
16+
17+
@GET
18+
@Path("/:id")
19+
public String findById(final String id) {
20+
return "base.findById";
21+
}
22+
23+
}
24+
25+
@Path("/api/users")
26+
public static class Users extends Base {
27+
28+
@Override
29+
@GET
30+
public String list() {
31+
return "users.list";
32+
}
33+
34+
@GET
35+
@Path("/q/:q")
36+
public String query(final String q) {
37+
return "users.query";
38+
}
39+
40+
}
41+
42+
{
43+
use(Users.class);
44+
}
45+
46+
@Test
47+
public void list() throws Exception {
48+
request()
49+
.get("/api/users/1")
50+
.expect("base.findById");
51+
52+
request()
53+
.get("/api/users")
54+
.expect("users.list");
55+
56+
request()
57+
.get("/api/users/q/q")
58+
.expect("users.query");
59+
}
60+
61+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3473,8 +3473,7 @@ private String configTree(final String[] sources, final int i) {
34733473
}
34743474

34753475
private static List<Object> normalize(final List<Object> services, final Env env,
3476-
final RouteMetadata classInfo,
3477-
final String prefix) {
3476+
final RouteMetadata classInfo, final String prefix) {
34783477
List<Object> result = new ArrayList<>();
34793478
List<Object> snapshot = services;
34803479
/** modules, routes, parsers, renderers and websockets */

jooby/src/main/java/org/jooby/internal/mvc/MvcRoutes.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.lang.annotation.Annotation;
2222
import java.lang.reflect.AnnotatedElement;
2323
import java.lang.reflect.Method;
24-
import java.lang.reflect.Modifier;
2524
import java.util.ArrayList;
2625
import java.util.HashMap;
2726
import java.util.LinkedHashMap;
@@ -82,7 +81,7 @@ public static List<Route.Definition> routes(final Env env, final RouteMetadata c
8281
String[] rootExcludes = excludes(routeClass, EMPTY);
8382

8483
Map<Method, List<Class<?>>> methods = new HashMap<>();
85-
for (Method method : routeClass.getDeclaredMethods()) {
84+
for (Method method : routeClass.getMethods()) {
8685
List<Class<?>> annotations = new ArrayList<>();
8786
for (Class annotationType : VERBS) {
8887
Annotation annotation = method.getAnnotation(annotationType);
@@ -91,9 +90,6 @@ public static List<Route.Definition> routes(final Env env, final RouteMetadata c
9190
}
9291
}
9392
if (annotations.size() > 0) {
94-
if (!Modifier.isPublic(method.getModifiers())) {
95-
throw new IllegalArgumentException("Not a public method: " + method);
96-
}
9793
methods.put(method, annotations);
9894
}
9995
}

jooby/src/test/java/org/jooby/internal/mvc/MvcRoutesTest.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,17 @@
22

33
import static org.easymock.EasyMock.expect;
44

5+
import java.util.List;
6+
57
import org.jooby.Env;
8+
import org.jooby.Route.Definition;
69
import org.jooby.internal.RouteMetadata;
710
import org.jooby.mvc.GET;
8-
import org.jooby.mvc.Path;
911
import org.jooby.test.MockUnit;
1012
import org.junit.Test;
1113

1214
public class MvcRoutesTest {
1315

14-
@Path("/")
15-
public static class NoPublicMethod {
16-
17-
@GET
18-
private void privateMethod() {
19-
20-
}
21-
22-
}
23-
2416
public static class NoPath {
2517

2618
@GET
@@ -30,17 +22,10 @@ public void nopath() {
3022

3123
}
3224

33-
@Test(expected = IllegalArgumentException.class)
34-
public void noPublicMethod() throws Exception {
35-
new MockUnit(Env.class)
36-
.expect(unit -> {
37-
Env env = unit.get(Env.class);
38-
expect(env.name()).andReturn("dev");
39-
})
40-
.run(unit -> {
41-
Env env = unit.get(Env.class);
42-
MvcRoutes.routes(env, new RouteMetadata(env), "", NoPublicMethod.class);
43-
});
25+
@Test
26+
public void emptyConstructor() throws Exception {
27+
new MvcRoutes();
28+
4429
}
4530

4631
@Test(expected = IllegalArgumentException.class)
@@ -52,7 +37,8 @@ public void nopath() throws Exception {
5237
})
5338
.run(unit -> {
5439
Env env = unit.get(Env.class);
55-
MvcRoutes.routes(env, new RouteMetadata(env), "", NoPath.class);
40+
List<Definition> routes = MvcRoutes.routes(env, new RouteMetadata(env), "", NoPath.class);
41+
System.out.println(routes);
5642
});
5743
}
5844
}

0 commit comments

Comments
 (0)