Assign lowest priority to `/**` pattern

Update AntPathMatcher Comparator to treat `/**` in the same way as
`null` paths.

Prior to this commit the pattern `/**` would be picked in preference
to patterns with 3 or more PathVariable (e.g. `/matches/{matchId}/
periods/{periodId}/teams/{teamId}/results`).

Issue: SPR-10550
master
Antonio Marrero 11 years ago committed by Phillip Webb
parent c1dafed886
commit 57f7b14b49
  1. 12
      spring-core/src/main/java/org/springframework/util/AntPathMatcher.java
  2. 5
      spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java

@ -391,15 +391,16 @@ public class AntPathMatcher implements PathMatcher {
@Override
public int compare(String pattern1, String pattern2) {
if (pattern1 == null && pattern2 == null) {
if (isNullOrCaptureAllPattern(pattern1) && isNullOrCaptureAllPattern(pattern2)) {
return 0;
}
else if (pattern1 == null) {
else if (isNullOrCaptureAllPattern(pattern1)) {
return 1;
}
else if (pattern2 == null) {
else if (isNullOrCaptureAllPattern(pattern2)) {
return -1;
}
boolean pattern1EqualsPath = pattern1.equals(path);
boolean pattern2EqualsPath = pattern2.equals(path);
if (pattern1EqualsPath && pattern2EqualsPath) {
@ -411,6 +412,7 @@ public class AntPathMatcher implements PathMatcher {
else if (pattern2EqualsPath) {
return 1;
}
int wildCardCount1 = getWildCardCount(pattern1);
int wildCardCount2 = getWildCardCount(pattern2);
@ -448,6 +450,10 @@ public class AntPathMatcher implements PathMatcher {
return 0;
}
private boolean isNullOrCaptureAllPattern(String pattern) {
return pattern == null || "/**".equals(pattern);
}
private int getWildCardCount(String pattern) {
if (pattern.endsWith(".*")) {
pattern = pattern.substring(0, pattern.length() - 2);

@ -437,6 +437,11 @@ public class AntPathMatcherTests {
assertEquals(-1, comparator.compare("/hotels/{hotel}/booking", "/hotels/{hotel}/bookings/{booking}"));
assertEquals(1, comparator.compare("/hotels/{hotel}/bookings/{booking}", "/hotels/{hotel}/booking"));
//SPR-10550
assertEquals(-1, comparator.compare("/hotels/{hotel}/bookings/{booking}/cutomers/{customer}", "/**"));
assertEquals(1, comparator.compare("/**","/hotels/{hotel}/bookings/{booking}/cutomers/{customer}"));
assertEquals(0, comparator.compare("/**","/**"));
assertEquals(-1, comparator.compare("/hotels/{hotel}", "/hotels/*"));
assertEquals(1, comparator.compare("/hotels/*", "/hotels/{hotel}"));

Loading…
Cancel
Save