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

Fix bug in Capability invoke #2239

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/feign/Capability.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static Object enrich(Object componentToEnrich,
static Object invoke(Object target, Capability capability, Class<?> capabilityToEnrich) {
return Arrays.stream(capability.getClass().getMethods())
.filter(method -> method.getName().equals("enrich"))
.filter(method -> method.getReturnType().isAssignableFrom(capabilityToEnrich))
.filter(method -> method.getReturnType() == capabilityToEnrich)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will work for Feign, all enrichers are based on interfaces....

I even wonder how this didn't broke any unit test.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will work for Feign, all enrichers are based on interfaces....

I even wonder how this didn't broke any unit test.

I think you're right, if you look at the feign application as a whole, this rarely happens, since I'm thinking about the logic of this place in terms of this method, thank you, I'll close this pull request,

.findFirst()
.map(method -> {
try {
Expand Down
31 changes: 31 additions & 0 deletions core/src/test/java/feign/CapabilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.util.Arrays;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;

public class CapabilityTest {
Expand Down Expand Up @@ -72,4 +73,34 @@ public Client enrich(Client client) {
assertThat(enriched, CoreMatchers.instanceOf(BClient.class));
}

private static class FamilyCapability implements Capability {
public Father enrich(Father father) {
father.balance = 10;
return father;
}
public Son enrich(Son son) {
son.balance = 5;
return son;
}
}

private static class Father {
int balance;
}

private static class Son extends Father {}

@Test
public void enrichTest() {
Son son = new Son();
Father father = new Father();
// enrich Son
Capability.invoke(son, new FamilyCapability(), Son.class);
Assert.assertEquals(son.balance, 5);
// enrich Father
Capability.invoke(father, new FamilyCapability(), Father.class);
Assert.assertEquals(father.balance, 10);
}


}