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-issuemaster opened this issue Aug 31, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

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:

  • #20235 Request params Optional<List and List are inconsistent
  • #20472 java.util.Optional MultipartFile[] @RequestParam argument is null in multipart/form-data POST

Referenced from: commits 53a9697, 15c82af

Backported to: 4.3.12

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

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
You can’t perform that action at this time.