spring-cloud-openfeign icon indicating copy to clipboard operation
spring-cloud-openfeign copied to clipboard

Allow empty list for @RequestPart List<MultipartFile>

Open darrenfoong opened this issue 5 years ago • 3 comments
trafficstars

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.

darrenfoong avatar Jun 12 '20 10:06 darrenfoong

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> (ø)

codecov[bot] avatar Jun 12 '20 10:06 codecov[bot]

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?

OlgaMaciaszek avatar Sep 17 '20 15:09 OlgaMaciaszek

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

darrenfoong avatar Dec 12 '20 10:12 darrenfoong