Browse Source

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
pull/973/head
Rossen Stoyanchev 10 years ago
parent
commit
08eb623c41
  1. 11
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java
  2. 3
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java

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

@ -94,18 +94,18 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi @@ -94,18 +94,18 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi
* Check if any of the HTTP request methods match the given request and
* return an instance that contains the matching HTTP request method only.
* @param request the current request
* @return the same instance if the condition is empty, a new condition with
* the matched request method, or {@code null} if no request methods match
* @return the same instance if the condition is empty (unless the request
* method is HTTP OPTIONS), a new condition with the matched request method,
* or {@code null} if there is no match or the condition is empty and the
* request method is OPTIONS.
*/
@Override
public RequestMethodsRequestCondition getMatchingCondition(HttpServletRequest request) {
RequestMethod requestMethod = getRequestMethod(request);
if (requestMethod == null) {
return null;
}
if (this.methods.isEmpty()) {
return (RequestMethod.OPTIONS.equals(requestMethod) ? null : this);
}
if (requestMethod != null) {
for (RequestMethod method : this.methods) {
if (method.equals(requestMethod)) {
return new RequestMethodsRequestCondition(method);
@ -114,6 +114,7 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi @@ -114,6 +114,7 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi
if (RequestMethod.HEAD.equals(requestMethod) && getMethods().contains(RequestMethod.GET)) {
return HEAD_CONDITION;
}
}
return null;
}

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

@ -82,12 +82,13 @@ public class RequestMethodsRequestConditionTests { @@ -82,12 +82,13 @@ public class RequestMethodsRequestConditionTests {
}
@Test
public void noDeclaredMethodsMatchesAllMethodsExceptOptions() {
public void emptyMatchesAnythingExceptHttpOptions() {
RequestCondition condition = new RequestMethodsRequestCondition();
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", "")));
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("POST", "")));
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("HEAD", "")));
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("CUSTOM", "")));
assertNull(condition.getMatchingCondition(new MockHttpServletRequest("OPTIONS", "")));
}

Loading…
Cancel
Save