Skip to content

Commit 1e04643

Browse files
committed
AntPathMatcher actually throws IllegalArgumentException if patterns cannot be combined
Issue: SPR-12998
1 parent acfc95e commit 1e04643

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

spring-core/src/main/java/org/springframework/util/AntPathMatcher.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,10 @@ public Map<String, String> extractUriTemplateVariables(String pattern, String pa
419419

420420
/**
421421
* Combine two patterns into a new pattern.
422-
*
423422
* <p>This implementation simply concatenates the two patterns, unless
424423
* the first pattern contains a file extension match (e.g., {@code *.html}).
425424
* In that case, the second pattern will be merged into the first. Otherwise,
426425
* an {@code IllegalArgumentException} will be thrown.
427-
*
428426
* <h3>Examples</h3>
429427
* <table border="1">
430428
* <tr><th>Pattern 1</th><th>Pattern 2</th><th>Result</th></tr>
@@ -442,7 +440,6 @@ public Map<String, String> extractUriTemplateVariables(String pattern, String pa
442440
* <tr><td>/*.html</td><td>/hotels</td><td>/hotels.html</td></tr>
443441
* <tr><td>/*.html</td><td>/*.txt</td><td>{@code IllegalArgumentException}</td></tr>
444442
* </table>
445-
*
446443
* @param pattern1 the first pattern
447444
* @param pattern2 the second pattern
448445
* @return the combination of the two patterns
@@ -460,7 +457,7 @@ public String combine(String pattern1, String pattern2) {
460457
return pattern1;
461458
}
462459

463-
boolean pattern1ContainsUriVar = pattern1.indexOf('{') != -1;
460+
boolean pattern1ContainsUriVar = (pattern1.indexOf('{') != -1);
464461
if (!pattern1.equals(pattern2) && !pattern1ContainsUriVar && match(pattern1, pattern2)) {
465462
// /* + /hotel -> /hotel ; "/*.*" + "/*.html" -> /*.html
466463
// However /user + /user -> /usr/user ; /{foo} + /bar -> /{foo}/bar
@@ -484,12 +481,18 @@ public String combine(String pattern1, String pattern2) {
484481
// simply concatenate the two patterns
485482
return concat(pattern1, pattern2);
486483
}
487-
String extension1 = pattern1.substring(starDotPos1 + 1);
484+
485+
String ext1 = pattern1.substring(starDotPos1 + 1);
488486
int dotPos2 = pattern2.indexOf('.');
489-
String fileName2 = (dotPos2 == -1 ? pattern2 : pattern2.substring(0, dotPos2));
490-
String extension2 = (dotPos2 == -1 ? "" : pattern2.substring(dotPos2));
491-
String extension = extension1.startsWith("*") ? extension2 : extension1;
492-
return fileName2 + extension;
487+
String file2 = (dotPos2 == -1 ? pattern2 : pattern2.substring(0, dotPos2));
488+
String ext2 = (dotPos2 == -1 ? "" : pattern2.substring(dotPos2));
489+
boolean ext1All = (ext1.equals(".*") || ext1.equals(""));
490+
boolean ext2All = (ext2.equals(".*") || ext2.equals(""));
491+
if (!ext1All && !ext2All) {
492+
throw new IllegalArgumentException("Cannot combine patterns: " + pattern1 + " vs " + pattern2);
493+
}
494+
String ext = (ext1All ? ext2 : ext1);
495+
return file2 + ext;
493496
}
494497

495498
private String concat(String path1, String path2) {
@@ -508,14 +511,18 @@ else if (path1EndsWithSeparator || path2StartsWithSeparator) {
508511
}
509512

510513
/**
511-
* Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of explicitness.
512-
* <p>The returned {@code Comparator} will {@linkplain java.util.Collections#sort(java.util.List,
513-
* java.util.Comparator) sort} a list so that more specific patterns (without uri templates or wild cards) come before
514-
* generic patterns. So given a list with the following patterns: <ol> <li>{@code /hotels/new}</li>
515-
* <li>{@code /hotels/{hotel}}</li> <li>{@code /hotels/*}</li> </ol> the returned comparator will sort this
516-
* list so that the order will be as indicated.
517-
* <p>The full path given as parameter is used to test for exact matches. So when the given path is {@code /hotels/2},
518-
* the pattern {@code /hotels/2} will be sorted before {@code /hotels/1}.
514+
* Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of
515+
* explicitness.
516+
* <p>This{@code Comparator} will {@linkplain java.util.Collections#sort(List, Comparator) sort}
517+
* a list so that more specific patterns (without uri templates or wild cards) come before
518+
* generic patterns. So given a list with the following patterns:
519+
* <ol>
520+
* <li>{@code /hotels/new}</li>
521+
* <li>{@code /hotels/{hotel}}</li> <li>{@code /hotels/*}</li>
522+
* </ol>
523+
* the returned comparator will sort this list so that the order will be as indicated.
524+
* <p>The full path given as parameter is used to test for exact matches. So when the given path
525+
* is {@code /hotels/2}, the pattern {@code /hotels/2} will be sorted before {@code /hotels/1}.
519526
* @param path the full path to use for comparison
520527
* @return a comparator capable of sorting patterns in order of explicitness
521528
*/

spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,11 @@
4444
*/
4545
public class AntPathMatcherTests {
4646

47-
private AntPathMatcher pathMatcher;
47+
private final AntPathMatcher pathMatcher = new AntPathMatcher();
4848

4949
@Rule
50-
public ExpectedException exception = ExpectedException.none();
50+
public final ExpectedException exception = ExpectedException.none();
5151

52-
@Before
53-
public void createMatcher() {
54-
pathMatcher = new AntPathMatcher();
55-
}
5652

5753
@Test
5854
public void match() {
@@ -432,7 +428,6 @@ public void combine() {
432428
assertEquals("/hotel/booking", pathMatcher.combine("/hotel/", "/booking")); // SPR-12975
433429
}
434430

435-
@Ignore("Disabled until SPR-12998 is resolved")
436431
@Test
437432
public void combineWithTwoFileExtensionPatterns() {
438433
exception.expect(IllegalArgumentException.class);

0 commit comments

Comments
 (0)