Skip to content

Commit

Permalink
Modifying Contract to support passing all parameters to encoders
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocarvalho777 committed Jul 8, 2021
1 parent 5c460ae commit 36dd5bc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
13 changes: 12 additions & 1 deletion core/src/main/java/feign/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public interface Contract {

abstract class BaseContract implements Contract {

private boolean allParametersBody;

protected boolean allParametersBody() {
return allParametersBody;
}

protected void allParametersBody(boolean allParametersBody) {
this.allParametersBody = allParametersBody;
}

/**
* @param targetType {@link feign.Target#type() type} of the Feign interface.
* @see #parseAndValidateMetadata(Class)
Expand Down Expand Up @@ -84,6 +94,7 @@ protected MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method me
data.method(method);
data.returnType(Types.resolve(targetType, targetType, method.getGenericReturnType()));
data.configKey(Feign.configKey(targetType, method));
data.allParametersBody(this.allParametersBody);

if (targetType.getInterfaces().length == 1) {
processAnnotationOnClass(data, targetType.getInterfaces()[0]);
Expand Down Expand Up @@ -121,7 +132,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.allParametersBody()) {
checkState(data.formParams().isEmpty(),
"Body parameters cannot be used with form parameters.%s", data.warnings());
checkState(data.bodyIndex() == null,
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/feign/DeclarativeContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final List<MethodMetadata> parseAndValidateMetadata(Class<?> targetType)
* (unless they are the same).
*
* @param data metadata collected so far relating to the current java method.
* @param clz the class to process
* @param targetType the class to process
*/
@Override
protected final void processAnnotationOnClass(MethodMetadata data, Class<?> targetType) {
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/feign/MethodMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class MethodMetadata implements Serializable {
private Integer headerMapIndex;
private Integer queryMapIndex;
private boolean queryMapEncoded;
private boolean allParametersBody;
private transient Type bodyType;
private final RequestTemplate template = new RequestTemplate();
private final List<String> formParams = new ArrayList<String>();
Expand Down Expand Up @@ -118,6 +119,15 @@ public MethodMetadata queryMapEncoded(boolean queryMapEncoded) {
return this;
}

public boolean allParametersBody() {
return allParametersBody;
}

MethodMetadata allParametersBody(boolean allParametersBody) {
this.allParametersBody = allParametersBody;
return this;
}

/**
* Type corresponding to {@link #bodyIndex()}.
*/
Expand Down
19 changes: 15 additions & 4 deletions core/src/main/java/feign/ReflectiveFeign.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public Map<String, MethodHandler> apply(Target target) {
if (!md.formParams().isEmpty() && md.template().bodyTemplate() == null) {
buildTemplate =
new BuildFormEncodedTemplateFromArgs(md, encoder, queryMapEncoder, target);
} else if (md.bodyIndex() != null) {
} else if (md.bodyIndex() != null || md.allParametersBody()) {
buildTemplate = new BuildEncodedTemplateFromArgs(md, encoder, queryMapEncoder, target);
} else {
buildTemplate = new BuildTemplateByResolvingArgs(md, queryMapEncoder, target);
Expand Down Expand Up @@ -379,10 +379,21 @@ private BuildEncodedTemplateFromArgs(MethodMetadata metadata, Encoder encoder,
protected RequestTemplate resolve(Object[] argv,
RequestTemplate mutable,
Map<String, Object> variables) {
Object body = argv[metadata.bodyIndex()];
checkArgument(body != null, "Body parameter %s was null", metadata.bodyIndex());

boolean allParametersBody = mutable.methodMetadata().allParametersBody();

Object body = null;
if (!allParametersBody) {
body = argv[metadata.bodyIndex()];
checkArgument(body != null, "Body parameter %s was null", metadata.bodyIndex());
}

try {
encoder.encode(body, metadata.bodyType(), mutable);
if (allParametersBody) {
encoder.encode(argv, Object[].class, mutable);
} else {
encoder.encode(body, metadata.bodyType(), mutable);
}
} catch (EncodeException e) {
throw e;
} catch (RuntimeException e) {
Expand Down

0 comments on commit 36dd5bc

Please sign in to comment.