diff --git a/src/main/java/org/springframework/hateoas/mediatype/vnderrors/VndErrors.java b/src/main/java/org/springframework/hateoas/mediatype/vnderrors/VndErrors.java index 94f6983a7..4bb8802d7 100644 --- a/src/main/java/org/springframework/hateoas/mediatype/vnderrors/VndErrors.java +++ b/src/main/java/org/springframework/hateoas/mediatype/vnderrors/VndErrors.java @@ -73,7 +73,7 @@ public class VndErrors extends CollectionModel { private final String message; @JsonInclude(value = JsonInclude.Include.NON_EMPTY) // - private final Integer logref; + private final Object logref; public VndErrors() { @@ -86,8 +86,8 @@ public VndErrors() { * Creates a new {@link VndErrors} instance containing a single {@link VndError} with the given logref, message and * optional {@link Link}s. */ - public VndErrors(String logref, String message, Link... links) { - this(new VndError(message, null, Integer.parseInt(logref), links)); + public VndErrors(Object logref, String message, Link... links) { + this(new VndError(message, null, logref, links)); } /** @@ -113,7 +113,7 @@ public VndErrors(VndError error, VndError... errors) { */ @JsonCreator public VndErrors(@JsonProperty("_embedded") List errors, @JsonProperty("message") String message, - @JsonProperty("logref") Integer logref, @JsonProperty("_links") Links links) { + @JsonProperty("logref") Object logref, @JsonProperty("_links") Links links) { Assert.notNull(errors, "Errors must not be null!"); // Retain for compatibility Assert.notEmpty(errors, "Errors must not be empty!"); @@ -218,7 +218,7 @@ public String getMessage() { return this.message; } - public Integer getLogref() { + public Object getLogref() { return this.logref; } @@ -258,7 +258,7 @@ public static class VndError extends RepresentationModel { private final @Nullable String path; - private final Integer logref; + private final Object logref; /** * Creates a new {@link VndError} with a message and optional a path and a logref. @@ -270,7 +270,7 @@ public static class VndError extends RepresentationModel { */ @JsonCreator public VndError(@JsonProperty("message") String message, @JsonProperty("path") @Nullable String path, - @JsonProperty("logref") Integer logref, @JsonProperty("_links") List links) { + @JsonProperty("logref") Object logref, @JsonProperty("_links") List links) { Assert.hasText(message, "Message must not be null or empty!"); @@ -280,16 +280,16 @@ public VndError(@JsonProperty("message") String message, @JsonProperty("path") @ this.add(links); } - public VndError(String message, @Nullable String path, Integer logref, Link... link) { + public VndError(String message, @Nullable String path, Object logref, Link... link) { this(message, path, logref, Arrays.asList(link)); } /** - * @deprecated Use {@link #VndError(String, String, Integer, Link...)} (with proper ordering of arguments) + * @deprecated Use {@link #VndError(String, String, Object, Link...)} (with proper ordering of arguments) */ @Deprecated public VndError(String logref, String message, Link... links) { - this(message, null, Integer.parseInt(logref), Arrays.asList(links)); + this(message, null, logref, Arrays.asList(links)); } public String getMessage() { @@ -303,7 +303,7 @@ public String getPath() { } @JsonInclude(JsonInclude.Include.NON_EMPTY) - public Integer getLogref() { + public Object getLogref() { return this.logref; } diff --git a/src/test/java/org/springframework/hateoas/mediatype/vnderror/VndErrorsMarshallingTest.java b/src/test/java/org/springframework/hateoas/mediatype/vnderror/VndErrorsMarshallingTest.java index e83e4d6b2..dfa7ca7b2 100755 --- a/src/test/java/org/springframework/hateoas/mediatype/vnderror/VndErrorsMarshallingTest.java +++ b/src/test/java/org/springframework/hateoas/mediatype/vnderror/VndErrorsMarshallingTest.java @@ -20,6 +20,7 @@ import java.io.IOException; +import com.fasterxml.jackson.core.JsonProcessingException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.core.io.ClassPathResource; @@ -133,4 +134,14 @@ public void nestedVndErrorsShouldSerialize() throws IOException { assertThat(mapper.writeValueAsString(vndErrors)).isEqualToIgnoringWhitespace(json); } + + @Test // #1291 + void basicVndErrorShouldSerialize() throws IOException { + + VndError error = new VndError("message", "path", "alphaLogref", Link.of("foo", "bar")); + + String json = read(new ClassPathResource("vnderror-string-logref.json", getClass())); + + assertThat(mapper.writeValueAsString(error)).isEqualToIgnoringWhitespace(json); + } } diff --git a/src/test/java/org/springframework/hateoas/mediatype/vnderror/VndErrorsUnitTest.java b/src/test/java/org/springframework/hateoas/mediatype/vnderror/VndErrorsUnitTest.java index 019534218..587ed0b54 100755 --- a/src/test/java/org/springframework/hateoas/mediatype/vnderror/VndErrorsUnitTest.java +++ b/src/test/java/org/springframework/hateoas/mediatype/vnderror/VndErrorsUnitTest.java @@ -90,4 +90,14 @@ void vndErrorsRendersToStringCorrectly() { assertThat(errors.toString()) .isEqualTo("VndErrors[VndError[logref: 42, message: message, links: [;rel=\"bar\"]]]"); } + + @Test // #1291 + void logRefCanBeAlphabeticalThroughMainConstructor() { + new VndErrors().withError(new VndError("message", "path", "alphaLogref", Link.of("foo", "bar"))); + } + + @Test // #1291 + void logRefCanBeAlphabeticalThroughDeprecatedConstructor() { + new VndErrors().withError(new VndError("alphaLogref", "message", Link.of("/link").withSelfRel())); + } } diff --git a/src/test/resources/org/springframework/hateoas/mediatype/vnderror/vnderror-string-logref.json b/src/test/resources/org/springframework/hateoas/mediatype/vnderror/vnderror-string-logref.json new file mode 100644 index 000000000..49feafe0d --- /dev/null +++ b/src/test/resources/org/springframework/hateoas/mediatype/vnderror/vnderror-string-logref.json @@ -0,0 +1,10 @@ +{ + "message" : "message", + "path" : "path", + "logref" : "alphaLogref", + "_links" : { + "bar" : { + "href" : "foo" + } + } +}