Skip to content

Commit bce7c37

Browse files
GRAILS-11512 - improve the handling of URL mappings which contain hyphens
1 parent 57a6bf5 commit bce7c37

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

grails-plugin-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
@@ -255,11 +255,10 @@ protected Pattern convertToRegex(String url) {
255255
* @see org.codehaus.groovy.grails.web.mapping.UrlMappingInfo
256256
*/
257257
public UrlMappingInfo match(String uri) {
258-
Integer slashCount = org.springframework.util.StringUtils.countOccurrencesOf(uri, "/");
259258
for (Pattern pattern : patterns) {
260259
Matcher m = pattern.matcher(uri);
261260
if (m.matches()) {
262-
UrlMappingInfo urlInfo = createUrlMappingInfo(uri, m, slashCount);
261+
UrlMappingInfo urlInfo = createUrlMappingInfo(uri, m);
263262
if (urlInfo != null) {
264263
return urlInfo;
265264
}
@@ -574,15 +573,16 @@ public UrlMappingData getUrlData() {
574573
}
575574

576575
@SuppressWarnings("unchecked")
577-
private UrlMappingInfo createUrlMappingInfo(String uri, Matcher m, int tokenCount) {
576+
private UrlMappingInfo createUrlMappingInfo(String uri, Matcher m) {
578577
boolean hasOptionalExtension = urlData.hasOptionalExtension();
579578
Map params = new HashMap();
580579
Errors errors = new MapBindingResult(params, "urlMapping");
580+
int groupCount = m.groupCount();
581581
String lastGroup = null;
582-
for (int i = 0, count = m.groupCount(); i < count; i++) {
582+
for (int i = 0; i < groupCount; i++) {
583583
lastGroup = m.group(i + 1);
584584
// if null optional.. ignore
585-
if (i == tokenCount & hasOptionalExtension) {
585+
if (i == groupCount - 1 && hasOptionalExtension) {
586586
ConstrainedProperty cp = constraints[constraints.length-1];
587587
cp.validate(this, lastGroup, errors);
588588

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)

0 commit comments

Comments
 (0)