Skip to content
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

only one MultipartFile object populated when using an java.util.Optional MutipartFile array or list @RequestParam [SPR-15919] #20473

Closed
spring-projects-issues opened this issue Aug 31, 2017 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Aug 31, 2017

Brice Roncace opened SPR-15919 and commented

Having the multipart/form-data form:

<form action="<c:url value="/optionalPost"/>" method="post" enctype="multipart/form-data">
  <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
  <input type="file" name="files" multiple/>
  <input type="submit"/>
</form>

When selecting multiple files for the file input, only one file is actually received by the controller when handling this request:

 // Does NOT work for multiple files (only one comes in)
@PostMapping("/optionalPost")
public String postTest2(@RequestParam Optional<List<MultipartFile>> files) {
  if (files != null && files.isPresent()) {
    System.out.println(files.get().size());
    files.get().forEach(file -> System.out.println(file.getOriginalFilename()));
  }
  return "index";
}

This fails in the same way:

// Does NOT work for multiple files (only one comes in)
@PostMapping("/optionalPost")
public String postTest3(@RequestParam Optional<MultipartFile[]> files) {
  if (files != null && files.isPresent()) {
    System.out.println(files.get().length);
    Stream.of(files.get()).forEach(file -> System.out.println(file.getOriginalFilename()));
  }
  return "index";
}

The workaround is to avoid the use of Optional:

// Works for multiple files
@PostMapping("/optionalPost")
public String postTest4(@RequestParam MultipartFile[] files) {
  System.out.println(files.length);
  Stream.of(files).forEach(file -> System.out.println(file.getOriginalFilename()));
  return "index";
}
// Works for multiple files
@PostMapping("/optionalPost")
public String postTest5(@RequestParam List<MultipartFile> files) {
  System.out.println(files.size());
  files.forEach(file -> System.out.println(file.getOriginalFilename()));
  return "index";
}

Affects: 4.3.10

Issue Links:

Referenced from: commits 53a9697, 15c82af

Backported to: 4.3.12

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Sep 20, 2017

Juergen Hoeller commented

Like #20472, this turns out to be a general limitation in the conversion infrastructure. The eventual fix for both issues was to make ObjectToOptionalConverter aware of array and collection sources, with special emptiness handling built in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants