Skip to content

Commit

Permalink
#1327 - Support alphanumeric logref for VndError.
Browse files Browse the repository at this point in the history
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.

Backport of: #1291.
Original pull request: #1293.
  • Loading branch information
gregturn authored and odrotbohm committed Jul 27, 2020
1 parent a166f41 commit 9ac8bae
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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));
}

/**
Expand All @@ -113,7 +113,7 @@ public VndErrors(VndError error, VndError... errors) {
*/
@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!");
Expand Down Expand Up @@ -218,7 +218,7 @@ public String getMessage() {
return this.message;
}

public Integer getLogref() {
public Object getLogref() {
return this.logref;
}

Expand Down Expand Up @@ -258,7 +258,7 @@ public static class VndError extends RepresentationModel<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.
Expand All @@ -270,7 +270,7 @@ public static class VndError extends RepresentationModel<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!");

Expand All @@ -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() {
Expand All @@ -303,7 +303,7 @@ public String getPath() {
}

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public Integer getLogref() {
public Object getLogref() {
return this.logref;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,14 @@ void vndErrorsRendersToStringCorrectly() {
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()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"message" : "message",
"path" : "path",
"logref" : "alphaLogref",
"_links" : {
"bar" : {
"href" : "foo"
}
}
}

0 comments on commit 9ac8bae

Please sign in to comment.