Skip to content

Encode issue with feign.template.Template#resolveExpression #2476

@effiu

Description

@effiu

I customized an AnnotatedParameterProcessor implementation class.
But I encountered a problem: when I use the following code to put the parameter into the request body.

feign.RequestTemplate#resolve(java.util.Map<java.lang.String,?>) {
if (this.bodyTemplate != null) {
      resolved.body(this.bodyTemplate.expand(variables));
    }
}

Such as the bodyTemplate‘s value is: %7B"ab":{ab}%7D, and the variable(ab) is a Array or a List.

feign.template.Template#resolveExpression
feign.template.Expressions.SimpleExpression#expand

String expand(Object variable, boolean encode) {
  StringBuilder expanded = new StringBuilder();
  if (Iterable.class.isAssignableFrom(variable.getClass())) {
    expanded.append(this.expandIterable((Iterable<?>) variable));
  } else {
    expanded.append((encode) ? encode(variable) : variable);
  }

  /* return the string value of the variable */
  String result = expanded.toString();
  if (!this.matches(result)) {
    throw new IllegalArgumentException("Value " + expanded
        + " does not match the expression pattern: " + this.getPattern());
  }
  return result;
}

When the variable is of type Iterable, it will be handle and encode. But I don't want to handle my variable.
How should i control it.

I customized an AnnotatedParameterProcessor to encapsulate the parameters on the feignclient method(POST) into a format like {"a":{a},"b":{b},"c":{c}}, and then put it into the bodyTemplate. Then, in feign.RequestTemplate#resolve(java.util.Map<java.lang.String,?>) replace {a} in the body with the real value, and put this string into the request body (JSON format). However, if {c} is a List, it will be encoded.
{"a":1,"b":test,"c":[],"d":{}},but variable c will be processed into other formats and encoded (like in URLs).

Metadata

Metadata

Assignees

No one assigned

    Labels

    feedback providedFeedback has been provided to the author

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions