-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Flux type response should be corresponding to List #2199
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a7d8b92
Refactoring: Move resolveLastTypeParameter from Util to Types
gromspys ac5af83
Add ReactiveDecoder
gromspys bffdd7e
Update code as per suggestions
gromspys 321cdc3
Update code as per suggestions
gromspys 5535986
Merge branch 'master' into feature/add-reactive-decoder
gromspys 71e8125
Refactoring
gromspys dd2b4ae
Add tests
gromspys 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 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 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 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 |
---|---|---|
|
@@ -14,24 +14,34 @@ implementation to your classpath. Then configure Feign to use the reactive stre | |
public interface GitHubReactor { | ||
|
||
@RequestLine("GET /repos/{owner}/{repo}/contributors") | ||
Flux<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo); | ||
Flux<Contributor> contributorsFlux(@Param("owner") String owner, @Param("repo") String repo); | ||
|
||
@RequestLine("GET /repos/{owner}/{repo}/contributors") | ||
Mono<List<Contributor>> contributorsMono(@Param("owner") String owner, @Param("repo") String repo); | ||
|
||
class Contributor { | ||
String login; | ||
|
||
public Contributor(String login) { | ||
this.login = login; | ||
} | ||
String login; | ||
|
||
public String getLogin() { | ||
return login; | ||
} | ||
|
||
public void setLogin(String login) { | ||
this.login = login; | ||
} | ||
} | ||
} | ||
|
||
public class ExampleReactor { | ||
public static void main(String args[]) { | ||
GitHubReactor gitHub = ReactorFeign.builder() | ||
GitHubReactor gitHub = ReactorFeign.builder() | ||
.decoder(new ReactiveDecoder(new JacksonDecoder())) | ||
.target(GitHubReactor.class, "https://api.github.com"); | ||
|
||
List<Contributor> contributors = gitHub.contributors("OpenFeign", "feign") | ||
.collect(Collectors.toList()) | ||
List<GitHubReactor.Contributor> contributorsFromFlux = gitHub.contributorsFlux("OpenFeign", "feign") | ||
.collectList() | ||
.block(); | ||
List<GitHubReactor.Contributor> contributorsFromMono = gitHub.contributorsMono("OpenFeign", "feign") | ||
.block(); | ||
} | ||
} | ||
|
@@ -79,33 +89,5 @@ the wrapped in the appropriate reactive wrappers. | |
### Iterable and Collections responses | ||
|
||
Due to the Synchronous nature of Feign requests, methods that return `Iterable` types must specify the collection | ||
in the `Publisher`. For `Reactor` types, this limits the use of `Flux` as a response type. If you | ||
want to use `Flux`, you will need to manually convert the `Mono` or `Iterable` response types into | ||
`Flux` using the `fromIterable` method. | ||
|
||
in the `Publisher`. For `Reactor` types, this limits the use of `Flux` as a response type. | ||
Comment on lines
90
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The removal of the example code that demonstrates how to manually convert |
||
|
||
```java | ||
public interface GitHub { | ||
|
||
@RequestLine("GET /repos/{owner}/{repo}/contributors") | ||
Mono<List<Contributor>> contributors(@Param("owner") String owner, @Param("repo") String repo); | ||
|
||
class Contributor { | ||
String login; | ||
|
||
public Contributor(String login) { | ||
this.login = login; | ||
} | ||
} | ||
} | ||
|
||
public class ExampleApplication { | ||
public static void main(String[] args) { | ||
GitHub gitHub = ReactorFeign.builder() | ||
.target(GitHub.class, "https://api.github.com"); | ||
|
||
Mono<List<Contributor>> contributors = gitHub.contributors("OpenFeign", "feign"); | ||
Flux<Contributor> contributorFlux = Flux.fromIterable(contributors.block()); | ||
} | ||
} | ||
``` |
50 changes: 50 additions & 0 deletions
50
reactive/src/main/java/feign/reactive/ReactiveDecoder.java
This file contains 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,50 @@ | ||
/* | ||
* Copyright 2012-2023 The Feign 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 | ||
* | ||
* http://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 feign.reactive; | ||
|
||
import feign.FeignException; | ||
import feign.Response; | ||
import feign.Types; | ||
import feign.codec.Decoder; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
|
||
public class ReactiveDecoder implements Decoder { | ||
|
||
private final Decoder delegate; | ||
|
||
public ReactiveDecoder(Decoder decoder) { | ||
this.delegate = decoder; | ||
} | ||
|
||
@Override | ||
public Object decode(Response response, Type type) throws IOException, FeignException { | ||
Class<?> rawType = Types.getRawType(type); | ||
if (rawType.isAssignableFrom(Mono.class)) { | ||
Type lastType = Types.resolveLastTypeParameter(type, Mono.class); | ||
return Mono.just(delegate.decode(response, lastType)); | ||
} | ||
if (rawType.isAssignableFrom(Flux.class)) { | ||
Type lastType = Types.resolveLastTypeParameter(type, Flux.class); | ||
Type listType = Types.parameterize(List.class, lastType); | ||
return Flux.fromIterable((Iterable)delegate.decode(response, listType)); | ||
} | ||
|
||
return delegate.decode(response, type); | ||
} | ||
} | ||
gromspys marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
contributorsFlux
andcontributorsMono
methods have the same@RequestLine
annotation. This could lead to confusion and potential errors. Consider renaming these methods to better reflect their return types or the data they are fetching.