Browse Source

Refine UriTemplate match pattern

The match/matches methods of UriTemplate use a regex with (.*) in place
of URI variables, which work fine except in the end where such a
pattern can match greedily more than one segment.

This commit updates the regex to use ([^/]*) instead since URI
variables are only meant to be used within a single path segment.

Issue: SPR-16169
pull/1503/merge
Rossen Stoyanchev 7 years ago
parent
commit
c60313de3f
  1. 2
      spring-web/src/main/java/org/springframework/web/util/UriTemplate.java
  2. 14
      spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java

2
spring-web/src/main/java/org/springframework/web/util/UriTemplate.java

@ -212,7 +212,7 @@ public class UriTemplate implements Serializable { @@ -212,7 +212,7 @@ public class UriTemplate implements Serializable {
String variable = builder.toString();
int idx = variable.indexOf(':');
if (idx == -1) {
pattern.append("(.*)");
pattern.append("([^/]*)");
variableNames.add(variable);
}
else {

14
spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java

@ -153,9 +153,7 @@ public class UriTemplateTests { @@ -153,9 +153,7 @@ public class UriTemplateTests {
assertEquals("Invalid match", expected, result);
}
// SPR-13627
@Test
@Test // SPR-13627
public void matchCustomRegexWithNestedCurlyBraces() throws Exception {
UriTemplate template = new UriTemplate("/site.{domain:co.[a-z]{2}}");
Map<String, String> result = template.match("/site.co.eu");
@ -180,6 +178,12 @@ public class UriTemplateTests { @@ -180,6 +178,12 @@ public class UriTemplateTests {
assertEquals("Invalid match", expected, result);
}
@Test // SPR-16169
public void matchWithMultipleSegmentsAtTheEnd() {
UriTemplate template = new UriTemplate("/account/{accountId}");
assertFalse(template.matches("/account/15/alias/5"));
}
@Test
public void queryVariables() throws Exception {
UriTemplate template = new UriTemplate("/search?q={query}");
@ -195,9 +199,7 @@ public class UriTemplateTests { @@ -195,9 +199,7 @@ public class UriTemplateTests {
assertTrue(template.matches("/search?query=foo#bar"));
}
// SPR-13705
@Test
@Test // SPR-13705
public void matchesWithSlashAtTheEnd() {
UriTemplate uriTemplate = new UriTemplate("/test/");
assertTrue(uriTemplate.matches("/test/"));

Loading…
Cancel
Save