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

Closed
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
10 changes: 5 additions & 5 deletions src/main/java/org/springframework/data/web/ProjectedPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
*/
package org.springframework.data.web;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation to mark projection interfaces that are supposed to be used as projection interface to bind request or
* response payloads to.
*
* @author Oliver Gierke
* @author Chris Bono
* @soundtrack
* @since 1.13
*/
@Documented
@Retention(RUNTIME)
@Target(TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.PARAMETER })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only place I see this documented is in spring-data-commons/src/main/antora/modules/ROOT/pages/repositories/core-extensions.adoc and was not sure if we wanted to add this ability in there as it is pretty high-level. Wdyt?

public @interface ProjectedPayload {
}
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)
if ((parameter.hasParameterAnnotation(ProjectedPayload.class) || parameter.hasParameterAnnotation(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Long-term (in 4.0) we will further investigate why this resolver does not support multipart file parameters. Until then, now that we allow @ProjectedPayload at the parameter level, we need to be sure to not handle @ProjectedPayload MultipartFile nor @ModelAttribute MultipartFile.

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,30 @@ 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();
}

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

var parameter = getParameter("withProjectedPayload", SampleInterface.class);

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

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

var parameter = getParameter("withProjectedPayloadMultipart", 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 +138,11 @@ interface Controller {
void withForeignAnnotation(@Autowired SampleInterface param);

void withModelAttribute(@ModelAttribute SampleInterface param);

void withModelAttributeMultipart(@ModelAttribute MultipartFile file);

void withProjectedPayload(@ProjectedPayload SampleInterface param);

void withProjectedPayloadMultipart(@ProjectedPayload MultipartFile file);
}
}