#1291 - Support alphanumeric logref for VndError.
According to https://github.com/blongden/vnd.error, logref is for "expressing a (numeric/alpha/alphanumeric) identifier". This patches `VndError` to support both strings and integers, ensuring each serializes properly. NOTE: `VndErrors` has been deprecated due to the spec itself being dead since 2014. However, it must be supported until fully removed from Spring HATEOAS. Original pull request: #1293.
This commit is contained in:
committed by
Oliver Drotbohm
parent
43528523eb
commit
5f625ede2d
@@ -73,7 +73,7 @@ public class VndErrors extends CollectionModel<VndErrors.VndError> {
|
||||
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 class VndErrors extends CollectionModel<VndErrors.VndError> {
|
||||
* 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 class VndErrors extends CollectionModel<VndErrors.VndError> {
|
||||
*/
|
||||
@JsonCreator
|
||||
public VndErrors(@JsonProperty("_embedded") List<VndError> 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 class VndErrors extends CollectionModel<VndErrors.VndError> {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public Integer getLogref() {
|
||||
public Object getLogref() {
|
||||
return this.logref;
|
||||
}
|
||||
|
||||
@@ -258,7 +258,7 @@ public class VndErrors extends CollectionModel<VndErrors.VndError> {
|
||||
|
||||
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 class VndErrors extends CollectionModel<VndErrors.VndError> {
|
||||
*/
|
||||
@JsonCreator
|
||||
public VndError(@JsonProperty("message") String message, @JsonProperty("path") @Nullable String path,
|
||||
@JsonProperty("logref") Integer logref, @JsonProperty("_links") List<Link> links) {
|
||||
@JsonProperty("logref") Object logref, @JsonProperty("_links") List<Link> links) {
|
||||
|
||||
Assert.hasText(message, "Message must not be null or empty!");
|
||||
|
||||
@@ -280,16 +280,16 @@ public class VndErrors extends CollectionModel<VndErrors.VndError> {
|
||||
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 class VndErrors extends CollectionModel<VndErrors.VndError> {
|
||||
}
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
public Integer getLogref() {
|
||||
public Object getLogref() {
|
||||
return this.logref;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import static org.springframework.hateoas.support.MappingUtils.*;
|
||||
|
||||
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 @@ class VndErrorsMarshallingTest {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,4 +90,14 @@ class VndErrorsUnitTest {
|
||||
assertThat(errors.toString())
|
||||
.isEqualTo("VndErrors[VndError[logref: 42, message: message, links: [<foo>;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()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"message" : "message",
|
||||
"path" : "path",
|
||||
"logref" : "alphaLogref",
|
||||
"_links" : {
|
||||
"bar" : {
|
||||
"href" : "foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user