Skip to content

Fixes #25829 by changing the check away from instanceof. Now checking… #25830

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
wants to merge 1 commit into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -81,7 +83,13 @@ public HttpHeaders getHeaders() {

@Override
public InputStream getBody() throws IOException {
if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) {
Collection<Part> parts = null;
try {
parts = this.multipartRequest.getParts();
} catch (ServletException e) {
//ignoring this as it might be a non multipart/form-data Content-Type
}
if (parts != null && !parts.isEmpty()) {
try {
return this.multipartRequest.getPart(this.partName).getInputStream();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.catalina.Server;
import org.junit.jupiter.api.Test;

import org.springframework.http.HttpHeaders;
Expand All @@ -32,6 +33,7 @@
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.testfixture.servlet.MockMultipartFile;
import org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockPart;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -137,4 +139,23 @@ public HttpHeaders getMultipartHeaders(String paramOrFileName) {
assertThat(result).isEqualTo(bytes);
}

@Test //#25829
public void getBodyViaRequestPart() throws Exception {
MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest() {
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return headers;
}
};
byte[] bytes = "Hello".getBytes();
MockPart mockPart = new MockPart("part", bytes);
mockPart.getHeaders().setContentType(MediaType.TEXT_PLAIN);
mockRequest.addPart(mockPart);
ServerHttpRequest request = new RequestPartServletServerHttpRequest(mockRequest, "part");
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
assertThat(result).isEqualTo(bytes);
}

}