-
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
Modifying Contract to support passing all parameters to encoders #1448 #1459
Changes from all commits
5787482
9f6c6ee
02b26b1
4f2c1ff
ee2abe2
fe87687
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,9 @@ protected MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method me | |
data.returnType( | ||
Types.resolve(targetType, targetType, method.getGenericReturnType())); | ||
data.configKey(Feign.configKey(targetType, method)); | ||
if (AlwaysEncodeBodyContract.class.isAssignableFrom(this.getClass())) { | ||
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. can't you override this method on the subclass? 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. na, I don't think it's possible. 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. This setting is necessary here to avoid having to override this whole method (a copy plus this setting) under |
||
data.alwaysEncodeBody(true); | ||
} | ||
|
||
if (targetType.getInterfaces().length == 1) { | ||
processAnnotationOnClass(data, targetType.getInterfaces()[0]); | ||
|
@@ -133,7 +136,7 @@ protected MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method me | |
if (data.isAlreadyProcessed(i)) { | ||
checkState(data.formParams().isEmpty() || data.bodyIndex() == null, | ||
"Body parameters cannot be used with form parameters.%s", data.warnings()); | ||
} else { | ||
} else if (!data.alwaysEncodeBody()) { | ||
checkState(data.formParams().isEmpty(), | ||
"Body parameters cannot be used with form parameters.%s", data.warnings()); | ||
checkState(data.bodyIndex() == null, | ||
|
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")); | ||
} | ||
|
||
} |
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.
Since the purpose of this class is to allow the body always to be defined by the encoder, there was no need to add
alwaysEncodeBody
field here.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.
Notice this class extends
DeclarativeContract
, instead ofBaseContract
. That is necessary to take advantage of the annotations processors, which is required for extensions ofAlwaysEncodeBodyContract
.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.
makes sense.