Skip to content

Commit

Permalink
ResponseEntityResultHandler overwrites headers
Browse files Browse the repository at this point in the history
Prior to this commit, controller handlers (regular and exception
handlers as well) would not overwrite existing HTTP response headers on
the exchange. This would lead to situations where Content-Type values
set during the initial handling phase would not be overwritten when
handling an error later on.

This commit aligns the implementation of that result handler on the
Spring MVC one in that regard.

Issue: SPR-17082
(Cherry-picked from 195f3f0)
  • Loading branch information
bclozel committed Jul 24, 2018
1 parent 0b5c099 commit 407bd96
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ else if (returnValue instanceof HttpHeaders) {
HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
if (!entityHeaders.isEmpty()) {
entityHeaders.entrySet().stream()
.filter(entry -> !responseHeaders.containsKey(entry.getKey()))
.forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.HttpMessageWriter;
Expand Down Expand Up @@ -334,6 +335,22 @@ public void handleMonoWithWildcardBodyTypeAndNullBody() throws Exception {
assertResponseBodyIsEmpty(exchange);
}

@Test // SPR-17082
public void handleResponseEntityWithExistingResponseHeaders() throws Exception {
ResponseEntity<Void> value = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).build();
MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
HandlerResult result = handlerResult(value, returnType);
MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
exchange.getResponse().getHeaders().setContentType(MediaType.TEXT_PLAIN);
this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));

assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode());
assertEquals(1, exchange.getResponse().getHeaders().size());
assertEquals(MediaType.APPLICATION_JSON, exchange.getResponse().getHeaders().getContentType());
assertResponseBodyIsEmpty(exchange);
}



private void testHandle(Object returnValue, MethodParameter returnType) {
MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
Expand Down

0 comments on commit 407bd96

Please sign in to comment.