From 27fe7de1c95f5b9480ab4c3cb0e38c4a8f75fbd5 Mon Sep 17 00:00:00 2001 From: "Greg L. Turnquist" Date: Fri, 26 Jun 2020 11:02:54 -0500 Subject: [PATCH] #1313 - Override Problem.withStatus in ExtendedProblem to preserve payload. When you use the withStatus() method on an ExtendedProblem, it hands back a Problem, which drops the payload. This commit adds the same sort of override already provided for the other attributes in ExtendedProblem. --- .../hateoas/mediatype/problem/Problem.java | 9 +++++++++ .../mediatype/problem/JacksonSerializationTest.java | 1 + .../hateoas/mediatype/problem/extension.json | 3 ++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/hateoas/mediatype/problem/Problem.java b/src/main/java/org/springframework/hateoas/mediatype/problem/Problem.java index 99e79d78..f9a8e406 100644 --- a/src/main/java/org/springframework/hateoas/mediatype/problem/Problem.java +++ b/src/main/java/org/springframework/hateoas/mediatype/problem/Problem.java @@ -195,6 +195,15 @@ public class Problem { return new ExtendedProblem<>(getType(), title, getStatus(), getDetail(), getInstance(), extendedProperties); } + /* + * (non-Javadoc) + * @see org.springframework.hateoas.mediatype.problem.Problem#withStatus(org.springframework.http.HttpStatus) + */ + @Override + public ExtendedProblem withStatus(@Nullable HttpStatus status) { + return new ExtendedProblem<>(getType(), getTitle(), status, getDetail(), getInstance(), extendedProperties); + } + /* * (non-Javadoc) * @see org.springframework.hateoas.mediatype.problem.Problem#withDetail(java.lang.String) diff --git a/src/test/java/org/springframework/hateoas/mediatype/problem/JacksonSerializationTest.java b/src/test/java/org/springframework/hateoas/mediatype/problem/JacksonSerializationTest.java index e4a93b96..64586b13 100644 --- a/src/test/java/org/springframework/hateoas/mediatype/problem/JacksonSerializationTest.java +++ b/src/test/java/org/springframework/hateoas/mediatype/problem/JacksonSerializationTest.java @@ -219,6 +219,7 @@ class JacksonSerializationTest { ExtendedProblem problem = Problem.create(details) .withType(URI.create("https://example.com/probs/out-of-credit")) // .withTitle("You do not have enough credit.") // + .withStatus(HttpStatus.BAD_REQUEST) // .withDetail("Your current balance is 30, but that costs 50.") // .withInstance(URI.create("/account/12345/msgs/abc")); diff --git a/src/test/resources/org/springframework/hateoas/mediatype/problem/extension.json b/src/test/resources/org/springframework/hateoas/mediatype/problem/extension.json index f6f6cd69..bfab6dd4 100644 --- a/src/test/resources/org/springframework/hateoas/mediatype/problem/extension.json +++ b/src/test/resources/org/springframework/hateoas/mediatype/problem/extension.json @@ -4,5 +4,6 @@ "detail" : "Your current balance is 30, but that costs 50.", "instance" : "/account/12345/msgs/abc", "balance" : 30, - "accounts" : [ "/account/12345", "/account/67890" ] + "accounts" : [ "/account/12345", "/account/67890" ], + "status" : 400 } \ No newline at end of file