Closed
Description
In my code I am doing something like:
mockMvc.multipart(DOCUMENTS, uuid) {
file(MockMultipartFile("file", "test.txt", "text/plain", "Test".toByteArray()))
part(MockPart("data", """{"node":"node"}""".toByteArray()))
}
which is supposed to create a request with both a part, named data
, and a file, named file
.
This was supported in Spring 5.2, but it is not supported anymore in Spring 5.3 since #25602
I am not sure if it was a "wrong" use case, not meant to be working, or if the fix introduced in the linked PR is a regression, since TestDispatcherServlet
simply creates a StandardMultipartHttpServletRequest
without copying the files over, as MockMultipartHttpServletRequestBuilder
was originally doing.
https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java#L95 simply copies the parts
, but not the files
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
rstoyanchev commentedon Nov 30, 2020
Can you provide more details around the failure and the target controller method? Also have you tried providing the file as a part, something like this (headers can also be set via setters):
ThanksForAllTheFish commentedon Dec 2, 2020
@rstoyanchev yes, I indeed tried with multiple
MockPart
s and that passes to my controller method the expected values. I could definitely useMockPart
instead ofMockMultipartFile
.As for the controller method I expect to be invoked:
this is it. Since #25602,
file
is null when bothpart
andfile
are used in the test.Just to be clear, it is absolutely not a problem to use
MockPart
, it just felt weird that the two can be combined but thenfile
ends up being discarded and wanted to double check if the change in the PR really led to the wanted result.rstoyanchev commentedon Dec 3, 2020
Indeed, this was not intended. In
MockMultipartHttpServletRequestBuilder
, when both files and parts are registered, we can turn the files to parts with a filename and effectively register everything as parts.[-]Failed to create a mock request with file and parts[/-][+]MockMvc ignores MultipartFile registrations when both files and parts are registered[/+]5 remaining items