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
Empty List instead of null in List<MultipartFile> files [SPR-16329] #20876
It seems that it's not possible to send null as List<MultipartFile> or MultipartFile[] in @RequestPart(value = "attachments", required = false) List<MultipartFile> files controller's method param. I tried PUT and POST.
Is there a way to somehow workaround this issue? I tried to plug custom HandlerMethodArgumentResolver based on AbstractNamedValueMethodArgumentResolver with using WebMvcConfigurerAdapter but as it concerns annotations (@RequestPart in the above example) it doesn't even being triggered.
It works as long as Pageable and Spring Data resolvers are not really needed. Then it throws: org.springframework.beans.BeanInstantiationException: Failed to instantiate org.springframework.data.domain.Pageable]: Specified class is an interface. I give up...
It turns out that MultipartFile and also Servlet 3.0 Part collections are never non-existent but rather just empty (aligned with their corresponding accessors on (Multipart)HttpServletRequest), in contrast to plain request parameters where a parameter may be sent with one or more values or also no value at all (as per ServletRequest.getParameterValues).
As far as I can tell, this always worked like this and therefore isn't a regression either. I'm afraid this has to be seen as "works as designed": You'll have to check for an empty collection there.
No value at all will be also changed into emptyList.
"works as designed"? Hm. Quite strange - preventing sending null for MultipartFile collection. Moreover it is not so easy to change that behaviour - I had to remove annotation and introduce own MultipartFile wrapper to omit default resolvers.
I should elaborate a bit: With multipart requests, there is a distinction between "no multipart request" and "no parts for the given name in a multipart request". The latter will resolve to an empty list (like in the corresponding Servlet APIs) whereas the former will resolve to null for a required=false parameter. This may arguably be a bit of a historic decision but it is what it is at this point.
Why do you absolutely need null for the List<MultipartFile> there? Couldn't you simply check for an empty list, even call downstream APIs with null if you prefer? Or declare the parameter as Optional and evaluate it through that little wrapper?
Thanks for the explanation but the thing is even if you have required=false parameter and you will not send anything - you will also get an emptyList for the type List<MultipartFile> or MultipartFile[] whatever it is @RequestParam or @RequestPart or even without any annotation. Unsinkable emptyList...
I use it for simple patern: emptyList - no changes to the collection, not empty - changes required, null - clear the collection.
I also didn't what to have Optional as a method paramether. But still it would be not very helpfull here.
Activity
spring-projects-issues commentedon Dec 29, 2017
Maciej Zerkowski commented
Is there a way to somehow workaround this issue? I tried to plug custom
HandlerMethodArgumentResolverbased onAbstractNamedValueMethodArgumentResolverwith usingWebMvcConfigurerAdapterbut as it concerns annotations (@RequestPartin the above example) it doesn't even being triggered.spring-projects-issues commentedon Jan 2, 2018
Maciej Zerkowski commented
Ok I wrote my own
RequestMappingHandlerAdapteroverritingMultipartResolutionDelegateand it works fine.spring-projects-issues commentedon Jan 2, 2018
Maciej Zerkowski commented
It works as long as
Pageableand Spring Data resolvers are not really needed. Then it throws:org.springframework.beans.BeanInstantiationException: Failed to instantiate org.springframework.data.domain.Pageable]: Specified class is an interface. I give up...spring-projects-issues commentedon Jan 11, 2018
Juergen Hoeller commented
It turns out that
MultipartFileand also Servlet 3.0Partcollections are never non-existent but rather just empty (aligned with their corresponding accessors on(Multipart)HttpServletRequest), in contrast to plain request parameters where a parameter may be sent with one or more values or also no value at all (as perServletRequest.getParameterValues).As far as I can tell, this always worked like this and therefore isn't a regression either. I'm afraid this has to be seen as "works as designed": You'll have to check for an empty collection there.
spring-projects-issues commentedon Jan 11, 2018
Maciej Zerkowski commented
No value at all will be also changed into emptyList.
"works as designed"? Hm. Quite strange - preventing sending null for
MultipartFilecollection. Moreover it is not so easy to change that behaviour - I had to remove annotation and introduce ownMultipartFilewrapper to omit default resolvers.spring-projects-issues commentedon Jan 11, 2018
Juergen Hoeller commented
I should elaborate a bit: With multipart requests, there is a distinction between "no multipart request" and "no parts for the given name in a multipart request". The latter will resolve to an empty list (like in the corresponding Servlet APIs) whereas the former will resolve to
nullfor arequired=falseparameter. This may arguably be a bit of a historic decision but it is what it is at this point.Why do you absolutely need
nullfor theList<MultipartFile>there? Couldn't you simply check for an empty list, even call downstream APIs withnullif you prefer? Or declare the parameter asOptionaland evaluate it through that little wrapper?spring-projects-issues commentedon Jan 11, 2018
Maciej Zerkowski commented
Thanks for the explanation but the thing is even if you have
required=falseparameter and you will not send anything - you will also get an emptyList for the typeList<MultipartFile>orMultipartFile[]whatever it is@RequestParamor@RequestPartor even without any annotation. Unsinkable emptyList...I use it for simple patern: emptyList - no changes to the collection, not empty - changes required, null - clear the collection.
I also didn't what to have Optional as a method paramether. But still it would be not very helpfull here.
spring-projects-issues commentedon Jan 11, 2018
Maciej Zerkowski commented
The method signature I'm refering to:
(@RequestPart Command command, <@RequestParam(required = false)|@RequestPart(required = false)> MultipartFile[]|List<MultipartFile> attachments)