Skip to content

Treat a null Request.Body as a request with no body - #3605

Merged
velo merged 2 commits into
OpenFeign:masterfrom
sharanggupta:gh-1210-null-request-body
Oct 7, 2026
Merged

velo merged 2 commits into
OpenFeign:masterfrom
sharanggupta:gh-1210-null-request-body

Conversation

@sharanggupta

@sharanggupta sharanggupta commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Request.create(..., (Request.Body) null, ...) is documented as legal ("body ... can be null"), but the constructor stored the null and body(), charset(), isBinary() and length() dereferenced it, so any body-less Request built outside RequestTemplate threw a NullPointerException on first access (the reproducer in #1210).

  • The constructor now normalises a null body to Request.Body.empty(), the same default RequestTemplate already uses. Body.empty() yields exactly the documented contract — body() and charset() return null, isBinary() is true, length() is 0 — so clients that branch on request.body() != null (ApacheHttpClient, Logger, Vertx, ...) keep working unchanged. This follows the direction from the review of #1210 attempt to fix null body issue #1237: keep body() returning null for "no body" rather than an empty array.
  • toString() checks the body's bytes instead of the (no longer nullable) wrapper, so a body-less request still prints only the request line and headers.
  • Two tests in RequestTest pin the contract; CHANGELOG entry added under 13.17.

One intentional, visible side effect: a body-less request built through RequestTemplate previously printed a trailing Binary data line from toString() (because RequestTemplate already defaults to Body.empty(), whose asString() returns "Binary data"). With this change it prints only the request line and headers. No test asserted the old output, and Logger builds its own output.

Scope: the hasBody() accessor suggested in #1237 is deliberately left out to keep this fix minimal and API-neutral; happy to follow up separately if wanted.

Verified with ./mvnw -pl core verify: 770 tests pass, license and code-format checks pass.

Fixes #1210

`Request.create(..., (Request.Body) null, ...)` is documented as legal
("body ... can be null"), but the constructor stored the `null` and
`body()`, `charset()`, `isBinary()` and `length()` dereferenced it, so
any body-less `Request` built outside `RequestTemplate` threw a
`NullPointerException` on first access.

* The constructor now normalises a `null` body to `Request.Body.empty()`,
  the same default `RequestTemplate` already uses. `Body.empty()` yields
  exactly the documented contract: `body()` and `charset()` return
  `null`, `isBinary()` is `true` and `length()` is `0`, so clients that
  branch on `request.body() != null` keep working unchanged.
* `toString()` now checks the body's bytes instead of the (no longer
  nullable) wrapper, so a body-less request still prints only the
  request line and headers.

Fixes OpenFeign#1210
… template-built requests

Signed-off-by: Marvin Froeder <velo.br@gmail.com>
@velo
velo merged commit 5055076 into OpenFeign:master Oct 7, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't determine if RequestTemplate or Request has a null body

2 participants