Skip to content

Commit d42a3a8

Browse files
committed
Fix RouteArgs priority (you can override them for a specific call)
1 parent 68c33f2 commit d42a3a8

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

framework/src/play/mvc/Router.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public class Router {
3535
* Pattern used to locate a method override instruction in request.querystring
3636
*/
3737
static Pattern methodOverride = new Pattern("^.*x-http-method-override=({method}GET|PUT|POST|DELETE).*$");
38-
3938
/**
4039
* Timestamp the routes file was last loaded at.
4140
*/
@@ -165,6 +164,11 @@ static void parse(VirtualFile routeFile, String prefix) {
165164
// Mutable map needs to be passed in.
166165
content = TemplateLoader.load(routeFile).render(new HashMap<String, Object>(16));
167166
}
167+
parse(content, prefix, fileAbsolutePath);
168+
}
169+
170+
static void parse(String content, String prefix, String fileAbsolutePath) {
171+
int lineNumber = 0;
168172
for (String line : content.split("\n")) {
169173
lineNumber++;
170174
line = line.trim().replaceAll("\\s+", " ");
@@ -225,7 +229,6 @@ public static void detectChanges(String prefix) {
225229
}
226230
}
227231
}
228-
229232
/**
230233
* All the loaded routes.
231234
*/
@@ -372,7 +375,11 @@ public static ActionDefinition reverse(String action, Map<String, Object> args)
372375
Map<String, Object> argsbackup = new HashMap<String, Object>(args);
373376
// Add routeArgs
374377
if (Scope.RouteArgs.current() != null) {
375-
args.putAll(Scope.RouteArgs.current().data);
378+
for (String key : Scope.RouteArgs.current().data.keySet()) {
379+
if (!args.containsKey(key)) {
380+
args.put(key, Scope.RouteArgs.current().data.get(key));
381+
}
382+
}
376383
}
377384
for (Route route : routes) {
378385
if (route.actionPattern != null) {
@@ -437,14 +444,14 @@ public static ActionDefinition reverse(String action, Map<String, Object> args)
437444
List<Object> vals = (List<Object>) value;
438445
try {
439446
path = path.replaceAll("\\{(<[^>]+>)?" + key + "\\}", URLEncoder.encode(vals.get(0).toString().replace("$", "\\$"), "utf-8"));
440-
} catch(UnsupportedEncodingException e) {
447+
} catch (UnsupportedEncodingException e) {
441448
throw new UnexpectedException(e);
442449
}
443450
} else {
444451
try {
445452
path = path.replaceAll("\\{(<[^>]+>)?" + key + "\\}", URLEncoder.encode(value.toString().replace("$", "\\$"), "utf-8").replace("%3A", ":").replace("%40", "@"));
446453
host = host.replaceAll("\\{(<[^>]+>)?" + key + "\\}", URLEncoder.encode(value.toString().replace("$", "\\$"), "utf-8").replace("%3A", ":").replace("%40", "@"));
447-
} catch(UnsupportedEncodingException e) {
454+
} catch (UnsupportedEncodingException e) {
448455
throw new UnexpectedException(e);
449456
}
450457
}
@@ -506,31 +513,27 @@ public static ActionDefinition reverse(String action, Map<String, Object> args)
506513
}
507514

508515
public static class ActionDefinition {
516+
509517
/**
510518
* The domain/host name.
511519
*/
512520
public String host;
513-
514521
/**
515522
* The HTTP method, e.g. "GET".
516523
*/
517524
public String method;
518-
519525
/**
520526
* @todo - what is this? does it include the domain?
521527
*/
522528
public String url;
523-
524529
/**
525530
* Whether the route contains an astericks *.
526531
*/
527532
public boolean star;
528-
529533
/**
530534
* @todo - what is this? does it include the class and package?
531535
*/
532536
public String action;
533-
534537
/**
535538
* @todo - are these the required args in the routing file, or the query string in a request?
536539
*/
@@ -644,7 +647,7 @@ public void compute() {
644647
if (m.matches()) {
645648
if (this.host.contains("{")) {
646649
String name = m.group(1).replace("{", "").replace("}", "");
647-
if(!name.equals("_")) {
650+
if (!name.equals("_")) {
648651
hostArg = new Arg();
649652
hostArg.name = name;
650653
Logger.trace("hostArg name [" + name + "]");
@@ -703,7 +706,6 @@ public void addParams(String params) {
703706
}
704707

705708
// TODO: Add args names
706-
707709
public void addFormat(String params) {
708710
if (params == null || params.length() < 1) {
709711
return;

0 commit comments

Comments
 (0)