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

Allow empty list for @RequestPart List<MultipartFile> #352

Closed

Conversation

darrenfoong
Copy link
Contributor

I have a use case where I need to send a list of MultipartFiles, but sometimes this list could be empty. Currently, to successfully send an empty list (i.e. the server receives an empty list as a method argument), I need to send null instead, which requires me to do a prior isEmpty() check.

(It is strange that sending null from the client results in an empty list received by the server, but this is documented here.)

Sending an empty list now throws an exception:

feign.codec.EncodeException: class java.util.Collections$EmptyList is not a type supported by this encoder.
	at feign.codec.Encoder$Default.encode(Encoder.java:94)
	at feign.form.multipart.DelegateWriter.write(DelegateWriter.java:49)
	at feign.form.multipart.AbstractWriter.write(AbstractWriter.java:36)
	at feign.form.MultipartFormContentProcessor.process(MultipartFormContentProcessor.java:87)
	at feign.form.FormEncoder.encode(FormEncoder.java:105)
	at feign.form.spring.SpringFormEncoder.encode(SpringFormEncoder.java:84)
...

This PR adds an EmptyCollectionWriter to MultipartFormContentProcessor in the SpringFormEncoder. This Writer is a no-op.

It would be ideal to add the EmptyCollectionWriter in the feign-form project instead, but that project is relatively inactive.

I also added a new test client endpoint that counts the number of files sent, to make sure that empty lists are not received as null, in case Spring Framework changes behaviour in the future.

@codecov
Copy link

codecov bot commented Jun 12, 2020

Codecov Report

Merging #352 into master will decrease coverage by 0.03%.
The diff coverage is 0.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##             master    #352      +/-   ##
===========================================
- Coverage      4.83%   4.80%   -0.04%     
  Complexity       13      13              
===========================================
  Files            45      46       +1     
  Lines          1385    1394       +9     
  Branches        205     205              
===========================================
  Hits             67      67              
- Misses         1316    1325       +9     
  Partials          2       2              
Impacted Files Coverage Δ Complexity Δ
...cloud/openfeign/support/EmptyCollectionWriter.java 0.00% <0.00%> (ø) 0.00 <0.00> (?)
...amework/cloud/openfeign/support/SpringEncoder.java 0.00% <0.00%> (ø) 0.00 <0.00> (ø)

@OlgaMaciaszek
Copy link
Collaborator

Hi, @darrenfoong - thanks for the PR. Could you first check with the maintainers of feign-form if it's possible to add the EmptyCollectionWriter there instead?

@darrenfoong
Copy link
Contributor Author

I've just created a PR for feign-form: OpenFeign/feign-form#99. I hope the maintainers become active...

@OlgaMaciaszek
Copy link
Collaborator

Closing as external issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants