Skip to content

Commit 08eb623

Browse files
committed
Fix regression introduced in 4.3 snapshot
Commit ccd17d introduced a regression where a custom HTTP method would no longer match for an empty @RequestMapping condition. The previous behavior should now be restored. Effectively RequestMethodRequestCondition as before will now match to any HTTP method (even unknown/custom ones) if the methods condition is empty. The only exception is HTTP OPTIONS for which we provide default handling as a fallback (i.e. when not mapped explicitly). Issue: SPR-13130
1 parent 1c2ac49 commit 08eb623

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,26 @@ public RequestMethodsRequestCondition combine(RequestMethodsRequestCondition oth
9494
* Check if any of the HTTP request methods match the given request and
9595
* return an instance that contains the matching HTTP request method only.
9696
* @param request the current request
97-
* @return the same instance if the condition is empty, a new condition with
98-
* the matched request method, or {@code null} if no request methods match
97+
* @return the same instance if the condition is empty (unless the request
98+
* method is HTTP OPTIONS), a new condition with the matched request method,
99+
* or {@code null} if there is no match or the condition is empty and the
100+
* request method is OPTIONS.
99101
*/
100102
@Override
101103
public RequestMethodsRequestCondition getMatchingCondition(HttpServletRequest request) {
102104
RequestMethod requestMethod = getRequestMethod(request);
103-
if (requestMethod == null) {
104-
return null;
105-
}
106105
if (this.methods.isEmpty()) {
107106
return (RequestMethod.OPTIONS.equals(requestMethod) ? null : this);
108107
}
109-
for (RequestMethod method : this.methods) {
110-
if (method.equals(requestMethod)) {
111-
return new RequestMethodsRequestCondition(method);
108+
if (requestMethod != null) {
109+
for (RequestMethod method : this.methods) {
110+
if (method.equals(requestMethod)) {
111+
return new RequestMethodsRequestCondition(method);
112+
}
113+
}
114+
if (RequestMethod.HEAD.equals(requestMethod) && getMethods().contains(RequestMethod.GET)) {
115+
return HEAD_CONDITION;
112116
}
113-
}
114-
if (RequestMethod.HEAD.equals(requestMethod) && getMethods().contains(RequestMethod.GET)) {
115-
return HEAD_CONDITION;
116117
}
117118
return null;
118119
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ public void methodHeadNoMatch() throws Exception {
8282
}
8383

8484
@Test
85-
public void noDeclaredMethodsMatchesAllMethodsExceptOptions() {
85+
public void emptyMatchesAnythingExceptHttpOptions() {
8686
RequestCondition condition = new RequestMethodsRequestCondition();
8787

8888
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", "")));
8989
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("POST", "")));
9090
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("HEAD", "")));
91+
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("CUSTOM", "")));
9192
assertNull(condition.getMatchingCondition(new MockHttpServletRequest("OPTIONS", "")));
9293
}
9394

0 commit comments

Comments
 (0)