Skip to content

Document request header behavior of ModifyRequestBody - #4258

Open
hyungzin0309 wants to merge 1 commit into
spring-cloud:mainfrom
hyungzin0309:gh-2548-document-modifyrequestbody-header-behavior
Open

Document request header behavior of ModifyRequestBody#4258
hyungzin0309 wants to merge 1 commit into
spring-cloud:mainfrom
hyungzin0309:gh-2548-document-modifyrequestbody-header-behavior

Conversation

@hyungzin0309

Copy link
Copy Markdown

ModifyRequestBody replaces the downstream request with a decorator whose headers are a copy taken before the RewriteFunction runs, and that decorator returns a new HttpHeaders instance on every getHeaders() call.

Two consequences are currently undocumented:

  1. Headers set inside the RewriteFunction never reach the downstream request — the ServerWebExchange returned by exchange.mutate() there is discarded by the filter.
  2. An in-place write to the HttpHeaders returned by exchange.getRequest().getHeaders() in a filter ordered after ModifyRequestBody is silently discarded. The same write on the underlying request throws UnsupportedOperationException, since AbstractServerHttpRequest wraps its headers with HttpHeaders.readOnlyHttpHeaders. Passing through this filter therefore turns a failing write into a silent one.

This documents both constraints and shows the supported alternative — mutating the request, which is what filter factories such as AddRequestHeader already do.

Unit tests cover the supported mutate() path and lock in the two documented constraints. Each also asserts the rewritten body reaches the chain, so they fail if the filter stops decorating the request.

Verified against main (5.0.x).

Fixes gh-2548

Link: #2548

ModifyRequestBody replaces the downstream request with a decorator whose
headers are a copy taken before the RewriteFunction runs, and that
decorator returns a new HttpHeaders instance on every getHeaders() call.

As a result, headers set inside the RewriteFunction never reach the
downstream request, and an in-place write to the HttpHeaders returned by
exchange.getRequest().getHeaders() is silently discarded rather than
failing with UnsupportedOperationException as it does on the underlying
request.

Document both constraints and show the supported alternative, mutating
the request, which is what filters such as AddRequestHeader already do.
Add unit tests covering the supported path and the two constraints. Each
test also asserts the rewritten body reaches the chain, so the tests
cannot pass if the filter stops decorating the request.

Fixes spring-cloudgh-2548

Signed-off-by: hyungzin0309 <50622006+hyungzin0309@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request headers can not be modified after request pass through ModifyRequestBodyGatewayFilter

2 participants