-
Notifications
You must be signed in to change notification settings - Fork 815
Allow @RequestPart user-defined POJOs #314
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
Merged
OlgaMaciaszek
merged 28 commits into
spring-cloud:2.2.x
from
darrenfoong:request-part-pojo
Apr 14, 2020
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
beba48b
Add Pojo and failing test to FeignClientTests
darrenfoong b629e13
Add SpringPojoFormEncoder
darrenfoong 59df0b6
Fix configuration
darrenfoong f965475
Update Pojo
darrenfoong 93b7f80
Implement multipartPojo
darrenfoong a8b7cb0
Does not work if Pojo has one field: this is crazy
darrenfoong ce3b96f
Disable expansion for multipart/form-data
darrenfoong b66c3ba
Remove Pojo
darrenfoong 9cdfc49
Tidy code
darrenfoong 1a69560
Add testSinglePojoRequestPart
darrenfoong 1a917e2
Update testMultiplePojoRequestPart
darrenfoong 0e3b5fa
Update testMultiplePojoRequestPart
darrenfoong 5312520
Add testRequestPartWithListOfPojosAndListOfMultipartFiles
darrenfoong a7d98fe
Fix Checkstyle errors
darrenfoong c8c1f0b
Update license year and authors
darrenfoong e33e0fd
Tidy isApplicable
darrenfoong d18b7ab
Refactor isApplicable
darrenfoong 974bf01
Rename classes and methods
darrenfoong 75066f9
Change variable names
darrenfoong 9a9112f
Rename PojoFormWriter to AbstractFormWriter
darrenfoong 1b515cb
Rename method
darrenfoong a5e07ba
Use ObjectProvider
darrenfoong 33a279b
Change constructor of SpringEncoder to move SpringPojoFormEncoder to …
darrenfoong 93050f3
Rename variables
darrenfoong dcc120e
Remove unused variable
darrenfoong a1440a4
Extract springEncoder() method
darrenfoong 3b7f9b2
Restore previous constructor
darrenfoong 1799ade
Extract isMultipartFormData() method
darrenfoong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...gn-core/src/main/java/org/springframework/cloud/openfeign/support/AbstractFormWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * 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.io.IOException; | ||
| import java.util.Iterator; | ||
| import java.util.function.Predicate; | ||
|
|
||
| import feign.codec.EncodeException; | ||
| import feign.form.multipart.AbstractWriter; | ||
| import feign.form.multipart.Output; | ||
| import feign.form.util.PojoUtil; | ||
|
|
||
| import org.springframework.http.MediaType; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import static feign.form.ContentProcessor.CRLF; | ||
| import static feign.form.util.PojoUtil.isUserPojo; | ||
|
|
||
| /** | ||
| * @author Darren Foong | ||
| */ | ||
| public abstract class AbstractFormWriter extends AbstractWriter { | ||
|
|
||
| @Override | ||
| public boolean isApplicable(Object object) { | ||
| return !isTypeOrCollection(object, o -> o instanceof MultipartFile) | ||
| && isTypeOrCollection(object, PojoUtil::isUserPojo); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(Output output, String key, Object object) throws EncodeException { | ||
| try { | ||
| String string = new StringBuilder() | ||
| .append("Content-Disposition: form-data; name=\"").append(key) | ||
| .append('"').append(CRLF).append("Content-Type: ") | ||
| .append(getContentType()).append("; charset=") | ||
| .append(output.getCharset().name()).append(CRLF).append(CRLF) | ||
| .append(writeAsString(object)).toString(); | ||
|
|
||
| output.write(string); | ||
| } | ||
| catch (IOException e) { | ||
| throw new EncodeException(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| protected abstract MediaType getContentType(); | ||
|
|
||
| protected abstract String writeAsString(Object object) throws IOException; | ||
|
|
||
| private boolean isTypeOrCollection(Object object, Predicate<Object> isType) { | ||
| if (object.getClass().isArray()) { | ||
| Object[] array = (Object[]) object; | ||
|
|
||
| return array.length > 1 && isType.test(array[0]); | ||
| } | ||
| else if (object instanceof Iterable) { | ||
| Iterable<?> iterable = (Iterable<?>) object; | ||
| Iterator<?> iterator = iterable.iterator(); | ||
|
|
||
| return iterator.hasNext() && isType.test(iterator.next()); | ||
| } | ||
| else { | ||
| return isType.test(object); | ||
| } | ||
| } | ||
|
|
||
| } | ||
46 changes: 46 additions & 0 deletions
46
...nfeign-core/src/main/java/org/springframework/cloud/openfeign/support/JsonFormWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * 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.io.IOException; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * @author Darren Foong | ||
| */ | ||
| @Component | ||
| public class JsonFormWriter extends AbstractFormWriter { | ||
|
|
||
| @Autowired | ||
| private ObjectMapper objectMapper; | ||
|
|
||
| @Override | ||
| protected MediaType getContentType() { | ||
| return MediaType.APPLICATION_JSON; | ||
| } | ||
|
|
||
| @Override | ||
| protected String writeAsString(Object object) throws IOException { | ||
| return objectMapper.writeValueAsString(object); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.