Skip to content

Commit 6993fe8

Browse files
GRAILS-11512 - improve the handling of URL mappings which contain hyphens
1 parent 7e45ece commit 6993fe8

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMappingTests.groovy

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,38 @@ mappings {
6565
controller = 'reporting'
6666
action = 'view'
6767
}
68+
"/$first-alpha-$second/$third-beta-$fourth-foo(.$format)?" {
69+
controller = 'hyphenTests'
70+
action = 'view'
71+
}
6872
}
6973
'''
74+
75+
void testHyphenDelimiters() {
76+
def holder = new DefaultUrlMappingsHolder(evaluator.evaluateMappings(new ByteArrayResource(mappingScript.bytes)))
77+
78+
def info = holder.match("/one-alpha-two/three-beta-four-foo.json")
79+
assertNotNull info
80+
assertEquals 'hyphenTests', info.controllerName
81+
assertEquals 'view', info.actionName
82+
assertEquals 'one', info.params.first
83+
assertEquals 'two', info.params.second
84+
assertEquals 'three', info.params.third
85+
assertEquals 'four', info.params.fourth
86+
assertEquals 'json', info.params.format
87+
88+
89+
info = holder.match("/one-alpha-two/three-beta-four-foo")
90+
assertNotNull info
91+
assertEquals 'hyphenTests', info.controllerName
92+
assertEquals 'view', info.actionName
93+
assertEquals 'one', info.params.first
94+
assertEquals 'two', info.params.second
95+
assertEquals 'three', info.params.third
96+
assertEquals 'four', info.params.fourth
97+
assertNull info.params.format
98+
}
99+
70100
void testMaptoURI() {
71101
def res = new ByteArrayResource(mappingScript.bytes)
72102
def mappings = evaluator.evaluateMappings(res)
@@ -372,7 +402,7 @@ mappings {
372402
assertEquals 'view', info.actionName
373403
assertEquals 'my.id', info.params.foo
374404
}
375-
405+
376406
void testInit() {
377407
def parser = new DefaultUrlMappingParser()
378408
def m = new RegexUrlMapping(parser.parse("/(*)/hello"), "test", null, null, null, null, null, UrlMapping.ANY_VERSION,[] as ConstrainedProperty[], servletContext)

grails-web-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,10 @@ protected Pattern convertToRegex(String url) {
256256
* @see org.codehaus.groovy.grails.web.mapping.UrlMappingInfo
257257
*/
258258
public UrlMappingInfo match(String uri) {
259-
Integer slashCount = org.springframework.util.StringUtils.countOccurrencesOf(uri, "/");
260259
for (Pattern pattern : patterns) {
261260
Matcher m = pattern.matcher(uri);
262261
if (m.matches()) {
263-
UrlMappingInfo urlInfo = createUrlMappingInfo(uri, m, slashCount);
262+
UrlMappingInfo urlInfo = createUrlMappingInfo(uri, m);
264263
if (urlInfo != null) {
265264
return urlInfo;
266265
}
@@ -575,15 +574,16 @@ public UrlMappingData getUrlData() {
575574
}
576575

577576
@SuppressWarnings("unchecked")
578-
private UrlMappingInfo createUrlMappingInfo(String uri, Matcher m, int tokenCount) {
577+
private UrlMappingInfo createUrlMappingInfo(String uri, Matcher m) {
579578
boolean hasOptionalExtension = urlData.hasOptionalExtension();
580579
Map params = new HashMap();
581580
Errors errors = new MapBindingResult(params, "urlMapping");
581+
int groupCount = m.groupCount();
582582
String lastGroup = null;
583-
for (int i = 0, count = m.groupCount(); i < count; i++) {
583+
for (int i = 0; i < groupCount; i++) {
584584
lastGroup = m.group(i + 1);
585585
// if null optional.. ignore
586-
if (i == tokenCount & hasOptionalExtension) {
586+
if (i == groupCount - 1 && hasOptionalExtension) {
587587
ConstrainedProperty cp = constraints[constraints.length-1];
588588
cp.validate(this, lastGroup, errors);
589589

0 commit comments

Comments
 (0)