OpenFeign / OpenFeign/feign

Encode issue with feign.template.Template#resolveExpression

Open
#2,476 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feedback provided
Dominant language
Java
Stars
9.8k
Forks
1.9k
Avg merge
1d 2h
Merged PRs (30d)
41

Description

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).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with feign.template.Template#resolveExpression and feign.template.Expressions.SimpleExpression#expand, then reproduce the bodyTemplate case where a List variable is expanded. Compare the intended JSON request body with the current encoded output; done means collection values are handled according to the requested body semantics without breaking existing template expansion behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.