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
This commit is contained in:
Rossen Stoyanchev 2018-01-11 16:36:35 -05:00
parent 84b8ceca0f
commit c60313de3f
2 changed files with 9 additions and 7 deletions

View File

@ -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 {

View File

@ -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 {
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 {
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/"));