Skip to content

Commit 525fc7a

Browse files
committed
Optimize object creation PartialMatchHelper
Closes gh-29667
1 parent 7228503 commit 525fc7a

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.http.server.PathContainer;
3737
import org.springframework.http.server.reactive.ServerHttpRequest;
3838
import org.springframework.util.Assert;
39+
import org.springframework.util.CollectionUtils;
3940
import org.springframework.util.MultiValueMap;
4041
import org.springframework.web.bind.annotation.RequestMethod;
4142
import org.springframework.web.method.HandlerMethod;
@@ -165,8 +166,11 @@ protected void handleMatch(RequestMappingInfo info, HandlerMethod handlerMethod,
165166
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos,
166167
ServerWebExchange exchange) throws Exception {
167168

168-
PartialMatchHelper helper = new PartialMatchHelper(infos, exchange);
169+
if (CollectionUtils.isEmpty(infos)) {
170+
return null;
171+
}
169172

173+
PartialMatchHelper helper = new PartialMatchHelper(infos, exchange);
170174
if (helper.isEmpty()) {
171175
return null;
172176
}
@@ -218,15 +222,14 @@ private static class PartialMatchHelper {
218222

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

221-
222-
public PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) {
223-
this.partialMatches.addAll(infos.stream().
224-
filter(info -> info.getPatternsCondition().getMatchingCondition(exchange) != null).
225-
map(info -> new PartialMatch(info, exchange)).
226-
collect(Collectors.toList()));
225+
PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) {
226+
for (RequestMappingInfo info : infos) {
227+
if (info.getPatternsCondition().getMatchingCondition(exchange) != null) {
228+
this.partialMatches.add(new PartialMatch(info, exchange));
229+
}
230+
}
227231
}
228232

229-
230233
/**
231234
* Whether there are any partial matches.
232235
*/

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.util.Arrays;
2323
import java.util.Collections;
2424
import java.util.EnumSet;
25+
import java.util.HashSet;
2526
import java.util.List;
2627
import java.util.Map;
2728
import java.util.Set;
@@ -334,9 +335,18 @@ public void handlePatchUnsupportedMediaType() {
334335
assertThat(umtse.getResponseHeaders().getAcceptPatch()).containsExactly(mediaType);
335336
})
336337
.verify();
337-
338338
}
339339

340+
@Test // gh-29611
341+
public void handleNoMatchWithoutPartialMatches() throws Exception {
342+
ServerWebExchange exchange = MockServerWebExchange.from(post("/non-existent"));
343+
344+
HandlerMethod handlerMethod = this.handlerMapping.handleNoMatch(new HashSet<>(), exchange);
345+
assertThat(handlerMethod).isNull();
346+
347+
handlerMethod = this.handlerMapping.handleNoMatch(null, exchange);
348+
assertThat(handlerMethod).isNull();
349+
}
340350

341351
@SuppressWarnings("unchecked")
342352
private <T> void assertError(Mono<Object> mono, final Class<T> exceptionClass, final Consumer<T> consumer) {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ private Map<String, MultiValueMap<String, String>> extractMatrixVariables(
241241
protected HandlerMethod handleNoMatch(
242242
Set<RequestMappingInfo> infos, String lookupPath, HttpServletRequest request) throws ServletException {
243243

244+
if (CollectionUtils.isEmpty(infos)) {
245+
return null;
246+
}
247+
244248
PartialMatchHelper helper = new PartialMatchHelper(infos, request);
245249
if (helper.isEmpty()) {
246250
return null;
@@ -291,7 +295,7 @@ private static class PartialMatchHelper {
291295

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

294-
public PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request) {
298+
PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request) {
295299
for (RequestMappingInfo info : infos) {
296300
if (info.getActivePatternsCondition().getMatchingCondition(request) != null) {
297301
this.partialMatches.add(new PartialMatch(info, request));

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -389,6 +389,18 @@ void handleMatchMatrixVariablesDecoding(TestRequestMappingInfoHandlerMapping map
389389
assertThat(uriVariables.get("cars")).isEqualTo("cars");
390390
}
391391

392+
@PathPatternsParameterizedTest // gh-29611
393+
void handleNoMatchWithoutPartialMatches(TestRequestMappingInfoHandlerMapping mapping) throws Exception {
394+
String path = "/non-existent";
395+
MockHttpServletRequest request = new MockHttpServletRequest("GET", path);
396+
397+
HandlerMethod handlerMethod = mapping.handleNoMatch(new HashSet<>(), path, request);
398+
assertThat(handlerMethod).isNull();
399+
400+
handlerMethod = mapping.handleNoMatch(null, path, request);
401+
assertThat(handlerMethod).isNull();
402+
}
403+
392404
private HandlerMethod getHandler(
393405
TestRequestMappingInfoHandlerMapping mapping, MockHttpServletRequest request) throws Exception {
394406

0 commit comments

Comments
 (0)