Skip to content
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

Exclude Part from nested constructor binding in WebFlux #31778

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.Map;
import java.util.TreeMap;

import org.springframework.core.MethodParameter;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Mono;

import org.springframework.beans.MutablePropertyValues;
Expand Down Expand Up @@ -87,6 +89,13 @@ public Mono<Void> construct(ServerWebExchange exchange) {
.then();
}

@Override
protected boolean shouldConstructArgument(MethodParameter param) {
Class<?> type = param.nestedIfOptional().getNestedParameterType();
return (super.shouldConstructArgument(param) &&
!MultipartFile.class.isAssignableFrom(type) && !Part.class.isAssignableFrom(type));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a necessary change indeed for Part but MultipartFile is not used in WebFlux, so we can leave that out.


/**
* Bind query parameters, form data, or multipart form data to the binder target.
* @param exchange the current exchange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.springframework.beans.testfixture.beans.ITestBean;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -219,6 +220,19 @@ public void testMultipart() throws Exception {
assertThat(bean.getSomePartList().get(1).filename()).isEqualTo("spring.png");
}

@Test
public void testConstructorMultipart() throws Exception {
WebExchangeDataBinder binder = new WebExchangeDataBinder(null);
binder.setTargetType(ResolvableType.forClass(ConstructorMultipartBean.class));

MultiValueMap<String, Object> data = new LinkedMultiValueMap<>();
data.add("part", new ClassPathResource("org/springframework/http/codec/multipart/foo.txt"));
binder.construct(exchangeMultipart(data)).block(Duration.ofMillis(5000));
ConstructorMultipartBean bean = (ConstructorMultipartBean) binder.getTarget();

assertThat(bean.getPart().filename()).isEqualTo("foo.txt");
assertThat(bean.getNullableFilePart()).isNull();
}


private ServerWebExchange exchange(MultiValueMap<String, String> formData) {
Expand Down Expand Up @@ -313,4 +327,24 @@ public void setSomePartList(List<FilePart> somePartList) {
}
}

private static class ConstructorMultipartBean {
private final FilePart part;
private final FilePart nullableFilePart;

public ConstructorMultipartBean(
FilePart part,
FilePart nullableFilePart
) {
this.part = part;
this.nullableFilePart = nullableFilePart;
}

public FilePart getPart() {
return part;
}

public FilePart getNullableFilePart() {
return nullableFilePart;
}
}
}