diff --git a/spring-web/src/main/java/org/springframework/web/util/pattern/RegexPathElement.java b/spring-web/src/main/java/org/springframework/web/util/pattern/RegexPathElement.java index 56e83d5fa4..467dee7421 100644 --- a/spring-web/src/main/java/org/springframework/web/util/pattern/RegexPathElement.java +++ b/spring-web/src/main/java/org/springframework/web/util/pattern/RegexPathElement.java @@ -72,7 +72,12 @@ class RegexPathElement extends PathElement { } else if ("*".equals(match)) { patternBuilder.append(".*"); - this.wildcardCount++; + int pos = matcher.start(); + if (pos < 1 || text.charAt(pos-1) != '.') { + // To be compatible with the AntPathMatcher comparator, + // '.*' is not considered a wildcard usage + this.wildcardCount++; + } } else if (match.startsWith("{") && match.endsWith("}")) { int colonIdx = match.indexOf(':'); diff --git a/spring-web/src/test/java/org/springframework/web/util/pattern/PathPatternMatcherTests.java b/spring-web/src/test/java/org/springframework/web/util/pattern/PathPatternMatcherTests.java index 05cd467e99..2d0802b833 100644 --- a/spring-web/src/test/java/org/springframework/web/util/pattern/PathPatternMatcherTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/pattern/PathPatternMatcherTests.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.hamcrest.Matchers; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -1052,6 +1053,25 @@ public class PathPatternMatcherTests { assertEquals(-1, comparator.compare(parse("*"), parse("*/**"))); assertEquals(1, comparator.compare(parse("*/**"), parse("*"))); } + + @Test + public void compare_spr15597() { + PathPatternParser parser = new PathPatternParser(); + PathPattern p1 = parser.parse("/{foo}"); + PathPattern p2 = parser.parse("/{foo}.*"); + Map r1 = p1.matchAndExtract("/file.txt"); + Map r2 = p2.matchAndExtract("/file.txt"); + + // works fine + assertEquals(r1.get("foo"), "file.txt"); + assertEquals(r2.get("foo"), "file"); + + // This produces 2 (see comments in https://jira.spring.io/browse/SPR-14544 ) + // Comparator patternComparator = new AntPathMatcher().getPatternComparator(""); + // System.out.println(patternComparator.compare("/{foo}","/{foo}.*")); + + assertThat(p1.compareTo(p2), Matchers.greaterThan(0)); + } @Test public void patternCompareTo() {