Skip to content

Commit fed4391

Browse files
committed
Fix issue in UriTemplate parsing
The recent commit 971f04 replaced the use of a NAMES_PATTERN regex in favor of direct parsing in order to deal with nested curly braces. The change also incorrectly replicated this logic which removes a trailing slash after Pattern quoting (and not before): https://github.com/spring-projects/spring-framework/blob/cca037a74df9b87f99858154b47696a9eccf69c8/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java#L207-L210 After some more investigation there doesn't appear to be any scenario where the quoted pattern would end with a trailing slash. It should always end with \E (end of quote) or a ")" (end of group). Nor are there any failing tests so this commit removes the logic altogether. Issue: SPR-13705
1 parent 998da2f commit fed4391

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ private static TemplateInfo parse(String uriTemplate) {
197197
if (c == '{') {
198198
level++;
199199
if (level == 1) {
200+
// start of URI variable
200201
pattern.append(quote(builder));
201202
builder = new StringBuilder();
202203
continue;
@@ -205,6 +206,7 @@ private static TemplateInfo parse(String uriTemplate) {
205206
else if (c == '}') {
206207
level--;
207208
if (level == 0) {
209+
// end of URI variable
208210
String variable = builder.toString();
209211
int idx = variable.indexOf(':');
210212
if (idx == -1) {
@@ -227,14 +229,11 @@ else if (c == '}') {
227229
continue;
228230
}
229231
}
230-
if (i + 1 == uriTemplate.length()) {
231-
if (c != '/') {
232-
builder.append(c);
233-
}
234-
pattern.append(quote(builder));
235-
}
236232
builder.append(c);
237233
}
234+
if (builder.length() > 0) {
235+
pattern.append(quote(builder));
236+
}
238237
return new TemplateInfo(variableNames, Pattern.compile(pattern.toString()));
239238
}
240239

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ public void fragments() throws Exception {
185185
assertTrue(template.matches("/search?query=foo#bar"));
186186
}
187187

188+
// SPR-13705
189+
190+
@Test
191+
public void matchesWithSlashAtTheEnd() {
192+
UriTemplate uriTemplate = new UriTemplate("/test/");
193+
assertTrue(uriTemplate.matches("/test/"));
194+
}
195+
188196
@Test
189197
public void expandWithDollar() {
190198
UriTemplate template = new UriTemplate("/{a}");

0 commit comments

Comments
 (0)