From 8e2b99eee09192dcda78a93f28b5ce9e0cdc99a0 Mon Sep 17 00:00:00 2001 From: Jeff Scott Brown Date: Tue, 8 Oct 2013 10:47:31 -0500 Subject: [PATCH 1/2] GRAILS-10500 - fix url mapping support for parameters that include a dot Only exclude dots from url elements if the element is immediately followed by a dot. This is related to https://github.com/grails/grails-core/commit/20aa322ee7a94d6b419532bdc452bd302e1b0284#diff-fc4e5a151130dd0c3eaaf8838e48664fR198 --- .../grails/web/mapping/RegexUrlMapping.java | 10 ++++++---- .../web/mapping/RegexUrlMappingTests.groovy | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/grails-plugin-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java b/grails-plugin-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java index 73b04d38b53..2769bfd580c 100644 --- a/grails-plugin-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java +++ b/grails-plugin-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java @@ -220,7 +220,6 @@ protected Pattern convertToRegex(String url) { String urlRoot = lastSlash > -1 ? pattern.substring(0, lastSlash) : pattern; String urlEnd = lastSlash > -1 ? pattern.substring(lastSlash, pattern.length()) : ""; - // Now replace "*" with "[^/]" and "**" with ".*". pattern = "^" + urlRoot .replace("(\\.(*))", "\\.?([^/]+)?") @@ -230,9 +229,12 @@ protected Pattern convertToRegex(String url) { pattern += urlEnd .replace("(\\.(*))", "\\.?([^/]+)?") - .replaceAll("([^\\*])\\*([^\\*])", "$1[^/\\.]+$2") - .replaceAll("([^\\*])\\*$", "$1[^/\\.]+") - .replaceAll("\\*\\*", ".*"); + .replaceAll("([^\\*])\\*([^\\*])", "$1[^/]+$2") + .replaceAll("([^\\*])\\*$", "$1[^/]+") + .replaceAll("\\*\\*", ".*") +// .replaceAll("\\(\\[\\^\\/\\]\\+\\)\\\\\\.", "([^/.]+)\\\\.") + .replaceAll("\\(\\[\\^\\/\\]\\+\\)\\?\\\\\\.", "([^/.]+)\\?\\\\.") + ; pattern += "/??$"; regex = Pattern.compile(pattern); diff --git a/grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMappingTests.groovy b/grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMappingTests.groovy index a72f77301da..381d5bbc852 100644 --- a/grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMappingTests.groovy +++ b/grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMappingTests.groovy @@ -61,6 +61,10 @@ mappings { controller = "survey" action = "viewByName" } + "/reports/$foo" { + controller = 'reporting' + action = 'view' + } } ''' void testMaptoURI() { @@ -352,6 +356,22 @@ mappings { assertEquals 'survey', info.controllerName assertEquals 'viewByName', info.actionName } + + void testParameterContainingADot() { + def holder = new DefaultUrlMappingsHolder(evaluator.evaluateMappings(new ByteArrayResource(mappingScript.bytes))) + + def info = holder.match("/reports/my") + assertNotNull info + assertEquals 'reporting', info.controllerName + assertEquals 'view', info.actionName + assertEquals 'my', info.params.foo + + info = holder.match("/reports/my.id") + assertNotNull info + assertEquals 'reporting', info.controllerName + assertEquals 'view', info.actionName + assertEquals 'my.id', info.params.foo + } void testInit() { def parser = new DefaultUrlMappingParser() From c92d32261e7d992eb9aaddfddf750cba26447f70 Mon Sep 17 00:00:00 2001 From: Jeff Scott Brown Date: Tue, 8 Oct 2013 20:34:44 -0500 Subject: [PATCH 2/2] GRAILS-10500 - fix url mapping support for parameters that include a dot Only exclude dots from url elements if the element is immediately followed by a dot. This is related to https://github.com/grails/grails-core/commit/20aa322ee7a94d6b419532bdc452bd302e1b0284#diff-fc4e5a151130dd0c3eaaf8838e48664fR198 --- .../grails/web/mapping/RegexUrlMapping.java | 2 +- .../UrlMappingsWithOptionalExtensionSpec.groovy | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/grails-plugin-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java b/grails-plugin-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java index 2769bfd580c..bceef6cfdae 100644 --- a/grails-plugin-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java +++ b/grails-plugin-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java @@ -232,7 +232,7 @@ protected Pattern convertToRegex(String url) { .replaceAll("([^\\*])\\*([^\\*])", "$1[^/]+$2") .replaceAll("([^\\*])\\*$", "$1[^/]+") .replaceAll("\\*\\*", ".*") -// .replaceAll("\\(\\[\\^\\/\\]\\+\\)\\\\\\.", "([^/.]+)\\\\.") + .replaceAll("\\(\\[\\^\\/\\]\\+\\)\\\\\\.", "([^/.]+)\\\\.") .replaceAll("\\(\\[\\^\\/\\]\\+\\)\\?\\\\\\.", "([^/.]+)\\?\\\\.") ; diff --git a/grails-plugin-url-mappings/src/test/groovy/org/codehaus/groovy/grails/web/mapping/UrlMappingsWithOptionalExtensionSpec.groovy b/grails-plugin-url-mappings/src/test/groovy/org/codehaus/groovy/grails/web/mapping/UrlMappingsWithOptionalExtensionSpec.groovy index db6ec4dbf66..83a95a98bac 100644 --- a/grails-plugin-url-mappings/src/test/groovy/org/codehaus/groovy/grails/web/mapping/UrlMappingsWithOptionalExtensionSpec.groovy +++ b/grails-plugin-url-mappings/src/test/groovy/org/codehaus/groovy/grails/web/mapping/UrlMappingsWithOptionalExtensionSpec.groovy @@ -34,7 +34,7 @@ class UrlMappingRsWithOptionalExtensionSpec extends AbstractUrlMappingsSpec { } - void "Test that dynamic URL mappings can be specified with an optional extension"() { + void "Test that dynamic URL mappings can be specified with an optional parameter and an optional extension"() { given:"A URL mapping with an optional extension" def urlMappingsHolder = getUrlMappingsHolder { "/$controller/$action?(.$format)?"() @@ -46,8 +46,19 @@ class UrlMappingRsWithOptionalExtensionSpec extends AbstractUrlMappingsSpec { urlMappingsHolder.match('/book') urlMappingsHolder.match('/book/list') urlMappingsHolder.match('/book/list').parameters.format == null + } - + void "Test that dynamic URL mappings can be specified with a required parameter and an optional extension"() { + given:"A URL mapping with an optional extension" + def urlMappingsHolder = getUrlMappingsHolder { + "/$controller/$action(.$format)?"() + } + + expect:"URLs with and without the format specified match" + urlMappingsHolder.match('/book/list.xml') + urlMappingsHolder.match('/book/list.xml').parameters.format == 'xml' + urlMappingsHolder.match('/book/list') + urlMappingsHolder.match('/book/list').parameters.format == null } void "Test deep dynamic URL mappings can be specified with an optional extension"() {