Browse Source

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):

cca037a74d/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
pull/928/merge
Rossen Stoyanchev 9 years ago
parent
commit
fed4391f87
  1. 11
      spring-web/src/main/java/org/springframework/web/util/UriTemplate.java
  2. 8
      spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java

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

@ -197,6 +197,7 @@ public class UriTemplate implements Serializable {
if (c == '{') { if (c == '{') {
level++; level++;
if (level == 1) { if (level == 1) {
// start of URI variable
pattern.append(quote(builder)); pattern.append(quote(builder));
builder = new StringBuilder(); builder = new StringBuilder();
continue; continue;
@ -205,6 +206,7 @@ public class UriTemplate implements Serializable {
else if (c == '}') { else if (c == '}') {
level--; level--;
if (level == 0) { if (level == 0) {
// end of URI variable
String variable = builder.toString(); String variable = builder.toString();
int idx = variable.indexOf(':'); int idx = variable.indexOf(':');
if (idx == -1) { if (idx == -1) {
@ -227,14 +229,11 @@ public class UriTemplate implements Serializable {
continue; continue;
} }
} }
if (i + 1 == uriTemplate.length()) {
if (c != '/') {
builder.append(c);
}
pattern.append(quote(builder));
}
builder.append(c); builder.append(c);
} }
if (builder.length() > 0) {
pattern.append(quote(builder));
}
return new TemplateInfo(variableNames, Pattern.compile(pattern.toString())); return new TemplateInfo(variableNames, Pattern.compile(pattern.toString()));
} }

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

@ -185,6 +185,14 @@ public class UriTemplateTests {
assertTrue(template.matches("/search?query=foo#bar")); assertTrue(template.matches("/search?query=foo#bar"));
} }
// SPR-13705
@Test
public void matchesWithSlashAtTheEnd() {
UriTemplate uriTemplate = new UriTemplate("/test/");
assertTrue(uriTemplate.matches("/test/"));
}
@Test @Test
public void expandWithDollar() { public void expandWithDollar() {
UriTemplate template = new UriTemplate("/{a}"); UriTemplate template = new UriTemplate("/{a}");

Loading…
Cancel
Save