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
This commit is contained in:
Brian Clozel
2024-10-25 10:22:12 +02:00
parent bbe362c0e6
commit a06bbccf9e
2 changed files with 12 additions and 1 deletions

View File

@@ -1872,7 +1872,10 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
if (headers == EMPTY) {
return new HttpHeaders();
}
return (headers instanceof ReadOnlyHttpHeaders ? new HttpHeaders(headers.headers) : headers);
while (headers.headers instanceof HttpHeaders wrapped) {
headers = wrapped;
}
return new HttpHeaders(headers.headers);
}
/**

View File

@@ -57,6 +57,14 @@ class HttpHeadersTests {
private final HttpHeaders headers = new HttpHeaders();
@Test
void writableHttpHeadersUnwrapsMultiple() {
HttpHeaders originalExchangeHeaders = HttpHeaders.readOnlyHttpHeaders(new HttpHeaders());
HttpHeaders firewallHeaders = new HttpHeaders(originalExchangeHeaders);
HttpHeaders writeable = HttpHeaders.writableHttpHeaders(firewallHeaders);
writeable.setContentType(MediaType.APPLICATION_JSON);
}
@Test
void getOrEmpty() {
String key = "FOO";