diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/EmptyCollectionWriter.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/EmptyCollectionWriter.java new file mode 100644 index 000000000..5c44a993c --- /dev/null +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/EmptyCollectionWriter.java @@ -0,0 +1,40 @@ +/* + * Copyright 2013-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.support; + +import java.util.Collection; + +import feign.codec.EncodeException; +import feign.form.multipart.Output; +import feign.form.multipart.Writer; + +/** + * @author Darren Foong + */ +public class EmptyCollectionWriter implements Writer { + + @Override + public boolean isApplicable(Object value) { + return value instanceof Collection && ((Collection) value).isEmpty(); + } + + @Override + public void write(Output output, String boundary, String key, Object value) + throws EncodeException { + } + +} diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringEncoder.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringEncoder.java index ed99a2b9b..8766ba7b3 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringEncoder.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringEncoder.java @@ -29,6 +29,7 @@ import feign.RequestTemplate; import feign.codec.EncodeException; import feign.codec.Encoder; +import feign.form.MultipartFormContentProcessor; import feign.form.spring.SpringFormEncoder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -46,6 +47,7 @@ import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter; import org.springframework.web.multipart.MultipartFile; +import static feign.form.ContentType.MULTIPART; import static org.springframework.cloud.openfeign.support.FeignUtils.getHeaders; import static org.springframework.cloud.openfeign.support.FeignUtils.getHttpHeaders; @@ -67,12 +69,20 @@ public class SpringEncoder implements Encoder { public SpringEncoder(ObjectFactory messageConverters) { this.springFormEncoder = new SpringFormEncoder(); this.messageConverters = messageConverters; + + MultipartFormContentProcessor processor = (MultipartFormContentProcessor) springFormEncoder + .getContentProcessor(MULTIPART); + processor.addFirstWriter(new EmptyCollectionWriter()); } public SpringEncoder(SpringFormEncoder springFormEncoder, ObjectFactory messageConverters) { this.springFormEncoder = springFormEncoder; this.messageConverters = messageConverters; + + MultipartFormContentProcessor processor = (MultipartFormContentProcessor) springFormEncoder + .getContentProcessor(MULTIPART); + processor.addFirstWriter(new EmptyCollectionWriter()); } @Override diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/valid/ValidFeignClientTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/valid/ValidFeignClientTests.java index 0cf667d83..e4efa90e6 100644 --- a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/valid/ValidFeignClientTests.java +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/valid/ValidFeignClientTests.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -336,6 +337,19 @@ public void testMultiplePojoRequestParts() { assertThat(response).isEqualTo("abc123hello world 1oi terra 2hello.bin"); } + @Test + public void testRequestPartWithEmptyListOfMultipartFiles() { + String partNames = this.multipartClient + .requestPartListOfMultipartFilesReturnsPartNames(Collections.emptyList()); + assertThat(partNames).isNull(); + String fileNames = this.multipartClient + .requestPartListOfMultipartFilesReturnsFileNames(Collections.emptyList()); + assertThat(fileNames).isNull(); + String count = this.multipartClient + .requestPartListOfMultipartFilesReturnsCount(Collections.emptyList()); + assertThat(count).isEqualTo("0"); + } + @Test public void testRequestPartWithListOfMultipartFiles() { List multipartFiles = Arrays.asList( @@ -347,6 +361,19 @@ public void testRequestPartWithListOfMultipartFiles() { String fileNames = this.multipartClient .requestPartListOfMultipartFilesReturnsFileNames(multipartFiles); assertThat(fileNames).contains("hello1.bin", "hello2.bin"); + String count = this.multipartClient + .requestPartListOfMultipartFilesReturnsCount(multipartFiles); + assertThat(count).isEqualTo(Integer.toString(multipartFiles.size())); + } + + @Test + public void testRequestPartWithListOfPojosAndEmptyListOfMultipartFiles() { + Hello pojo1 = new Hello(HELLO_WORLD_1); + Hello pojo2 = new Hello(OI_TERRA_2); + String response = this.multipartClient + .requestPartListOfPojosAndListOfMultipartFiles( + Arrays.asList(pojo1, pojo2), Collections.emptyList()); + assertThat(response).isEqualTo("hello world 1oi terra 2"); } @Test @@ -444,6 +471,12 @@ String multipartPojo(@RequestPart("hello") String hello, @RequestPart("pojo2") Hello pojo2, @RequestPart("file") MultipartFile file); + @RequestMapping(method = RequestMethod.POST, path = "/multipartCount", + consumes = MediaType.MULTIPART_FORM_DATA_VALUE, + produces = MediaType.TEXT_PLAIN_VALUE) + String requestPartListOfMultipartFilesReturnsCount( + @RequestPart("files") List files); + @RequestMapping(method = RequestMethod.POST, path = "/multipartNames", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.TEXT_PLAIN_VALUE) @@ -798,6 +831,14 @@ String multipartPojo(@RequestPart("hello") String hello, + file.getOriginalFilename(); } + @RequestMapping(method = RequestMethod.POST, path = "/multipartCount", + consumes = MediaType.MULTIPART_FORM_DATA_VALUE, + produces = MediaType.TEXT_PLAIN_VALUE) + String requestPartListOfMultipartFilesReturnsCount( + @RequestPart("files") List files) { + return Integer.toString(files.size()); + } + @RequestMapping(method = RequestMethod.POST, path = "/multipartNames", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)