Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
Closes gh-29634
  • Loading branch information
rstoyanchev committed Dec 9, 2022
1 parent 5ac97b1 commit 39d4d20
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 30 deletions.
Expand Up @@ -170,8 +170,11 @@ protected void handleMatch(RequestMappingInfo info, HandlerMethod handlerMethod,
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos,
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()) {
return null;
}
Expand Down Expand Up @@ -222,26 +225,17 @@ protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos,
*/
private static final class PartialMatchHelper {

private static final PartialMatchHelper EMPTY_HELPER = new PartialMatchHelper(Collections.emptySet(), null);

private final List<PartialMatch> partialMatches = new ArrayList<>();


private PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) {
PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) {
for (RequestMappingInfo info : infos) {
if (info.getPatternsCondition().getMatchingCondition(exchange) != null) {
this.partialMatches.add(new PartialMatch(info, exchange));
}
}
}

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.
*/
Expand Down
Expand Up @@ -346,9 +346,9 @@ public void handlePatchUnsupportedMediaType() {

}

@Test
public void handleNoMatchEmptyRequestMappingInfo() throws Exception {
ServerWebExchange exchange = MockServerWebExchange.from(post("/bar"));
@Test // gh-29611
public void handleNoMatchWithoutPartialMatches() throws Exception {
ServerWebExchange exchange = MockServerWebExchange.from(post("/non-existent"));

HandlerMethod handlerMethod = this.handlerMapping.handleNoMatch(new HashSet<>(), exchange);
assertThat(handlerMethod).isNull();
Expand Down
Expand Up @@ -246,7 +246,11 @@ private Map<String, MultiValueMap<String, String>> extractMatrixVariables(
protected HandlerMethod handleNoMatch(
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()) {
return null;
}
Expand Down Expand Up @@ -295,25 +299,16 @@ protected HandlerMethod handleNoMatch(
*/
private static final class PartialMatchHelper {

private static final PartialMatchHelper EMPTY_HELPER = new PartialMatchHelper(Collections.emptySet(), null);

private final List<PartialMatch> partialMatches = new ArrayList<>();

private PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request) {
PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request) {
for (RequestMappingInfo info : infos) {
if (info.getActivePatternsCondition().getMatchingCondition(request) != null) {
this.partialMatches.add(new PartialMatch(info, request));
}
}
}

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.
*/
Expand Down
Expand Up @@ -398,14 +398,15 @@ void handleMatchMatrixVariablesDecoding(TestRequestMappingInfoHandlerMapping map
assertThat(uriVariables.get("cars")).isEqualTo("cars");
}

@PathPatternsParameterizedTest
void handleNoMatchEmptyRequestMappingInfo(TestRequestMappingInfoHandlerMapping mapping) throws ServletException {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/cars;color=green");
@PathPatternsParameterizedTest // gh-29611
void handleNoMatchWithoutPartialMatches(TestRequestMappingInfoHandlerMapping mapping) throws ServletException {
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();

handlerMethod = mapping.handleNoMatch(null, "/{cars}", request);
handlerMethod = mapping.handleNoMatch(null, path, request);
assertThat(handlerMethod).isNull();
}

Expand Down

0 comments on commit 39d4d20

Please sign in to comment.