Description
Martin Sivák opened SPR-13849 and commented
The method argument resolver fails with org.springframework.web.multipart.MultipartException: The current request is not a multipart request when an optional MultipartFile argument is present in the request handler method during a plain POST request with no files included.
I would expect that multipart request is enforced when files are present, but not necessary when there are no files and the file argument is marked as optional.
{{
@RequestMapping
(method = RequestMethod.POST)
@ResponseStatus
(HttpStatus.OK)
public ResponseEntity<EmberModel> upload(@CurrentUser
User currentUser,
@RequestParam
(value = "file", required = false) MultipartFile file,
@RequestParam
("entity") @Valid
DocumentApiModel entity) throws IOException, BaseRestException {
}}
It seems that this can be "trivially" fixed in org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName(RequestParamMethodArgumentResolver.java:167) by checking the optional attribute and the provided value.
Affects: 4.2.1
Issue Links:
- Spring's support for javax.servlet.http.Part vs. MultipartFile incomplete, inconsistent [SPR-10591] #15220 Spring's support for javax.servlet.http.Part vs. MultipartFile incomplete, inconsistent
- Support for java.util.Optional as a @RequestPart [SPR-12644] #17245 Support for java.util.Optional as a
@RequestPart
- Request parameters cannot bind to Optional<List<String>> [SPR-13418] #17997 Request parameters cannot bind to Optional<List>
- Part list/array gets resolved to all parts in current request [SPR-13893] #18467 Part list/array gets resolved to all parts in current request
- RequestPartMethodArgumentResolver should defensively handle MethodParameter nesting level and java.util.Optional access [SPR-13850] #18423 RequestPartMethodArgumentResolver should defensively handle MethodParameter nesting level and java.util.Optional access
- Allow binding all MultipartFile instances as handler method argument [SPR-17405] #21938 Allow binding all MultipartFile instances as handler method argument
Referenced from: commits b4f33ad