Empty path should match the "/" PathPattern
This commit makes both slash "/" and empty "" request paths match the "/" `PathPattern`. Issue: SPR-15653
This commit is contained in:
committed by
Brian Clozel
parent
b85764c7db
commit
07b961caa6
@@ -156,7 +156,6 @@ public class PathPattern implements Comparable<PathPattern> {
|
|||||||
return this.patternString;
|
return this.patternString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether this pattern matches the given path.
|
* Whether this pattern matches the given path.
|
||||||
* @param pathContainer the candidate path to attempt to match against
|
* @param pathContainer the candidate path to attempt to match against
|
||||||
@@ -164,7 +163,8 @@ public class PathPattern implements Comparable<PathPattern> {
|
|||||||
*/
|
*/
|
||||||
public boolean matches(PathContainer pathContainer) {
|
public boolean matches(PathContainer pathContainer) {
|
||||||
if (this.head == null) {
|
if (this.head == null) {
|
||||||
return !hasLength(pathContainer);
|
return !hasLength(pathContainer) ||
|
||||||
|
(this.matchOptionalTrailingSeparator && pathContainerIsJustSeparator(pathContainer));
|
||||||
}
|
}
|
||||||
else if (!hasLength(pathContainer)) {
|
else if (!hasLength(pathContainer)) {
|
||||||
if (this.head instanceof WildcardTheRestPathElement || this.head instanceof CaptureTheRestPathElement) {
|
if (this.head instanceof WildcardTheRestPathElement || this.head instanceof CaptureTheRestPathElement) {
|
||||||
@@ -187,7 +187,9 @@ public class PathPattern implements Comparable<PathPattern> {
|
|||||||
@Nullable
|
@Nullable
|
||||||
public PathMatchInfo matchAndExtract(PathContainer pathContainer) {
|
public PathMatchInfo matchAndExtract(PathContainer pathContainer) {
|
||||||
if (this.head == null) {
|
if (this.head == null) {
|
||||||
return hasLength(pathContainer) ? null : PathMatchInfo.EMPTY;
|
return hasLength(pathContainer) &&
|
||||||
|
!(this.matchOptionalTrailingSeparator && pathContainerIsJustSeparator(pathContainer))
|
||||||
|
? null : PathMatchInfo.EMPTY;
|
||||||
}
|
}
|
||||||
else if (!hasLength(pathContainer)) {
|
else if (!hasLength(pathContainer)) {
|
||||||
if (this.head instanceof WildcardTheRestPathElement || this.head instanceof CaptureTheRestPathElement) {
|
if (this.head instanceof WildcardTheRestPathElement || this.head instanceof CaptureTheRestPathElement) {
|
||||||
@@ -708,5 +710,10 @@ public class PathPattern implements Comparable<PathPattern> {
|
|||||||
private static int scoreByNormalizedLength(PathPattern pattern) {
|
private static int scoreByNormalizedLength(PathPattern pattern) {
|
||||||
return -pattern.getNormalizedLength();
|
return -pattern.getNormalizedLength();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean pathContainerIsJustSeparator(PathContainer pathContainer) {
|
||||||
|
return pathContainer.value().length() == 1 &&
|
||||||
|
pathContainer.value().charAt(0) == separator;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,6 +77,7 @@ public class PathPatternTests {
|
|||||||
@Test
|
@Test
|
||||||
public void basicMatching() {
|
public void basicMatching() {
|
||||||
checkMatches("", "");
|
checkMatches("", "");
|
||||||
|
checkMatches("", "/");
|
||||||
checkMatches("", null);
|
checkMatches("", null);
|
||||||
checkNoMatch("/abc", "/");
|
checkNoMatch("/abc", "/");
|
||||||
checkMatches("/", "/");
|
checkMatches("/", "/");
|
||||||
@@ -1113,9 +1114,13 @@ public class PathPatternTests {
|
|||||||
result = matchAndExtract("/abc/{var}","/abc/one");
|
result = matchAndExtract("/abc/{var}","/abc/one");
|
||||||
assertEquals("one",result.getUriVariables().get("var"));
|
assertEquals("one",result.getUriVariables().get("var"));
|
||||||
assertNull(result.getMatrixVariables().get("var"));
|
assertNull(result.getMatrixVariables().get("var"));
|
||||||
|
|
||||||
|
result = matchAndExtract("","");
|
||||||
|
assertNotNull(result);
|
||||||
|
result = matchAndExtract("","/");
|
||||||
|
assertNotNull(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private PathPattern.PathMatchInfo matchAndExtract(String pattern, String path) {
|
private PathPattern.PathMatchInfo matchAndExtract(String pattern, String path) {
|
||||||
return parse(pattern).matchAndExtract(PathPatternTests.toPathContainer(path));
|
return parse(pattern).matchAndExtract(PathPatternTests.toPathContainer(path));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user