From 7493ce86b6b6014287789069d41943f5d3ac02fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 4 Mar 2024 14:25:51 +0100 Subject: [PATCH] Fix ServletResponseHttpHeaders#get null handling 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 --- .../http/server/ServletServerHttpResponse.java | 4 +++- .../http/server/ServletServerHttpResponseTests.java | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java index 7ec872f05bc9..875e5bd3127a 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java @@ -171,13 +171,15 @@ public String getFirst(String headerName) { } @Override + @Nullable public List 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 values1 = servletResponse.getHeaders(headerName); diff --git a/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpResponseTests.java b/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpResponseTests.java index efdf1bf691f4..34a40016ac03 100644 --- a/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpResponseTests.java +++ b/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpResponseTests.java @@ -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";