Skip to content

Commit

Permalink
Optimize object creation PartialMatchHelper
Browse files Browse the repository at this point in the history
Closes gh-29667
  • Loading branch information
rstoyanchev committed Dec 9, 2022
1 parent 7228503 commit 525fc7a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
Expand Down Expand Up @@ -165,8 +166,11 @@ protected void handleMatch(RequestMappingInfo info, HandlerMethod handlerMethod,
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos,
ServerWebExchange exchange) throws Exception {

PartialMatchHelper helper = new PartialMatchHelper(infos, exchange);
if (CollectionUtils.isEmpty(infos)) {
return null;
}

PartialMatchHelper helper = new PartialMatchHelper(infos, exchange);
if (helper.isEmpty()) {
return null;
}
Expand Down Expand Up @@ -218,15 +222,14 @@ private static class PartialMatchHelper {

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


public PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) {
this.partialMatches.addAll(infos.stream().
filter(info -> info.getPatternsCondition().getMatchingCondition(exchange) != null).
map(info -> new PartialMatch(info, exchange)).
collect(Collectors.toList()));
PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) {
for (RequestMappingInfo info : infos) {
if (info.getPatternsCondition().getMatchingCondition(exchange) != null) {
this.partialMatches.add(new PartialMatch(info, exchange));
}
}
}


/**
* Whether there are any partial matches.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
Expand All @@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -334,9 +335,18 @@ public void handlePatchUnsupportedMediaType() {
assertThat(umtse.getResponseHeaders().getAcceptPatch()).containsExactly(mediaType);
})
.verify();

}

@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();

handlerMethod = this.handlerMapping.handleNoMatch(null, exchange);
assertThat(handlerMethod).isNull();
}

@SuppressWarnings("unchecked")
private <T> void assertError(Mono<Object> mono, final Class<T> exceptionClass, final Consumer<T> consumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ private Map<String, MultiValueMap<String, String>> extractMatrixVariables(
protected HandlerMethod handleNoMatch(
Set<RequestMappingInfo> infos, String lookupPath, HttpServletRequest request) throws ServletException {

if (CollectionUtils.isEmpty(infos)) {
return null;
}

PartialMatchHelper helper = new PartialMatchHelper(infos, request);
if (helper.isEmpty()) {
return null;
Expand Down Expand Up @@ -291,7 +295,7 @@ private static class PartialMatchHelper {

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

public 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
Expand Down Expand Up @@ -389,6 +389,18 @@ void handleMatchMatrixVariablesDecoding(TestRequestMappingInfoHandlerMapping map
assertThat(uriVariables.get("cars")).isEqualTo("cars");
}

@PathPatternsParameterizedTest // gh-29611
void handleNoMatchWithoutPartialMatches(TestRequestMappingInfoHandlerMapping mapping) throws Exception {
String path = "/non-existent";
MockHttpServletRequest request = new MockHttpServletRequest("GET", path);

HandlerMethod handlerMethod = mapping.handleNoMatch(new HashSet<>(), path, request);
assertThat(handlerMethod).isNull();

handlerMethod = mapping.handleNoMatch(null, path, request);
assertThat(handlerMethod).isNull();
}

private HandlerMethod getHandler(
TestRequestMappingInfoHandlerMapping mapping, MockHttpServletRequest request) throws Exception {

Expand Down

0 comments on commit 525fc7a

Please sign in to comment.