Skip to content

Commit a06bbcc

Browse files
committed
HttpHeaders.writeableHttpHeaders should unwrap many times
Prior to this commit, the `HttpHeaders.writeableHttpHeaders` would only consider headers read-only instances that were wrapped once by `HttpHeaders.readOnlyHttpHeaders`. This does not work when other `HttpHeaders` wrappers are involved in the chain. This commit ensures that `writeableHttpHeaders` unwraps all headers instances down to the actual multivalue map and create a new headers instance out of it. Fixes gh-33789
1 parent bbe362c commit a06bbcc

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Diff for: spring-web/src/main/java/org/springframework/http/HttpHeaders.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,10 @@ public static HttpHeaders writableHttpHeaders(HttpHeaders headers) {
18721872
if (headers == EMPTY) {
18731873
return new HttpHeaders();
18741874
}
1875-
return (headers instanceof ReadOnlyHttpHeaders ? new HttpHeaders(headers.headers) : headers);
1875+
while (headers.headers instanceof HttpHeaders wrapped) {
1876+
headers = wrapped;
1877+
}
1878+
return new HttpHeaders(headers.headers);
18761879
}
18771880

18781881
/**

Diff for: spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ class HttpHeadersTests {
5757
private final HttpHeaders headers = new HttpHeaders();
5858

5959

60+
@Test
61+
void writableHttpHeadersUnwrapsMultiple() {
62+
HttpHeaders originalExchangeHeaders = HttpHeaders.readOnlyHttpHeaders(new HttpHeaders());
63+
HttpHeaders firewallHeaders = new HttpHeaders(originalExchangeHeaders);
64+
HttpHeaders writeable = HttpHeaders.writableHttpHeaders(firewallHeaders);
65+
writeable.setContentType(MediaType.APPLICATION_JSON);
66+
}
67+
6068
@Test
6169
void getOrEmpty() {
6270
String key = "FOO";

0 commit comments

Comments
 (0)