Deprecate HttpHeaders.writableHttpHeaders

Prior to this commit, gh-21783 introduced `ReadOnlyHttpHeaders` to avoid
parsing media types multiple times during the lifetime of an HTTP
exchange: such values are cached and the headers map is made read-only.
This also added a new `HttpHeaders.writableHttpHeaders` method to unwrap
the read-only variant when needed.

It turns out this method sends the wrong signal to the community
because:
* the underlying map might be unmodifiable even if this is not an
  instance of ReadOnlyHttpHeaders
* developers were assuming that modifying the collection that backs the
  read-only instance would work around the cached values for
  Content-Type and Accept headers

This commit adds more documentation to highlight the desired behavior
for cached values by the read-only variant, and deprecates the
`writableHttpHeaders` method as `ReadOnlyHttpHeaders` is package private
and we should not surface that concept anyway.
Instead, this commit unwraps the read-only variant if needed when a new
HttpHeaders instance is created.

Closes gh-32116
This commit is contained in:
Brian Clozel
2024-03-12 12:06:30 +01:00
parent 670fc9bb4e
commit 4b732d62c2
5 changed files with 183 additions and 155 deletions

View File

@@ -140,7 +140,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
@SuppressWarnings("ConstantConditions")
private HttpHeaders getHeaders() {
if (this.headers == null) {
this.headers = HttpHeaders.writableHttpHeaders(this.originalResponse.headers().asHttpHeaders());
this.headers = new HttpHeaders(this.originalResponse.headers().asHttpHeaders());
}
return this.headers;
}