Skip to content

Commit

Permalink
Fix ServletResponseHttpHeaders#get null handling
Browse files Browse the repository at this point in the history
ServletResponseHttpHeaders#get should be annotated with `@Nullable` and
return null instead of a singleton list containing null when there is no
content type header.

Closes gh-32362
  • Loading branch information
sdeleuze committed Mar 4, 2024
1 parent ce9dc19 commit 7493ce8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,15 @@ public String getFirst(String headerName) {
}

@Override
@Nullable
public List<String> get(Object key) {
Assert.isInstanceOf(String.class, key, "Key must be a String-based header name");

String headerName = (String) key;
if (headerName.equalsIgnoreCase(CONTENT_TYPE)) {
// Content-Type is written as an override so don't merge
return Collections.singletonList(getFirst(headerName));
String value = getFirst(headerName);
return (value != null ? Collections.singletonList(value) : null);
}

Collection<String> values1 = servletResponse.getHeaders(headerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ void getHeaders() {
assertThat(mockResponse.getCharacterEncoding()).as("Invalid Content-Type").isEqualTo("UTF-8");
}

@Test
void getHeadersWithNoContentType() {
this.response = new ServletServerHttpResponse(this.mockResponse);
assertThat(this.response.getHeaders().get(HttpHeaders.CONTENT_TYPE)).isNull();
}

@Test
void getHeadersWithContentType() {
this.mockResponse.setContentType(MediaType.TEXT_PLAIN_VALUE);
this.response = new ServletServerHttpResponse(this.mockResponse);
assertThat(this.response.getHeaders().get(HttpHeaders.CONTENT_TYPE)).containsExactly(MediaType.TEXT_PLAIN_VALUE);
}

@Test
void preExistingHeadersFromHttpServletResponse() {
String headerName = "Access-Control-Allow-Origin";
Expand Down

0 comments on commit 7493ce8

Please sign in to comment.