Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch VndErrors to handle alphanumeric logrefs. #1293

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.2.0-HATEOAS-1291-SNAPSHOT</version>

<name>Spring HATEOAS</name>
<url>https://github.com/spring-projects/spring-hateoas</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class VndErrors extends CollectionModel<VndErrors.VndError> {

@Getter //
@JsonInclude(value = JsonInclude.Include.NON_EMPTY) //
private final Integer logref;
private final Object logref;

public VndErrors() {

Expand All @@ -91,8 +91,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 @@ -118,7 +118,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 @@ -240,7 +240,7 @@ public static class VndError extends RepresentationModel<VndError> {
private final @Nullable String path;

@Getter(onMethod = @__(@JsonInclude(JsonInclude.Include.NON_EMPTY))) //
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 @@ -252,7 +252,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 @@ -262,16 +262,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));
}

@Override
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"
}
}
}