Polishing

This commit is contained in:
Sam Brannen
2023-02-19 17:43:31 +01:00
parent 2d62be8590
commit 6d24e62e83
8 changed files with 226 additions and 290 deletions

View File

@@ -33,7 +33,7 @@ import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
* the Jackson library.
*
* <p>The annotations ensure the {@link ProblemDetail#getProperties() properties}
* map is unwrapped and rendered as top level JSON properties, and likewise that
* map is unwrapped and rendered as top-level JSON properties, and likewise that
* the {@code properties} map contains unknown properties from the JSON.
*
* <p>{@link Jackson2ObjectMapperBuilder} automatically registers this as a

View File

@@ -30,15 +30,14 @@ import org.springframework.lang.Nullable;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
/**
* Provides the same declarations as {@link ProblemDetailJacksonMixin}, and
* some additional ones to support XML serialization when
* jackson-dataformat-xml is on the classpath. Customizes the XML root element
* name and adds namespace information.
* Provides the same declarations as {@link ProblemDetailJacksonMixin} and some
* additional ones to support XML serialization when {@code jackson-dataformat-xml}
* is on the classpath. Customizes the XML root element name and adds namespace
* information.
*
* <p>Note that we can't use {@code JsonRootName} to specify the namespace
* since that is not inherited by fields of the class. This is why we need a
* dedicated mixin for use when jackson-dataformat-xml is on the classpath.
* For more details, see
* <p>Note that we cannot use {@code @JsonRootName} to specify the namespace since that
* is not inherited by fields of the class. This is why we need a dedicated "mix-in"
* when {@code jackson-dataformat-xml} is on the classpath. For more details, see
* <a href="https://github.com/FasterXML/jackson-dataformat-xml/issues/355">FasterXML/jackson-dataformat-xml#355</a>.
*
* @author Rossen Stoyanchev

View File

@@ -16,11 +16,11 @@
package org.springframework.http.converter.json;
import java.net.URI;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
@@ -34,64 +34,73 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Rossen Stoyanchev
* @since 6.0
*/
public class ProblemDetailJacksonMixinTests {
class ProblemDetailJacksonMixinTests {
private final ObjectMapper mapper = new Jackson2ObjectMapperBuilder().build();
@Test
void writeStatusAndHeaders() throws Exception {
testWrite(
ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Missing header"),
"{\"type\":\"about:blank\"," +
"\"title\":\"Bad Request\"," +
"\"status\":400," +
"\"detail\":\"Missing header\"}");
ProblemDetail detail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Missing header");
testWrite(detail,
"""
{
"type": "about:blank",
"title": "Bad Request",
"status": 400,
"detail": "Missing header"
}""");
}
@Test
void writeCustomProperty() throws Exception {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Missing header");
problemDetail.setProperty("host", "abc.org");
problemDetail.setProperty("user", null);
ProblemDetail detail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Missing header");
detail.setProperty("host", "abc.org");
detail.setProperty("user", null);
testWrite(problemDetail,
"{\"type\":\"about:blank\"," +
"\"title\":\"Bad Request\"," +
"\"status\":400," +
"\"detail\":\"Missing header\"," +
"\"host\":\"abc.org\"," +
"\"user\":null}");
testWrite(detail, """
{
"type": "about:blank",
"title": "Bad Request",
"status": 400,
"detail": "Missing header",
"host": "abc.org",
"user": null
}""");
}
@Test
void readCustomProperty() throws Exception {
ProblemDetail detail = this.mapper.readValue(
"{\"type\":\"about:blank\"," +
"\"title\":\"Bad Request\"," +
"\"status\":400," +
"\"detail\":\"Missing header\"," +
"\"host\":\"abc.org\"," +
"\"user\":null}", ProblemDetail.class);
ProblemDetail detail = this.mapper.readValue("""
{
"type": "about:blank",
"title": "Bad Request",
"status": 400,
"detail": "Missing header",
"host": "abc.org",
"user": null
}""", ProblemDetail.class);
assertThat(detail.getType()).isEqualTo(URI.create("about:blank"));
assertThat(detail.getTitle()).isEqualTo("Bad Request");
assertThat(detail.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
assertThat(detail.getDetail()).isEqualTo("Missing header");
assertThat(detail.getProperties()).containsEntry("host", "abc.org");
assertThat(detail.getProperties()).containsEntry("user", null);
assertThat(detail.getProperties())
.containsEntry("host", "abc.org")
.containsEntry("user", null);
}
@Test
void readCustomPropertyFromXml() throws Exception {
ObjectMapper xmlMapper = new Jackson2ObjectMapperBuilder().createXmlMapper(true).build();
ProblemDetail detail = xmlMapper.readValue(
"<problem xmlns=\"urn:ietf:rfc:7807\">" +
"<type>about:blank</type>" +
"<title>Bad Request</title>" +
"<status>400</status>" +
"<detail>Missing header</detail>" +
"<host>abc.org</host>" +
"</problem>", ProblemDetail.class);
ProblemDetail detail = xmlMapper.readValue("""
<problem xmlns="urn:ietf:rfc:7807">
<type>about:blank</type>
<title>Bad Request</title>
<status>400</status>
<detail>Missing header</detail>
<host>abc.org</host>
</problem>""", ProblemDetail.class);
assertThat(detail.getType()).isEqualTo(URI.create("about:blank"));
assertThat(detail.getTitle()).isEqualTo("Bad Request");
@@ -102,7 +111,7 @@ public class ProblemDetailJacksonMixinTests {
private void testWrite(ProblemDetail problemDetail, String expected) throws Exception {
String output = this.mapper.writeValueAsString(problemDetail);
assertThat(output).isEqualTo(expected);
JSONAssert.assertEquals(expected, output, false);
}
}