From 5ba0cc5860b89e3984933a29b03a4b15d39ea09c Mon Sep 17 00:00:00 2001 From: rockinrimmer Date: Thu, 15 Sep 2016 15:15:42 +0100 Subject: [PATCH] =?UTF-8?q?Check=20if=20object=20is=20already=20a=20Respon?= =?UTF-8?q?seEntity,=20if=20so=20return=20instead=20of=20=E2=80=A6=20(#134?= =?UTF-8?q?3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Check if object is already a ResponseEntity, if so return instead of wrapping in another ResponseEnitiy. Which caused headers and status code from original ResponseEnitiy to be lost and for body to be output with whole ResponseEntity instead of just the body * added test --- .../netflix/rx/SingleReturnValueHandler.java | 4 ++++ .../rx/SingleReturnValueHandlerTest.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandler.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandler.java index 1db91b76..a875e511 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandler.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandler.java @@ -100,6 +100,10 @@ public class SingleReturnValueHandler implements AsyncHandlerMethodReturnValueHa .map(new Func1>() { @Override public ResponseEntity call(Object object) { + if (object instanceof ResponseEntity){ + return (ResponseEntity) object; + } + return new ResponseEntity(object, getHttpHeaders(responseEntity), getHttpStatus(responseEntity)); diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandlerTest.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandlerTest.java index 8c7f0c45..e35ee000 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandlerTest.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandlerTest.java @@ -69,6 +69,11 @@ public class SingleReturnValueHandlerTest { return new ResponseEntity<>(Single.just("single value"), HttpStatus.NOT_FOUND); } + + @RequestMapping(method = RequestMethod.GET, value = "/singleCreatedWithResponse") + public Single> singleOuterWithResponse() { + return Single.just(new ResponseEntity<>("single value", HttpStatus.CREATED)); + } @RequestMapping(method = RequestMethod.GET, value = "/throw") public Single error() { @@ -114,6 +119,19 @@ public class SingleReturnValueHandlerTest { assertNotNull(response); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); } + + @Test + public void shouldRetrieveSingleValueWithCreatedCode() { + + // when + ResponseEntity response = restTemplate.getForEntity(path("/singleCreatedWithResponse"), + String.class); + + // then + assertNotNull(response); + assertEquals(HttpStatus.CREATED, response.getStatusCode()); + assertEquals("single value", response.getBody()); + } private String path(String context) { return String.format("http://localhost:%d%s", port, context);