You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
only one MultipartFile object populated when using an java.util.Optional MutipartFile array or list @RequestParam [SPR-15919] #20473
in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug
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";
}
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.
in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug
Activity
spring-projects-issues commentedon 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
ObjectToOptionalConverteraware of array and collection sources, with special emptiness handling built in.