Skip to content

MultipartFile argument requires multipart request even when optional (and empty) [SPR-13849] #18422

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Referenced from: commits b4f33ad

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions