Skip to content

Commit 50ed578

Browse files
committed
Avoid matching multipart parameters annotated with @ModelAttribute
The ProxyHandlerMethodArgumentResolver now avoids matching multipart parameters annotated with @ModelAttribute. This allows multipart parameters to be handled by RequestParamMethodArgumentResolver which properly handles multipart arguments. Fixes spring-projects#3258 Related tickets spring-projects#2937 Signed-off-by: Chris Bono <[email protected]>
1 parent e17dd32 commit 50ed578

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/main/java/org/springframework/data/web/ProxyingHandlerMethodArgumentResolver.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
import org.springframework.web.context.request.NativeWebRequest;
3737
import org.springframework.web.method.annotation.ModelAttributeMethodProcessor;
3838
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
39+
import org.springframework.web.multipart.support.MultipartResolutionDelegate;
3940

4041
/**
4142
* {@link HandlerMethodArgumentResolver} to create Proxy instances for interface based controller method parameters.
4243
*
4344
* @author Oliver Gierke
45+
* @author Chris Bono
4446
* @since 1.10
4547
*/
4648
public class ProxyingHandlerMethodArgumentResolver extends ModelAttributeMethodProcessor
@@ -88,9 +90,9 @@ public boolean supportsParameter(MethodParameter parameter) {
8890
return false;
8991
}
9092

91-
// Annotated parameter
92-
if (parameter.getParameterAnnotation(ProjectedPayload.class) != null
93-
|| parameter.getParameterAnnotation(ModelAttribute.class) != null) {
93+
// Annotated parameter (excluding multipart @ModelAttribute)
94+
if (parameter.hasParameterAnnotation(ProjectedPayload.class) ||
95+
(parameter.hasParameterAnnotation(ModelAttribute.class) && !MultipartResolutionDelegate.isMultipartArgument(parameter))) {
9496
return true;
9597
}
9698

src/test/java/org/springframework/data/web/ProxyingHandlerMethodArgumentResolverUnitTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
import org.springframework.core.convert.support.DefaultConversionService;
2828
import org.springframework.util.ReflectionUtils;
2929
import org.springframework.web.bind.annotation.ModelAttribute;
30+
import org.springframework.web.multipart.MultipartFile;
3031

3132
/**
3233
* Unit tests for {@link ProxyingHandlerMethodArgumentResolver}.
3334
*
3435
* @author Oliver Gierke
36+
* @author Chris Bono
3537
* @soundtrack Karlijn Langendijk & Sönke Meinen - Englishman In New York (Sting,
3638
* https://www.youtube.com/watch?v=O7LZsqrnaaA)
3739
*/
@@ -88,6 +90,14 @@ void doesSupportAtModelAttribute() throws Exception {
8890
assertThat(resolver.supportsParameter(parameter)).isTrue();
8991
}
9092

93+
@Test // GH-3258
94+
void doesNotSupportAtModelAttributeForMultipartParam() throws Exception {
95+
96+
var parameter = getParameter("withModelAttributeMultipart", MultipartFile.class);
97+
98+
assertThat(resolver.supportsParameter(parameter)).isFalse();
99+
}
100+
91101
private static MethodParameter getParameter(String methodName, Class<?> parameterType) {
92102

93103
var method = ReflectionUtils.findMethod(Controller.class, methodName, parameterType);
@@ -112,5 +122,7 @@ interface Controller {
112122
void withForeignAnnotation(@Autowired SampleInterface param);
113123

114124
void withModelAttribute(@ModelAttribute SampleInterface param);
125+
126+
void withModelAttributeMultipart(@ModelAttribute MultipartFile file);
115127
}
116128
}

0 commit comments

Comments
 (0)