PathVariable consistently reflects value up to 1st ";"

Given "/{foo}" and "/a=42;c=b", previously that would be treated as a
sequence of matrix vars with an empty path variable. After the change
the path variable "foo" is "a=42".

This should be ok for backawards compatibility since it's unlikely for
anything to rely on an empty path variable.

Issue: SPR-11897
This commit is contained in:
Rossen Stoyanchev
2018-07-18 20:56:33 -04:00
parent 9c08a482d1
commit 66d73017d5
3 changed files with 43 additions and 24 deletions

View File

@@ -149,20 +149,23 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
Map<String, MultiValueMap<String, String>> result = new LinkedHashMap<>();
uriVariables.forEach((uriVarKey, uriVarValue) -> {
int equalsIndex = uriVarValue.indexOf('=');
if (equalsIndex == -1) {
return;
}
String matrixVariables;
int semicolonIndex = uriVarValue.indexOf(';');
if ((semicolonIndex == -1) || (semicolonIndex == 0) || (equalsIndex < semicolonIndex)) {
if (semicolonIndex != -1 && semicolonIndex != 0) {
uriVariables.put(uriVarKey, uriVarValue.substring(0, semicolonIndex));
}
String matrixVariables;
if (semicolonIndex == -1 || semicolonIndex == 0 || equalsIndex < semicolonIndex) {
matrixVariables = uriVarValue;
}
else {
matrixVariables = uriVarValue.substring(semicolonIndex + 1);
uriVariables.put(uriVarKey, uriVarValue.substring(0, semicolonIndex));
}
MultiValueMap<String, String> vars = WebUtils.parseMatrixVariables(matrixVariables);

View File

@@ -311,6 +311,7 @@ public class RequestMappingInfoHandlerMappingTests {
MultiValueMap<String, String> matrixVariables;
Map<String, String> uriVariables;
// URI var parsed into path variable + matrix params..
request = new MockHttpServletRequest();
handleMatch(request, "/{cars}", "/cars;colors=red,blue,green;year=2012");
@@ -322,6 +323,7 @@ public class RequestMappingInfoHandlerMappingTests {
assertEquals("2012", matrixVariables.getFirst("year"));
assertEquals("cars", uriVariables.get("cars"));
// URI var with regex for path variable, and URI var for matrix params..
request = new MockHttpServletRequest();
handleMatch(request, "/{cars:[^;]+}{params}", "/cars;colors=red,blue,green;year=2012");
@@ -334,6 +336,7 @@ public class RequestMappingInfoHandlerMappingTests {
assertEquals("cars", uriVariables.get("cars"));
assertEquals(";colors=red,blue,green;year=2012", uriVariables.get("params"));
// URI var with regex for path variable, and (empty) URI var for matrix params..
request = new MockHttpServletRequest();
handleMatch(request, "/{cars:[^;]+}{params}", "/cars");
@@ -343,6 +346,18 @@ public class RequestMappingInfoHandlerMappingTests {
assertNull(matrixVariables);
assertEquals("cars", uriVariables.get("cars"));
assertEquals("", uriVariables.get("params"));
// SPR-11897
request = new MockHttpServletRequest();
handleMatch(request, "/{foo}", "/a=42;b=c");
matrixVariables = getMatrixVariables(request, "foo");
uriVariables = getUriTemplateVariables(request);
assertNotNull(matrixVariables);
assertEquals("42", matrixVariables.getFirst("a"));
assertEquals("c", matrixVariables.getFirst("b"));
assertEquals("a=42", uriVariables.get("foo"));
}
@Test // SPR-10140, SPR-16867