Skip to content

Commit 07b961c

Browse files
aclementbclozel
authored andcommitted
Empty path should match the "/" PathPattern
This commit makes both slash "/" and empty "" request paths match the "/" `PathPattern`. Issue: SPR-15653
1 parent b85764c commit 07b961c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

spring-web/src/main/java/org/springframework/web/util/pattern/PathPattern.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@ public String getPatternString() {
156156
return this.patternString;
157157
}
158158

159-
160159
/**
161160
* Whether this pattern matches the given path.
162161
* @param pathContainer the candidate path to attempt to match against
163162
* @return {@code true} if the path matches this pattern
164163
*/
165164
public boolean matches(PathContainer pathContainer) {
166165
if (this.head == null) {
167-
return !hasLength(pathContainer);
166+
return !hasLength(pathContainer) ||
167+
(this.matchOptionalTrailingSeparator && pathContainerIsJustSeparator(pathContainer));
168168
}
169169
else if (!hasLength(pathContainer)) {
170170
if (this.head instanceof WildcardTheRestPathElement || this.head instanceof CaptureTheRestPathElement) {
@@ -187,7 +187,9 @@ else if (!hasLength(pathContainer)) {
187187
@Nullable
188188
public PathMatchInfo matchAndExtract(PathContainer pathContainer) {
189189
if (this.head == null) {
190-
return hasLength(pathContainer) ? null : PathMatchInfo.EMPTY;
190+
return hasLength(pathContainer) &&
191+
!(this.matchOptionalTrailingSeparator && pathContainerIsJustSeparator(pathContainer))
192+
? null : PathMatchInfo.EMPTY;
191193
}
192194
else if (!hasLength(pathContainer)) {
193195
if (this.head instanceof WildcardTheRestPathElement || this.head instanceof CaptureTheRestPathElement) {
@@ -708,5 +710,10 @@ private boolean hasLength(@Nullable PathContainer container) {
708710
private static int scoreByNormalizedLength(PathPattern pattern) {
709711
return -pattern.getNormalizedLength();
710712
}
713+
714+
private boolean pathContainerIsJustSeparator(PathContainer pathContainer) {
715+
return pathContainer.value().length() == 1 &&
716+
pathContainer.value().charAt(0) == separator;
717+
}
711718

712719
}

spring-web/src/test/java/org/springframework/web/util/pattern/PathPatternTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public void matching_LiteralPathElement() {
7777
@Test
7878
public void basicMatching() {
7979
checkMatches("", "");
80+
checkMatches("", "/");
8081
checkMatches("", null);
8182
checkNoMatch("/abc", "/");
8283
checkMatches("/", "/");
@@ -1113,9 +1114,13 @@ public void parameters() {
11131114
result = matchAndExtract("/abc/{var}","/abc/one");
11141115
assertEquals("one",result.getUriVariables().get("var"));
11151116
assertNull(result.getMatrixVariables().get("var"));
1117+
1118+
result = matchAndExtract("","");
1119+
assertNotNull(result);
1120+
result = matchAndExtract("","/");
1121+
assertNotNull(result);
11161122
}
11171123

1118-
11191124
private PathPattern.PathMatchInfo matchAndExtract(String pattern, String path) {
11201125
return parse(pattern).matchAndExtract(PathPatternTests.toPathContainer(path));
11211126
}

0 commit comments

Comments
 (0)