From 08eb623c41d0ba8276d4131330271ea0f49c7879 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 18 Feb 2016 14:37:36 -0500 Subject: [PATCH] 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 --- .../RequestMethodsRequestCondition.java | 23 ++++++++++--------- .../RequestMethodsRequestConditionTests.java | 3 ++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java index 0ff3d5337c..b5ef6cb67b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java @@ -94,25 +94,26 @@ 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); } - for (RequestMethod method : this.methods) { - if (method.equals(requestMethod)) { - return new RequestMethodsRequestCondition(method); + if (requestMethod != null) { + for (RequestMethod method : this.methods) { + if (method.equals(requestMethod)) { + return new RequestMethodsRequestCondition(method); + } + } + if (RequestMethod.HEAD.equals(requestMethod) && getMethods().contains(RequestMethod.GET)) { + return HEAD_CONDITION; } - } - if (RequestMethod.HEAD.equals(requestMethod) && getMethods().contains(RequestMethod.GET)) { - return HEAD_CONDITION; } return null; } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java index cddbd8f356..45de6ab031 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java @@ -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", ""))); }