-
Notifications
You must be signed in to change notification settings - Fork 737
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
Duplicate File Parts Generated in MockMvc using MockPart #953
Comments
This is my sample code to make
|
I wrote #954 as a reference. |
Thanks for the report, but I think this should be considered in Spring Framework in the first instance. As things stand, only Please open a Spring Framework issue, comment here with a link to it, and we can take things from there. |
In my initial assessment, I thought that restdocs-mockmvc already performed checks on I fully agree with your perspective, and that's precisely why I included this point in the Review Points. I will open an issue in the link you provided and include a comment there. |
The Framework team are best-placed to suggest something here. My hope is that they can provide a mechanism by which the files and parts can be retrieved without any duplication. |
I agree, I hope so too. I created an Issue in spring-framework |
Not at this time. We need to wait for an update from the Framework team. |
MockMultipartHttpServletRequest and MockMultipartHttpServletRequestBuilder Behavior have been found with the behavior of
MockMultipartHttpServletRequest
(spring-test) andMockMultipartHttpServletRequestBuilder
(spring-test).MockMultipartHttpServletRequest
internally managesmultipartFiles
as separate file types. When usingMockMultipartHttpServletRequestBuilder
to create a mock multipart request, files can be added using either the.part
method to add aMockPart
or the.file
method to add aMockMultipartFile
.The actual servlet creation process is implemented as follows:
As can be seen in the code, files added using the
.file
method are only processed usingaddFile
. However, files added using the.part
method are processed using bothaddPart
andaddFile
, resulting in duplicate data being included in the servlet creation.Spring Server and Multipart Data Processing
Spring servers can process multipart data in two ways:
@RequestParam("file") MultipartFile file
request.multipartData().getFirst("file")
In particular, when using Functional Endpoints, only the second method is available. An example is shown below:
Spring RestDocs and MockPart Issues
When using
RestDocs
to test, files added using the.part
method result in the following output:As shown in the output, even though only one file was added using the
.part
method, the file is included twice in the request.Review Points
.file
to build aMockMultipartFile
, the request's part data is empty, making it impossible to read the data using Functional Endpoints. Is it appropriate to add part data to the request in tests as well?Part
normally. Therefore, it seems more consistent to fill in the part data for mock objects as well.RestDocs
alone but appears to be a problem stemming from the subtle differences in behavior between various modules (Spring
,spring-test(MockMVC)
,spring-restdocs
, etc.).Proposed Solution
Currently,
MockMvcRequestConverter
(Spring RestDocs) dependency that inspects and processesMockMultipartHttpServletRequest
instances. to process "file" because parts is emply when you use .file() for uploading MultipartIMO, this seems to be the case when using the common Spring method @RequestParam("file") MultipartFile file to upload files.
Solution:
Handle within RestDocs
Since
MockMvcRequestConverter
is already dependent onMockMultipartHttpServletRequest
, it would be more suitable to handle this issue withinRestDocs
rather than modifying other modules and potentially breaking backward compatibility.Remove duplicates
Since
MockMultipartHttpServletRequestBuilder
always adds bothFile
andPart
whenaddPart with filename
is called,When the same file is added to both
File
andPart
, remove the duplicate.Duplicate validation
There may be cases where multiple files with the same name need to be included intentionally.
Therefore, the duplicate removal logic should only apply when
File
andPart
exist as a pair.By implementing this solution, the issue of duplicate file parts being generated in
MockMultipartHttpServletRequest
can be resolved, ensuring thatRestDocs
generates accurate API documentation.The text was updated successfully, but these errors were encountered: