Polishing contribution

Closes gh-29634
This commit is contained in:
rstoyanchev 2022-12-09 10:24:42 +00:00
parent 5ac97b16a8
commit 39d4d2041a
4 changed files with 20 additions and 30 deletions

View File

@ -170,8 +170,11 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos, protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos,
ServerWebExchange exchange) throws Exception { ServerWebExchange exchange) throws Exception {
PartialMatchHelper helper = PartialMatchHelper.from(infos, exchange); if (CollectionUtils.isEmpty(infos)) {
return null;
}
PartialMatchHelper helper = new PartialMatchHelper(infos, exchange);
if (helper.isEmpty()) { if (helper.isEmpty()) {
return null; return null;
} }
@ -222,12 +225,10 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
*/ */
private static final class PartialMatchHelper { private static final class PartialMatchHelper {
private static final PartialMatchHelper EMPTY_HELPER = new PartialMatchHelper(Collections.emptySet(), null);
private final List<PartialMatch> partialMatches = new ArrayList<>(); private final List<PartialMatch> partialMatches = new ArrayList<>();
private PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) { PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) {
for (RequestMappingInfo info : infos) { for (RequestMappingInfo info : infos) {
if (info.getPatternsCondition().getMatchingCondition(exchange) != null) { if (info.getPatternsCondition().getMatchingCondition(exchange) != null) {
this.partialMatches.add(new PartialMatch(info, exchange)); this.partialMatches.add(new PartialMatch(info, exchange));
@ -235,13 +236,6 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
} }
} }
public static PartialMatchHelper from(Set<RequestMappingInfo> infos, ServerWebExchange exchange) {
if (CollectionUtils.isEmpty(infos)) {
return EMPTY_HELPER;
}
return new PartialMatchHelper(infos, exchange);
}
/** /**
* Whether there are any partial matches. * Whether there are any partial matches.
*/ */

View File

@ -346,9 +346,9 @@ public class RequestMappingInfoHandlerMappingTests {
} }
@Test @Test // gh-29611
public void handleNoMatchEmptyRequestMappingInfo() throws Exception { public void handleNoMatchWithoutPartialMatches() throws Exception {
ServerWebExchange exchange = MockServerWebExchange.from(post("/bar")); ServerWebExchange exchange = MockServerWebExchange.from(post("/non-existent"));
HandlerMethod handlerMethod = this.handlerMapping.handleNoMatch(new HashSet<>(), exchange); HandlerMethod handlerMethod = this.handlerMapping.handleNoMatch(new HashSet<>(), exchange);
assertThat(handlerMethod).isNull(); assertThat(handlerMethod).isNull();

View File

@ -246,7 +246,11 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
protected HandlerMethod handleNoMatch( protected HandlerMethod handleNoMatch(
Set<RequestMappingInfo> infos, String lookupPath, HttpServletRequest request) throws ServletException { Set<RequestMappingInfo> infos, String lookupPath, HttpServletRequest request) throws ServletException {
PartialMatchHelper helper = PartialMatchHelper.from(infos, request); if (CollectionUtils.isEmpty(infos)) {
return null;
}
PartialMatchHelper helper = new PartialMatchHelper(infos, request);
if (helper.isEmpty()) { if (helper.isEmpty()) {
return null; return null;
} }
@ -295,11 +299,9 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
*/ */
private static final class PartialMatchHelper { private static final class PartialMatchHelper {
private static final PartialMatchHelper EMPTY_HELPER = new PartialMatchHelper(Collections.emptySet(), null);
private final List<PartialMatch> partialMatches = new ArrayList<>(); private final List<PartialMatch> partialMatches = new ArrayList<>();
private PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request) { PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request) {
for (RequestMappingInfo info : infos) { for (RequestMappingInfo info : infos) {
if (info.getActivePatternsCondition().getMatchingCondition(request) != null) { if (info.getActivePatternsCondition().getMatchingCondition(request) != null) {
this.partialMatches.add(new PartialMatch(info, request)); this.partialMatches.add(new PartialMatch(info, request));
@ -307,13 +309,6 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
} }
} }
public static PartialMatchHelper from(Set<RequestMappingInfo> infos, HttpServletRequest request) {
if (CollectionUtils.isEmpty(infos)) {
return EMPTY_HELPER;
}
return new PartialMatchHelper(infos, request);
}
/** /**
* Whether there are any partial matches. * Whether there are any partial matches.
*/ */

View File

@ -398,14 +398,15 @@ class RequestMappingInfoHandlerMappingTests {
assertThat(uriVariables.get("cars")).isEqualTo("cars"); assertThat(uriVariables.get("cars")).isEqualTo("cars");
} }
@PathPatternsParameterizedTest @PathPatternsParameterizedTest // gh-29611
void handleNoMatchEmptyRequestMappingInfo(TestRequestMappingInfoHandlerMapping mapping) throws ServletException { void handleNoMatchWithoutPartialMatches(TestRequestMappingInfoHandlerMapping mapping) throws ServletException {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/cars;color=green"); String path = "/non-existent";
MockHttpServletRequest request = new MockHttpServletRequest("GET", path);
HandlerMethod handlerMethod = mapping.handleNoMatch(new HashSet<>(), "/{cars}", request); HandlerMethod handlerMethod = mapping.handleNoMatch(new HashSet<>(), path, request);
assertThat(handlerMethod).isNull(); assertThat(handlerMethod).isNull();
handlerMethod = mapping.handleNoMatch(null, "/{cars}", request); handlerMethod = mapping.handleNoMatch(null, path, request);
assertThat(handlerMethod).isNull(); assertThat(handlerMethod).isNull();
} }