Empty path mapping behaves consistently

An empty path mapping in an @RequestMapping now consistently matches to
empty paths regardless of whether there are both type and method level,
annotations, or method-level only.

Closes gh-22543
This commit is contained in:
Rossen Stoyanchev
2019-04-09 15:03:24 -04:00
parent f839c1f9cd
commit d1f888a7a9
17 changed files with 96 additions and 41 deletions

View File

@@ -140,6 +140,19 @@ public class PatternsRequestConditionTests {
assertNull(match);
}
@Test // gh-22543
public void matchWithEmptyPatterns() {
PatternsRequestCondition condition = new PatternsRequestCondition();
assertEquals(new PatternsRequestCondition(this.parser.parse("")), condition);
assertNotNull(condition.getMatchingCondition(MockServerWebExchange.from(get(""))));
assertNull(condition.getMatchingCondition(MockServerWebExchange.from(get("/anything"))));
condition = condition.combine(new PatternsRequestCondition());
assertEquals(new PatternsRequestCondition(this.parser.parse("")), condition);
assertNotNull(condition.getMatchingCondition(MockServerWebExchange.from(get(""))));
assertNull(condition.getMatchingCondition(MockServerWebExchange.from(get("/anything"))));
}
@Test
public void compareToConsistentWithEquals() throws Exception {
PatternsRequestCondition c1 = createPatternsCondition("/foo*");

View File

@@ -34,6 +34,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.reactive.result.method.RequestMappingInfo;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;
import org.springframework.web.util.pattern.PatternParseException;
import static java.util.Arrays.asList;
@@ -61,7 +62,8 @@ public class RequestMappingInfoTests {
public void createEmpty() {
RequestMappingInfo info = paths().build();
assertEquals(0, info.getPatternsCondition().getPatterns().size());
PathPattern emptyPattern = (new PathPatternParser()).parse("");
assertEquals(Collections.singleton(emptyPattern), info.getPatternsCondition().getPatterns());
assertEquals(0, info.getMethodsCondition().getMethods().size());
assertEquals(true, info.getConsumesCondition().isEmpty());
assertEquals(true, info.getProducesCondition().isEmpty());

View File

@@ -253,14 +253,12 @@ public class RequestMappingInfoHandlerMappingTests {
assertSame(handlerMethod, mapped);
}
@Test
@Test // gh-22543
public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() {
RequestMappingInfo key = paths().build();
ServerWebExchange exchange = MockServerWebExchange.from(get("/1/2"));
this.handlerMapping.handleMatch(key, handlerMethod, exchange);
PathPattern bestMatch = (PathPattern) exchange.getAttributes().get(BEST_MATCHING_PATTERN_ATTRIBUTE);
assertEquals("/1/2", bestMatch.getPatternString());
ServerWebExchange exchange = MockServerWebExchange.from(get(""));
this.handlerMapping.handleMatch(paths().build(), handlerMethod, exchange);
PathPattern pattern = (PathPattern) exchange.getAttributes().get(BEST_MATCHING_PATTERN_ATTRIBUTE);
assertEquals("", pattern.getPatternString());
}
@Test