Skip to content

Commit 64ee5e5

Browse files
committed
Fix bug with custom RequestCondition
A custom RequestCondition which can be provided by overriding methods in RequestMappingHandlerMapping worked only for conditions that match and did not return null (as it should have) for conditions that don't match. Issues: SPR-9134
1 parent 9833a4c commit 64ee5e5

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@
2525
/**
2626
* A holder for a {@link RequestCondition} useful when the type of the held
2727
* request condition is not known ahead of time - e.g. custom condition.
28-
*
29-
* <p>An implementation of {@code RequestCondition} itself, a
30-
* {@code RequestConditionHolder} decorates the held request condition allowing
31-
* it to be combined and compared with other custom request conditions while
28+
*
29+
* <p>An implementation of {@code RequestCondition} itself, a
30+
* {@code RequestConditionHolder} decorates the held request condition allowing
31+
* it to be combined and compared with other custom request conditions while
3232
* ensuring type and null safety.
33-
*
33+
*
3434
* @author Rossen Stoyanchev
3535
* @since 3.1
3636
*/
3737
public final class RequestConditionHolder extends AbstractRequestCondition<RequestConditionHolder> {
3838

3939
@SuppressWarnings("rawtypes")
4040
private final RequestCondition condition;
41-
41+
4242
/**
43-
* Create a new holder to wrap the given request condition.
43+
* Create a new holder to wrap the given request condition.
4444
* @param requestCondition the condition to hold, may be {@code null}
4545
*/
4646
public RequestConditionHolder(RequestCondition<?> requestCondition) {
@@ -65,7 +65,7 @@ protected String getToStringInfix() {
6565
}
6666

6767
/**
68-
* Combine the request conditions held by the two RequestConditionHolder
68+
* Combine the request conditions held by the two RequestConditionHolder
6969
* instances after making sure the conditions are of the same type.
7070
* Or if one holder is empty, the other holder is returned.
7171
*/
@@ -97,9 +97,9 @@ private void assertIsCompatible(RequestConditionHolder other) {
9797
throw new ClassCastException("Incompatible request conditions: " + clazz + " and " + otherClazz);
9898
}
9999
}
100-
100+
101101
/**
102-
* Get the matching condition for the held request condition wrap it in a
102+
* Get the matching condition for the held request condition wrap it in a
103103
* new RequestConditionHolder instance. Or otherwise if this is an empty
104104
* holder, return the same holder instance.
105105
*/
@@ -108,11 +108,11 @@ public RequestConditionHolder getMatchingCondition(HttpServletRequest request) {
108108
return this;
109109
}
110110
RequestCondition<?> match = (RequestCondition<?>) condition.getMatchingCondition(request);
111-
return new RequestConditionHolder(match);
111+
return (match != null) ? new RequestConditionHolder(match) : null;
112112
}
113113

114114
/**
115-
* Compare the request conditions held by the two RequestConditionHolder
115+
* Compare the request conditions held by the two RequestConditionHolder
116116
* instances after making sure the conditions are of the same type.
117117
* Or if one holder is empty, the other holder is preferred.
118118
*/
@@ -132,5 +132,5 @@ else if (other.condition == null) {
132132
return condition.compareTo(other.condition, request);
133133
}
134134
}
135-
135+
136136
}

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,19 @@
1717
package org.springframework.web.servlet.mvc.condition;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNull;
2021
import static org.junit.Assert.assertSame;
2122

2223
import javax.servlet.http.HttpServletRequest;
2324

2425
import org.junit.Test;
2526
import org.springframework.mock.web.MockHttpServletRequest;
2627
import org.springframework.web.bind.annotation.RequestMethod;
27-
import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition;
28-
import org.springframework.web.servlet.mvc.condition.ParamsRequestCondition;
29-
import org.springframework.web.servlet.mvc.condition.RequestConditionHolder;
30-
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
3128

3229
/**
33-
* A test fixture for
30+
* A test fixture for
3431
* {code org.springframework.web.servlet.mvc.method.RequestConditionHolder} tests.
35-
*
32+
*
3633
* @author Rossen Stoyanchev
3734
*/
3835
public class RequestConditionHolderTests {
@@ -41,7 +38,7 @@ public class RequestConditionHolderTests {
4138
public void combineEmpty() {
4239
RequestConditionHolder empty = new RequestConditionHolder(null);
4340
RequestConditionHolder notEmpty = new RequestConditionHolder(new ParamsRequestCondition("name"));
44-
41+
4542
assertSame(empty, empty.combine(new RequestConditionHolder(null)));
4643
assertSame(notEmpty, notEmpty.combine(empty));
4744
assertSame(notEmpty, empty.combine(notEmpty));
@@ -52,7 +49,7 @@ public void combine() {
5249
RequestConditionHolder params1 = new RequestConditionHolder(new ParamsRequestCondition("name1"));
5350
RequestConditionHolder params2 = new RequestConditionHolder(new ParamsRequestCondition("name2"));
5451
RequestConditionHolder expected = new RequestConditionHolder(new ParamsRequestCondition("name1", "name2"));
55-
52+
5653
assertEquals(expected, params1.combine(params2));
5754
}
5855

@@ -67,14 +64,24 @@ public void combineIncompatible() {
6764
public void match() {
6865
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
6966
request.setParameter("name1", "value1");
70-
67+
7168
RequestMethodsRequestCondition rm = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST);
7269
RequestConditionHolder custom = new RequestConditionHolder(rm);
7370
RequestMethodsRequestCondition expected = new RequestMethodsRequestCondition(RequestMethod.GET);
74-
71+
7572
assertEquals(expected, custom.getMatchingCondition(request).getCondition());
7673
}
77-
74+
75+
@Test
76+
public void noMatch() {
77+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
78+
79+
RequestMethodsRequestCondition rm = new RequestMethodsRequestCondition(RequestMethod.POST);
80+
RequestConditionHolder custom = new RequestConditionHolder(rm);
81+
82+
assertNull(custom.getMatchingCondition(request));
83+
}
84+
7885
@Test
7986
public void matchEmpty() {
8087
RequestConditionHolder empty = new RequestConditionHolder(null);
@@ -91,7 +98,7 @@ public void compare() {
9198
assertEquals(1, params11.compareTo(params12, request));
9299
assertEquals(-1, params12.compareTo(params11, request));
93100
}
94-
101+
95102
@Test
96103
public void compareEmpty() {
97104
HttpServletRequest request = new MockHttpServletRequest();

0 commit comments

Comments
 (0)