Browse Source

Combined, empty RequestMapping matches both "" and "/"

Closes gh-29625
pull/29780/merge
rstoyanchev 3 years ago
parent
commit
312db36849
  1. 18
      spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/PatternsRequestCondition.java
  2. 4
      spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/PatternsRequestConditionTests.java
  3. 4
      spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/RequestMappingInfoTests.java
  4. 20
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PathPatternsRequestCondition.java
  5. 17
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java
  6. 4
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/PathPatternsRequestConditionTests.java
  7. 4
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/PatternsRequestConditionTests.java
  8. 4
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoTests.java

18
spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/PatternsRequestCondition.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -48,6 +48,9 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat @@ -48,6 +48,9 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
private static final Set<String> EMPTY_PATH = Collections.singleton("");
private static final SortedSet<PathPattern> ROOT_PATH_PATTERNS =
new TreeSet<>(List.of(new PathPatternParser().parse(""), new PathPatternParser().parse("/")));
private final SortedSet<PathPattern> patterns;
@ -109,19 +112,18 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat @@ -109,19 +112,18 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
}
/**
* Returns a new instance with URL patterns from the current instance ("this") and
* the "other" instance as follows:
* Combine the patterns of the current and of the other instances as follows:
* <ul>
* <li>If there are patterns in both instances, combine the patterns in "this" with
* the patterns in "other" using {@link PathPattern#combine(PathPattern)}.
* <li>If only one instance has patterns, use them.
* <li>If neither instance has patterns, use an empty String (i.e. "").
* <li>If only one instance has patterns, use those.
* <li>If both have patterns, combine patterns from "this" instance with
* patterns from the other instance via {@link PathPattern#combine(PathPattern)}.
* <li>If neither has patterns, use {@code ""} and {@code "/"} as root path patterns.
* </ul>
*/
@Override
public PatternsRequestCondition combine(PatternsRequestCondition other) {
if (isEmptyPathMapping() && other.isEmptyPathMapping()) {
return this;
return new PatternsRequestCondition(ROOT_PATH_PATTERNS);
}
else if (other.isEmptyPathMapping()) {
return this;

4
spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/PatternsRequestConditionTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -52,8 +52,8 @@ public class PatternsRequestConditionTests { @@ -52,8 +52,8 @@ public class PatternsRequestConditionTests {
PatternsRequestCondition c2 = new PatternsRequestCondition();
PatternsRequestCondition c3 = c1.combine(c2);
assertThat(c3).isSameAs(c1);
assertThat(c1.getPatterns()).isSameAs(c2.getPatterns()).containsExactly(this.parser.parse(""));
assertThat(c3.toString()).isEqualTo("[/ || ]");
}
@Test

4
spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/RequestMappingInfoTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -73,7 +73,7 @@ public class RequestMappingInfoTests { @@ -73,7 +73,7 @@ public class RequestMappingInfoTests {
assertThat(info.getCustomCondition()).isSameAs(anotherInfo.getCustomCondition());
RequestMappingInfo result = info.combine(anotherInfo);
assertThat(info.getPatternsCondition()).isSameAs(result.getPatternsCondition());
assertThat(result.getPatternsCondition().toString()).isEqualTo("[/ || ]");
assertThat(info.getMethodsCondition()).isSameAs(result.getMethodsCondition());
assertThat(info.getParamsCondition()).isSameAs(result.getParamsCondition());
assertThat(info.getHeadersCondition()).isSameAs(result.getHeadersCondition());

20
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PathPatternsRequestCondition.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,6 +20,7 @@ import java.util.Collection; @@ -20,6 +20,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
@ -52,6 +53,9 @@ public final class PathPatternsRequestCondition extends AbstractRequestCondition @@ -52,6 +53,9 @@ public final class PathPatternsRequestCondition extends AbstractRequestCondition
private static final Set<String> EMPTY_PATH = Collections.singleton("");
private static final SortedSet<PathPattern> ROOT_PATH_PATTERNS =
new TreeSet<>(List.of(new PathPatternParser().parse(""), new PathPatternParser().parse("/")));
private final SortedSet<PathPattern> patterns;
@ -146,20 +150,18 @@ public final class PathPatternsRequestCondition extends AbstractRequestCondition @@ -146,20 +150,18 @@ public final class PathPatternsRequestCondition extends AbstractRequestCondition
}
/**
* Returns a new instance with URL patterns from the current instance
* ("this") and the "other" instance as follows:
* Combine the patterns of the current and of the other instances as follows:
* <ul>
* <li>If there are patterns in both instances, combine the patterns in
* "this" with the patterns in "other" using
* {@link PathPattern#combine(PathPattern)}.
* <li>If only one instance has patterns, use them.
* <li>If neither instance has patterns, use an empty String (i.e. "").
* <li>If only one instance has patterns, use those.
* <li>If both have patterns, combine patterns from "this" instance with
* patterns from the other instance via {@link PathPattern#combine(PathPattern)}.
* <li>If neither has patterns, use {@code ""} and {@code "/"} as root path patterns.
* </ul>
*/
@Override
public PathPatternsRequestCondition combine(PathPatternsRequestCondition other) {
if (isEmptyPathMapping() && other.isEmptyPathMapping()) {
return this;
return new PathPatternsRequestCondition(ROOT_PATH_PATTERNS);
}
else if (other.isEmptyPathMapping()) {
return this;

17
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -51,6 +51,8 @@ public class PatternsRequestCondition extends AbstractRequestCondition<PatternsR @@ -51,6 +51,8 @@ public class PatternsRequestCondition extends AbstractRequestCondition<PatternsR
private final static Set<String> EMPTY_PATH_PATTERN = Collections.singleton("");
private final static String[] ROOT_PATH_PATTERNS = new String[] {"", "/"};
private final Set<String> patterns;
@ -227,19 +229,18 @@ public class PatternsRequestCondition extends AbstractRequestCondition<PatternsR @@ -227,19 +229,18 @@ public class PatternsRequestCondition extends AbstractRequestCondition<PatternsR
}
/**
* Returns a new instance with URL patterns from the current instance ("this") and
* the "other" instance as follows:
* Combine the patterns of the current and of the other instances as follows:
* <ul>
* <li>If there are patterns in both instances, combine the patterns in "this" with
* the patterns in "other" using {@link PathMatcher#combine(String, String)}.
* <li>If only one instance has patterns, use them.
* <li>If neither instance has patterns, use an empty String (i.e. "").
* <li>If only one instance has patterns, use those.
* <li>If both have patterns, combine patterns from "this" instance with
* patterns from the other instance via {@link PathMatcher#combine(String, String)}.
* <li>If neither has patterns, use {@code ""} and {@code "/"} as root path patterns.
* </ul>
*/
@Override
public PatternsRequestCondition combine(PatternsRequestCondition other) {
if (isEmptyPathMapping() && other.isEmptyPathMapping()) {
return this;
return new PatternsRequestCondition(ROOT_PATH_PATTERNS);
}
else if (other.isEmptyPathMapping()) {
return this;

4
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/PathPatternsRequestConditionTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -59,8 +59,8 @@ public class PathPatternsRequestConditionTests { @@ -59,8 +59,8 @@ public class PathPatternsRequestConditionTests {
PathPatternsRequestCondition c2 = createCondition();
PathPatternsRequestCondition c3 = c1.combine(c2);
assertThat(c3).isSameAs(c1);
assertThat(c1.getPatternValues()).isSameAs(c2.getPatternValues()).containsExactly("");
assertThat(c3.getPatternValues()).containsExactly("", "/");
}
@Test

4
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/PatternsRequestConditionTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -58,8 +58,8 @@ class PatternsRequestConditionTests { @@ -58,8 +58,8 @@ class PatternsRequestConditionTests {
PatternsRequestCondition c2 = new PatternsRequestCondition();
PatternsRequestCondition c3 = c1.combine(c2);
assertThat(c3).isSameAs(c1);
assertThat(c1.getPatterns()).isSameAs(c2.getPatterns()).containsExactly("");
assertThat(c3.getPatterns()).containsExactly("", "/");
}
@Test

4
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -77,7 +77,7 @@ class RequestMappingInfoTests { @@ -77,7 +77,7 @@ class RequestMappingInfoTests {
assertThat(info.getCustomCondition()).isSameAs(anotherInfo.getCustomCondition());
RequestMappingInfo result = info.combine(anotherInfo);
assertThat(info.getActivePatternsCondition()).isSameAs(result.getActivePatternsCondition());
assertThat(result.getPatternValues()).containsExactly("", "/");
assertThat(info.getMethodsCondition()).isSameAs(result.getMethodsCondition());
assertThat(info.getParamsCondition()).isSameAs(result.getParamsCondition());
assertThat(info.getHeadersCondition()).isSameAs(result.getHeadersCondition());

Loading…
Cancel
Save