Skip to content

Commit c60313d

Browse files
committed
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
1 parent 84b8cec commit c60313d

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ else if (c == '}') {
212212
String variable = builder.toString();
213213
int idx = variable.indexOf(':');
214214
if (idx == -1) {
215-
pattern.append("(.*)");
215+
pattern.append("([^/]*)");
216216
variableNames.add(variable);
217217
}
218218
else {

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,7 @@ public void matchCustomRegex() throws Exception {
153153
assertEquals("Invalid match", expected, result);
154154
}
155155

156-
// SPR-13627
157-
158-
@Test
156+
@Test // SPR-13627
159157
public void matchCustomRegexWithNestedCurlyBraces() throws Exception {
160158
UriTemplate template = new UriTemplate("/site.{domain:co.[a-z]{2}}");
161159
Map<String, String> result = template.match("/site.co.eu");
@@ -180,6 +178,12 @@ public void matchMultipleInOneSegment() throws Exception {
180178
assertEquals("Invalid match", expected, result);
181179
}
182180

181+
@Test // SPR-16169
182+
public void matchWithMultipleSegmentsAtTheEnd() {
183+
UriTemplate template = new UriTemplate("/account/{accountId}");
184+
assertFalse(template.matches("/account/15/alias/5"));
185+
}
186+
183187
@Test
184188
public void queryVariables() throws Exception {
185189
UriTemplate template = new UriTemplate("/search?q={query}");
@@ -195,9 +199,7 @@ public void fragments() throws Exception {
195199
assertTrue(template.matches("/search?query=foo#bar"));
196200
}
197201

198-
// SPR-13705
199-
200-
@Test
202+
@Test // SPR-13705
201203
public void matchesWithSlashAtTheEnd() {
202204
UriTemplate uriTemplate = new UriTemplate("/test/");
203205
assertTrue(uriTemplate.matches("/test/"));

0 commit comments

Comments
 (0)