Skip to content

Commit b317869

Browse files
author
ipolevoy
committed
#400 Routing error in case controller and package has the same names
1 parent a736942 commit b317869

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

activeweb/src/main/java/org/javalite/activeweb/Router.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import static org.javalite.activeweb.ControllerFactory.createControllerInstance;
2727
import static org.javalite.activeweb.ControllerFactory.getControllerClassName;
28-
import static org.javalite.common.Collections.map;
2928

3029
/**
3130
* Responsible for looking at a URI and creating a route to controller if one is found.
@@ -298,7 +297,7 @@ protected ControllerPath getControllerPath(String uri) {
298297
return new ControllerPath();
299298
} else {
300299
String controllerPackage;
301-
if ((controllerPackage = findPackagePrefix(uri)) != null) {
300+
if ((controllerPackage = findPackageSuffix(uri)) != null) {
302301
String controllerName = findControllerNamePart(controllerPackage, uri);
303302
return new ControllerPath(controllerName, controllerPackage);
304303
} else {
@@ -354,7 +353,7 @@ protected static String findControllerNamePart(String pack, String uri) {
354353
temp = temp.substring(pack.length() + 1);
355354

356355
if (temp.equals("") || temp.equals(pack))
357-
throw new ControllerException("You defined a controller package '" + pack + "', but this request does not specify controller name");
356+
throw new ControllerException("You defined a controller package '" + pack + "', but did not specify controller name");
358357

359358
return temp.split("\\.")[0];
360359
}
@@ -366,7 +365,7 @@ protected static String findControllerNamePart(String pack, String uri) {
366365
* @return a part of a package name which can be found in between "app.controllers" and short name of class, or null
367366
* if not found
368367
*/
369-
protected String findPackagePrefix(String uri) {
368+
protected String findPackageSuffix(String uri) {
370369

371370
String temp = uri.startsWith("/") ? uri.substring(1) : uri;
372371
temp = temp.replace(".", "_");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package app.controllers;
2+
3+
import org.javalite.activeweb.AppController;
4+
5+
/**
6+
* @author igor on 9/26/18.
7+
*/
8+
public class ApiController extends AppController {
9+
10+
public void index() {
11+
respond("ApiController#index");
12+
}
13+
14+
}

activeweb/src/test/java/org/javalite/activeweb/RouterControllerPathSpec.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public void before(){
3636

3737
@Test
3838
public void shouldFindHomeControllerFromPath() {
39-
40-
4139
a(router.getControllerPath("/").getControllerName()).shouldBeEqual("home");
4240
a(router.getControllerPath("/").getControllerPackage()).shouldBeNull();
4341
a(router.getControllerPath("/hello").getControllerName()).shouldBeEqual("hello");

activeweb/src/test/java/org/javalite/activeweb/RouterCustomSpec.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,19 @@ public void init(AppContext appContext) {
325325
the(responseContent()).shouldNotContain("IndexOutOfBoundsException");
326326
the(responseContent()).shouldContain("TestController#index");
327327
}
328+
329+
@Test
330+
public void should_issue400() {
331+
332+
routeConfig = new AbstractRouteConfig() {
333+
public void init(AppContext appContext) {
334+
route("/api").to(ApiController.class).action("index").get();
335+
336+
}
337+
};
338+
339+
request.setServletPath("/api");
340+
execDispatcher();
341+
System.out.println(responseContent());
342+
}
328343
}

0 commit comments

Comments
 (0)