Skip to content

Commit bea5016

Browse files
committed
fixed validation test failures
1 parent 84be348 commit bea5016

File tree

2 files changed

+22
-48
lines changed

2 files changed

+22
-48
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolver.java

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,22 @@ else if ("javax.servlet.http.Part".equals(parameter.getParameterType().getName()
135135
try {
136136
HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, partName);
137137
arg = readWithMessageConverters(inputMessage, parameter, parameter.getParameterType());
138-
Annotation[] annotations = parameter.getParameterAnnotations();
139-
for (Annotation annot : annotations) {
140-
if ("Valid".equals(annot.annotationType().getSimpleName())) {
141-
WebDataBinder binder = binderFactory.createBinder(request, arg, partName);
142-
Object hints = AnnotationUtils.getValue(annot);
143-
binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
144-
BindingResult bindingResult = binder.getBindingResult();
145-
if (bindingResult.hasErrors()) {
146-
throw new MethodArgumentNotValidException(parameter, bindingResult);
138+
if (arg != null) {
139+
Annotation[] annotations = parameter.getParameterAnnotations();
140+
for (Annotation annot : annotations) {
141+
if ("Valid".equals(annot.annotationType().getSimpleName())) {
142+
WebDataBinder binder = binderFactory.createBinder(request, arg, partName);
143+
Object hints = AnnotationUtils.getValue(annot);
144+
binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
145+
BindingResult bindingResult = binder.getBindingResult();
146+
if (bindingResult.hasErrors()) {
147+
throw new MethodArgumentNotValidException(parameter, bindingResult);
148+
}
147149
}
148150
}
149151
}
150152
}
151-
catch (MissingServletRequestPartException e) {
153+
catch (MissingServletRequestPartException ex) {
152154
// handled below
153155
arg = null;
154156
}
@@ -193,26 +195,5 @@ private boolean isMultipartFileCollection(MethodParameter parameter) {
193195
}
194196
return false;
195197
}
196-
197-
/**
198-
* Whether to validate the given {@code @RequestPart} method argument.
199-
* The default implementation looks for {@code @javax.validation.Valid}.
200-
* @param argument the resolved argument value
201-
* @param parameter the method argument
202-
*/
203-
protected boolean isValidationApplicable(Object argument, MethodParameter parameter) {
204-
if (argument == null) {
205-
return false;
206-
}
207-
else {
208-
Annotation[] annotations = parameter.getParameterAnnotations();
209-
for (Annotation annot : annotations) {
210-
if ("Valid".equals(annot.annotationType().getSimpleName())) {
211-
return true;
212-
}
213-
}
214-
return false;
215-
}
216-
}
217198

218199
}

org.springframework.web/src/test/java/org/springframework/web/method/annotation/support/ModelAttributeMethodProcessorTests.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,13 @@
1616

1717
package org.springframework.web.method.annotation.support;
1818

19-
import static java.lang.annotation.ElementType.CONSTRUCTOR;
20-
import static java.lang.annotation.ElementType.FIELD;
21-
import static java.lang.annotation.ElementType.METHOD;
22-
import static java.lang.annotation.ElementType.PARAMETER;
23-
import static java.lang.annotation.RetentionPolicy.RUNTIME;
24-
import static org.easymock.EasyMock.anyObject;
25-
import static org.easymock.EasyMock.createMock;
26-
import static org.easymock.EasyMock.eq;
27-
import static org.easymock.EasyMock.expect;
28-
import static org.easymock.EasyMock.notNull;
29-
import static org.easymock.EasyMock.replay;
30-
import static org.easymock.EasyMock.verify;
31-
import static org.junit.Assert.assertEquals;
32-
import static org.junit.Assert.assertFalse;
33-
import static org.junit.Assert.assertSame;
34-
import static org.junit.Assert.assertTrue;
35-
3619
import java.lang.annotation.Retention;
3720
import java.lang.annotation.Target;
3821
import java.lang.reflect.Method;
3922

4023
import org.junit.Before;
4124
import org.junit.Test;
25+
4226
import org.springframework.beans.TestBean;
4327
import org.springframework.core.MethodParameter;
4428
import org.springframework.mock.web.MockHttpServletRequest;
@@ -54,6 +38,11 @@
5438
import org.springframework.web.context.request.WebRequest;
5539
import org.springframework.web.method.support.ModelAndViewContainer;
5640

41+
import static java.lang.annotation.ElementType.*;
42+
import static java.lang.annotation.RetentionPolicy.*;
43+
import static org.easymock.EasyMock.*;
44+
import static org.junit.Assert.*;
45+
5746
/**
5847
* Test fixture with {@link ModelAttributeMethodProcessor}.
5948
*
@@ -254,6 +243,10 @@ public void bind(WebRequest request) {
254243
public void validate() {
255244
validateInvoked = true;
256245
}
246+
247+
public void validate(Object... validationHints) {
248+
validateInvoked = true;
249+
}
257250
}
258251

259252
@Target({ METHOD, FIELD, CONSTRUCTOR, PARAMETER })

0 commit comments

Comments
 (0)