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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -67,12 +69,20 @@ public class SpringEncoder implements Encoder {
public SpringEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
this.springFormEncoder = new SpringFormEncoder();
this.messageConverters = messageConverters;

MultipartFormContentProcessor processor = (MultipartFormContentProcessor) springFormEncoder
.getContentProcessor(MULTIPART);
processor.addFirstWriter(new EmptyCollectionWriter());
}

public SpringEncoder(SpringFormEncoder springFormEncoder,
ObjectFactory<HttpMessageConverters> messageConverters) {
this.springFormEncoder = springFormEncoder;
this.messageConverters = messageConverters;

MultipartFormContentProcessor processor = (MultipartFormContentProcessor) springFormEncoder
.getContentProcessor(MULTIPART);
processor.addFirstWriter(new EmptyCollectionWriter());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<MultipartFile> multipartFiles = Arrays.asList(
Expand All @@ -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
Expand Down Expand Up @@ -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<MultipartFile> files);

@RequestMapping(method = RequestMethod.POST, path = "/multipartNames",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.TEXT_PLAIN_VALUE)
Expand Down Expand Up @@ -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<MultipartFile> files) {
return Integer.toString(files.size());
}

@RequestMapping(method = RequestMethod.POST, path = "/multipartNames",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.TEXT_PLAIN_VALUE)
Expand Down