Skip to content

Commit 2d8315f

Browse files
committed
Reverse SPR-10402 change that caused 3.2.3 regression
SPR-10402 in Spring Framework 3.2.3 treated empty request parameter values as missing values, if the empty value was turned into a null by a PropertyEditor or Converter. This caused the regression. Issue: SPR-10578, SPR-10402, SPR-10584
1 parent 5feac07 commit 2d8315f

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -97,17 +97,11 @@ else if ("".equals(arg) && (namedValueInfo.defaultValue != null)) {
9797
arg = resolveDefaultValue(namedValueInfo.defaultValue);
9898
}
9999

100-
boolean emptyArgValue = "".equals(arg);
101-
102100
if (binderFactory != null) {
103101
WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
104102
arg = binder.convertIfNecessary(arg, paramType, parameter);
105103
}
106104

107-
if (emptyArgValue && (arg == null)) {
108-
handleMissingValue(namedValueInfo.name, parameter);
109-
}
110-
111105
handleResolvedValue(arg, namedValueInfo.name, parameter, mavContainer, webRequest);
112106

113107
return arg;

spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -49,6 +49,7 @@
4949
import static org.mockito.BDDMockito.*;
5050
import static org.mockito.Mockito.*;
5151

52+
5253
/**
5354
* Test fixture with {@link org.springframework.web.method.annotation.RequestParamMethodArgumentResolver}.
5455
*
@@ -70,6 +71,7 @@ public class RequestParamMethodArgumentResolverTests {
7071
private MethodParameter paramServlet30Part;
7172
private MethodParameter paramRequestPartAnnot;
7273
private MethodParameter paramRequired;
74+
private MethodParameter paramNotRequired;
7375

7476
private NativeWebRequest webRequest;
7577

@@ -81,8 +83,10 @@ public void setUp() throws Exception {
8183

8284
ParameterNameDiscoverer paramNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
8385

84-
Method method = getClass().getMethod("params", String.class, String[].class, Map.class, MultipartFile.class,
85-
Map.class, String.class, MultipartFile.class, List.class, Part.class, MultipartFile.class, String.class);
86+
Method method = getClass().getMethod("params", String.class, String[].class,
87+
Map.class, MultipartFile.class, Map.class, String.class,
88+
MultipartFile.class, List.class, Part.class, MultipartFile.class,
89+
String.class, String.class);
8690

8791
paramNamedDefaultValueString = new MethodParameter(method, 0);
8892
paramNamedStringArray = new MethodParameter(method, 1);
@@ -99,6 +103,7 @@ public void setUp() throws Exception {
99103
paramServlet30Part.initParameterNameDiscovery(paramNameDiscoverer);
100104
paramRequestPartAnnot = new MethodParameter(method, 9);
101105
paramRequired = new MethodParameter(method, 10);
106+
paramNotRequired = new MethodParameter(method, 11);
102107

103108
request = new MockHttpServletRequest();
104109
webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
@@ -243,9 +248,9 @@ public void missingRequestParam() throws Exception {
243248
fail("Expected exception");
244249
}
245250

246-
// SPR-10402
251+
// SPR-10578
247252

248-
@Test(expected = MissingServletRequestParameterException.class)
253+
@Test
249254
public void missingRequestParamEmptyValueConvertedToNull() throws Exception {
250255

251256
WebDataBinder binder = new WebRequestDataBinder(null);
@@ -256,8 +261,25 @@ public void missingRequestParamEmptyValueConvertedToNull() throws Exception {
256261

257262
this.request.addParameter("stringNotAnnot", "");
258263

259-
resolver.resolveArgument(paramStringNotAnnot, null, webRequest, binderFactory);
260-
fail("Expected exception");
264+
Object arg = resolver.resolveArgument(paramStringNotAnnot, null, webRequest, binderFactory);
265+
266+
assertNull(arg);
267+
}
268+
269+
@Test
270+
public void missingRequestParamEmptyValueNotRequired() throws Exception {
271+
272+
WebDataBinder binder = new WebRequestDataBinder(null);
273+
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
274+
275+
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
276+
given(binderFactory.createBinder(webRequest, null, "name")).willReturn(binder);
277+
278+
this.request.addParameter("name", "");
279+
280+
Object arg = resolver.resolveArgument(paramNotRequired, null, webRequest, binderFactory);
281+
282+
assertNull(arg);
261283
}
262284

263285
@Test
@@ -311,7 +333,8 @@ public void params(@RequestParam(value = "name", defaultValue = "bar") String pa
311333
List<MultipartFile> multipartFileList,
312334
Part servlet30Part,
313335
@RequestPart MultipartFile requestPartAnnot,
314-
@RequestParam(value = "name") String paramRequired) {
336+
@RequestParam(value = "name") String paramRequired,
337+
@RequestParam(value = "name", required=false) String paramNotRequired) {
315338
}
316339

317340
}

0 commit comments

Comments
 (0)