-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modifying Contract to support passing all parameters to encoders #1448 (
#1459) * Modifying Contract to support passing all parameters to encoders * Formatting license * Adding unit tests * Adding AlwaysEncodeBodyContract abstract class (#1)
- Loading branch information
1 parent
df95702
commit 072dcf9
Showing
6 changed files
with
183 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright 2012-2021 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; | ||
|
||
/** | ||
* {@link DeclarativeContract} extension that allows user provided custom encoders to define the | ||
* request message payload using only the request template and the method parameters, not requiring | ||
* a specific and unique body object. | ||
* | ||
* This type of contract is useful when an application needs a Feign client whose request payload is | ||
* defined entirely by a custom Feign encoder regardless of how many parameters are declared at the | ||
* client method. In this case, even with no presence of body parameter the provided encoder will | ||
* have to know how to define the request payload (for example, based on the method name, method | ||
* return type, and other metadata provided by custom annotations, all available via the provided | ||
* {@link RequestTemplate} object). | ||
* | ||
* @author fabiocarvalho777@gmail.com | ||
*/ | ||
public abstract class AlwaysEncodeBodyContract extends DeclarativeContract { | ||
} |
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
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
117 changes: 117 additions & 0 deletions
117
core/src/test/java/feign/AlwaysEncodeBodyContractTest.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,117 @@ | ||
/** | ||
* Copyright 2012-2021 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; | ||
|
||
import feign.codec.EncodeException; | ||
import feign.codec.Encoder; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import java.io.IOException; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.lang.reflect.Type; | ||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
public class AlwaysEncodeBodyContractTest { | ||
|
||
@Retention(RUNTIME) | ||
@Target(ElementType.METHOD) | ||
private @interface SampleMethodAnnotation { | ||
} | ||
|
||
private static class SampleContract extends AlwaysEncodeBodyContract { | ||
SampleContract() { | ||
AnnotationProcessor<SampleMethodAnnotation> annotationProcessor = | ||
(annotation, metadata) -> metadata.template().method(Request.HttpMethod.POST); | ||
super.registerMethodAnnotation(SampleMethodAnnotation.class, annotationProcessor); | ||
} | ||
} | ||
|
||
private interface SampleTargetMultipleNonAnnotatedParameters { | ||
@SampleMethodAnnotation | ||
String concatenate(String word1, String word2, String word3); | ||
} | ||
|
||
private interface SampleTargetNoParameters { | ||
@SampleMethodAnnotation | ||
String concatenate(); | ||
} | ||
|
||
private interface SampleTargetOneParameter { | ||
@SampleMethodAnnotation | ||
String concatenate(String word1); | ||
} | ||
|
||
private static class AllParametersSampleEncoder implements Encoder { | ||
@Override | ||
public void encode(Object object, Type bodyType, RequestTemplate template) | ||
throws EncodeException { | ||
Object[] methodParameters = (Object[]) object; | ||
String body = | ||
Arrays.stream(methodParameters).map(String::valueOf).collect(Collectors.joining()); | ||
template.body(body); | ||
} | ||
} | ||
|
||
private static class BodyParameterSampleEncoder implements Encoder { | ||
@Override | ||
public void encode(Object object, Type bodyType, RequestTemplate template) | ||
throws EncodeException { | ||
template.body(String.valueOf(object)); | ||
} | ||
} | ||
|
||
private static class SampleClient implements Client { | ||
@Override | ||
public Response execute(Request request, Request.Options options) throws IOException { | ||
return Response.builder() | ||
.status(200) | ||
.request(request) | ||
.body(request.body()) | ||
.build(); | ||
} | ||
} | ||
|
||
/** | ||
* This test makes sure Feign calls the client provided encoder regardless of how many | ||
* non-annotated parameters the client method has, as alwaysEncodeBody is set to true. | ||
*/ | ||
@Test | ||
public void alwaysEncodeBodyTrueTest() { | ||
SampleTargetMultipleNonAnnotatedParameters sampleClient1 = Feign.builder() | ||
.contract(new SampleContract()) | ||
.encoder(new AllParametersSampleEncoder()) | ||
.client(new SampleClient()) | ||
.target(SampleTargetMultipleNonAnnotatedParameters.class, "http://localhost"); | ||
Assert.assertEquals("foobarchar", sampleClient1.concatenate("foo", "bar", "char")); | ||
|
||
SampleTargetNoParameters sampleClient2 = Feign.builder() | ||
.contract(new SampleContract()) | ||
.encoder(new AllParametersSampleEncoder()) | ||
.client(new SampleClient()) | ||
.target(SampleTargetNoParameters.class, "http://localhost"); | ||
Assert.assertEquals("", sampleClient2.concatenate()); | ||
|
||
SampleTargetOneParameter sampleClient3 = Feign.builder() | ||
.contract(new SampleContract()) | ||
.encoder(new AllParametersSampleEncoder()) | ||
.client(new SampleClient()) | ||
.target(SampleTargetOneParameter.class, "http://localhost"); | ||
Assert.assertEquals("moo", sampleClient3.concatenate("moo")); | ||
} | ||
|
||
} |