Skip to content

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

Closed
darrenfoong wants to merge 5 commits intospring-cloud:mainfrom
darrenfoong:request-part-empty-list
Closed

Allow empty list for @RequestPart List<MultipartFile>#352
darrenfoong wants to merge 5 commits intospring-cloud:mainfrom
darrenfoong:request-part-empty-list

Conversation

@darrenfoong
Copy link
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown
Contributor Author

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

@OlgaMaciaszek
Copy link
Copy Markdown
Collaborator

Closing as external issue.

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 0% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 4.80%. Comparing base (bba3f2e) to head (e4067d2).
⚠️ Report is 1120 commits behind head on main.

Files with missing lines Patch % Lines
...amework/cloud/openfeign/support/SpringEncoder.java 0.00% 6 Missing ⚠️
...cloud/openfeign/support/EmptyCollectionWriter.java 0.00% 3 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##              main    #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              
Files with missing lines Coverage Δ
...cloud/openfeign/support/EmptyCollectionWriter.java 0.00% <0.00%> (ø)
...amework/cloud/openfeign/support/SpringEncoder.java 0.00% <0.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

4 participants