Skip to content

Avoid matching multipart parameters annotated with @ModelAttribute #3277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.annotation.ModelAttributeMethodProcessor;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.multipart.support.MultipartResolutionDelegate;

/**
* {@link HandlerMethodArgumentResolver} to create Proxy instances for interface based controller method parameters.
*
* @author Oliver Gierke
* @author Chris Bono
* @since 1.10
*/
public class ProxyingHandlerMethodArgumentResolver extends ModelAttributeMethodProcessor
Expand Down Expand Up @@ -88,9 +90,9 @@ public boolean supportsParameter(MethodParameter parameter) {
return false;
}

// Annotated parameter
if (parameter.getParameterAnnotation(ProjectedPayload.class) != null
|| parameter.getParameterAnnotation(ModelAttribute.class) != null) {
// Annotated parameter (excluding multipart @ModelAttribute)
if (parameter.hasParameterAnnotation(ProjectedPayload.class) ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the proxy handler does not properly handle multipart arguments, but the request param method handler does properly support multipart params. Is this the proper fix or should the proxy handler also support multipart args properly? cc: @odrotbohm @mp911de

(parameter.hasParameterAnnotation(ModelAttribute.class) && !MultipartResolutionDelegate.isMultipartArgument(parameter))) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.multipart.MultipartFile;

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

@Test // GH-3258
void doesNotSupportAtModelAttributeForMultipartParam() throws Exception {

var parameter = getParameter("withModelAttributeMultipart", MultipartFile.class);

assertThat(resolver.supportsParameter(parameter)).isFalse();
}

private static MethodParameter getParameter(String methodName, Class<?> parameterType) {

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

void withModelAttribute(@ModelAttribute SampleInterface param);

void withModelAttributeMultipart(@ModelAttribute MultipartFile file);
}
}