Repository navigation
Treat a null Request.Body as a request with no body - #3605
Merged
Merged
Conversation
`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
velo
approved these changes
Oct 7, 2026
… template-built requests Signed-off-by: Marvin Froeder <velo.br@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Request.create(..., (Request.Body) null, ...)is documented as legal ("body ... can be null"), but the constructor stored thenullandbody(),charset(),isBinary()andlength()dereferenced it, so any body-lessRequestbuilt outsideRequestTemplatethrew aNullPointerExceptionon first access (the reproducer in #1210).nullbody toRequest.Body.empty(), the same defaultRequestTemplatealready uses.Body.empty()yields exactly the documented contract —body()andcharset()returnnull,isBinary()istrue,length()is0— so clients that branch onrequest.body() != null(ApacheHttpClient, Logger, Vertx, ...) keep working unchanged. This follows the direction from the review of #1210 attempt to fix null body issue #1237: keepbody()returningnullfor "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.RequestTestpin the contract; CHANGELOG entry added under 13.17.One intentional, visible side effect: a body-less request built through
RequestTemplatepreviously printed a trailingBinary dataline fromtoString()(becauseRequestTemplatealready defaults toBody.empty(), whoseasString()returns"Binary data"). With this change it prints only the request line and headers. No test asserted the old output, andLoggerbuilds 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