Skip to content

Commit f1653cc

Browse files
committed
Fix PathPattern incorrectly matching variable against root path
Issue: SPR-15264
1 parent 45df1d9 commit f1653cc

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

spring-web/src/main/java/org/springframework/web/util/patterns/CaptureVariablePathElement.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public boolean matches(int candidateIndex, MatchingContext matchingContext) {
8080
}
8181
boolean match = false;
8282
if (next == null) {
83-
match = (nextPos == matchingContext.candidateLength);
83+
// Needs to be at least one character #SPR15264
84+
match = (nextPos == matchingContext.candidateLength && nextPos > candidateIndex);
8485
}
8586
else {
8687
if (matchingContext.isMatchStartMatching && nextPos == matchingContext.candidateLength) {

spring-web/src/test/java/org/springframework/web/util/patterns/PathPatternMatcherTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,31 @@ public void extractPathWithinPattern() throws Exception {
507507
checkExtractPathWithinPattern("/", "//", "");
508508
}
509509

510+
@Test
511+
public void extractUriTemplateVariables_spr15264() {
512+
PathPattern pp = new PathPatternParser().parse("/{foo}");
513+
assertTrue(pp.matches("/abc"));
514+
assertFalse(pp.matches("/"));
515+
assertFalse(pp.matches("//"));
516+
checkCapture("/{foo}", "/abc", "foo", "abc");
517+
518+
pp = new PathPatternParser().parse("/{foo}/{bar}");
519+
assertTrue(pp.matches("/abc/def"));
520+
assertFalse(pp.matches("//def"));
521+
assertFalse(pp.matches("//"));
522+
523+
pp = parse("/{foo}/boo");
524+
assertTrue(pp.matches("/abc/boo"));
525+
assertTrue(pp.matches("/a/boo"));
526+
assertFalse(pp.matches("//boo"));
527+
528+
checkCapture("/{word:[a-z]*}", "/abc", "word", "abc");
529+
pp = parse("/{word:[a-z]*}");
530+
assertFalse(pp.matches("/1"));
531+
assertTrue(pp.matches("/a"));
532+
assertFalse(pp.matches("/"));
533+
}
534+
510535
@Test
511536
public void extractUriTemplateVariables() throws Exception {
512537
checkCapture("/hotels/{hotel}", "/hotels/1", "hotel", "1");

0 commit comments

Comments
 (0)