OpenFeign / OpenFeign/feign

Unable to send empty JSON object with `@Body`

Open
#2,041 3 comments 2 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

Both approaches don't seem to work using feign 12.3 with okhttp and GSON encoder/decoder:

@Headers({"Accept: application/json", "Content-Type: application/json"})
public interface Api {

    @RequestLine("POST /empty")
    @Body("%7B%7D")
    void postEncoded();

    @RequestLine("POST /empty")
    @Body("{}")
    void postUnencoded();
}

The first sends it verbatim, which can't be parsed by the server, and the second triggers the expansion logic, creating an exception:

java.lang.IllegalArgumentException: an expression is required.
	at feign.template.Expressions.create(Expressions.java:68) ~[feign-core-12.3.jar:?]
	at feign.template.Template.parseFragment(Template.java:218) ~[feign-core-12.3.jar:?]
	at feign.template.Template.parseTemplate(Template.java:202) ~[feign-core-12.3.jar:?]
	at feign.template.Template.<init>(Template.java:61) ~[feign-core-12.3.jar:?]
	at feign.template.BodyTemplate.<init>(BodyTemplate.java:54) ~[feign-core-12.3.jar:?]
	at feign.template.BodyTemplate.create(BodyTemplate.java:50) ~[feign-core-12.3.jar:?]
	at feign.RequestTemplate.bodyTemplate(RequestTemplate.java:909) ~[feign-core-12.3.jar:?]
	at feign.Contract$Default.lambda$new$2(Contract.java:285) ~[feign-core-12.3.jar:?]
	at feign.DeclarativeContract$GuardedAnnotationProcessor.process(DeclarativeContract.java:253) ~[feign-core-12.3.jar:?]
	at feign.DeclarativeContract.lambda$processAnnotationOnMethod$7(DeclarativeContract.java:93) ~[feign-core-12.3.jar:?]
	at java.util.ArrayList.forEach(ArrayList.java:1511) ~[?:?]
	at feign.DeclarativeContract.processAnnotationOnMethod(DeclarativeContract.java:93) ~[feign-core-12.3.jar:?]
	at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:110) ~[feign-core-12.3.jar:?]
	at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:65) ~[feign-core-12.3.jar:?]
	at feign.DeclarativeContract.parseAndValidateMetadata(DeclarativeContract.java:38) ~[feign-core-12.3.jar:?]
	at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:134) ~[feign-core-12.3.jar:?]
	at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:56) ~[feign-core-12.3.jar:?]
	at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:48) ~[feign-core-12.3.jar:?]
	at feign.Feign$Builder.target(Feign.java:196) ~[feign-core-12.3.jar:?]
	at feign.Feign$Builder.target(Feign.java:192) ~[feign-core-12.3.jar:?]

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 at the @Body processing path named in the trace, especially BodyTemplate, Template, Expressions, and RequestTemplate.bodyTemplate. Reproduce the two annotations with Feign 12.3, then add coverage for sending an empty JSON object without triggering expression expansion; done means the request body is {} and no IllegalArgumentException occurs.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.