From 195f3f07e7ea01b58ffc5633447f48c63128d662 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 24 Jul 2018 22:21:30 +0200 Subject: [PATCH] ResponseEntityResultHandler overwrites headers 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 --- .../annotation/ResponseEntityResultHandler.java | 1 - .../ResponseEntityResultHandlerTests.java | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java index f581ab295e..ac1331969e 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java @@ -155,7 +155,6 @@ public class ResponseEntityResultHandler extends AbstractMessageWriterResultHand 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())); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java index a3f3c7255a..7be305b999 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java @@ -44,6 +44,7 @@ import org.springframework.core.io.buffer.support.DataBufferTestUtils; 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; @@ -334,6 +335,22 @@ public class ResponseEntityResultHandlerTests { assertResponseBodyIsEmpty(exchange); } + @Test // SPR-17082 + public void handleResponseEntityWithExistingResponseHeaders() throws Exception { + ResponseEntity 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"));