Skip to content

Deep copy default headers, cookies in client builders - #37325

Merged
bclozel merged 1 commit into
spring-projects:mainfrom
christianaurichzm:client-builder-deep-copy
Oct 22, 2026
Merged

bclozel merged 1 commit into
spring-projects:mainfrom
christianaurichzm:client-builder-deep-copy

Conversation

@christianaurichzm

Copy link
Copy Markdown
Contributor

Builders returned by clone() and mutate() for RestClient, WebClient and WebTestClient get a copy of the default headers and cookies maps, but the value lists are still shared with the original. So adding a value to a header or cookie that's already there leaks back into the original:

RestClient base = RestClient.builder().defaultCookie("id", "one").build();
base.mutate().defaultCookie("id", "two").build();

base.mutate().defaultCookies(System.out::println); // {id=[one, two]}

This is the same kind of side effect as #35698, and RestTestClient still has it, since its builder clone goes through RestClient.Builder#clone().

WebTestClient also still has the problem #25992 fixed for WebClient: its build() passes views of the builder's maps to the client, so changing the builder afterwards changes what an already built client sends.

The fix copies the value lists, the same way build() already does for RestClient and WebClient.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 23, 2026
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Sep 24, 2026
Prior to this commit, the copy constructors of DefaultRestClientBuilder,
DefaultWebClientBuilder and DefaultWebTestClientBuilder copied the
default headers and cookies maps but shared their value lists. Adding a
value under an existing name on a builder returned by clone() or
mutate() therefore also changed the builder it was copied from, as well
as every builder later returned by mutate() on the same client.

DefaultWebTestClientBuilder#build() also handed read-only views of its
live maps to the built client, so later changes to the builder leaked
into clients that were already built.

This commit copies the value lists in the copy constructors, as build()
already does for RestClient and WebClient, and in
DefaultWebTestClientBuilder#build().

Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
@bclozel bclozel self-assigned this Oct 22, 2026
@bclozel bclozel added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 22, 2026
@bclozel bclozel added this to the 7.1.0-RC2 milestone Oct 22, 2026
@bclozel
bclozel force-pushed the client-builder-deep-copy branch from f8bb1f2 to 4cf6335 Compare October 22, 2026 15:19
@bclozel
bclozel merged commit 4cf6335 into spring-projects:main Oct 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants