#93 - Fix VndErrors un-marshalling when using Jackson.
Added equals() and hashCode() methods on VndErrors and VndError. Original pull request: #94.
This commit is contained in:
committed by
Oliver Gierke
parent
c5caea5efd
commit
eed331dbe2
@@ -27,8 +27,6 @@ import javax.xml.bind.annotation.XmlType;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
/**
|
||||
* A representation model class to be rendered as specified for the media type {@code application/vnd.error}.
|
||||
*
|
||||
@@ -38,7 +36,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
|
||||
@XmlRootElement(name = "errors")
|
||||
public class VndErrors implements Iterable<VndErrors.VndError> {
|
||||
|
||||
@XmlElement(name = "error")
|
||||
@XmlElement(name = "error")//
|
||||
private final List<VndError> vndErrors;
|
||||
|
||||
/**
|
||||
@@ -68,6 +66,20 @@ public class VndErrors implements Iterable<VndErrors.VndError> {
|
||||
this.vndErrors.addAll(Arrays.asList(errors));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link VndErrors} wrapper for the given {@link VndErrors}.
|
||||
*
|
||||
* @param errors must not be {@literal null} or empty.
|
||||
*/
|
||||
@com.fasterxml.jackson.annotation.JsonCreator
|
||||
@org.codehaus.jackson.annotate.JsonCreator
|
||||
public VndErrors(List<VndError> errors) {
|
||||
|
||||
Assert.notNull(errors, "Errors must not be null!");
|
||||
Assert.isTrue(!errors.isEmpty(), "Errors must not be empty!");
|
||||
this.vndErrors = errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected default constructor to allow JAXB marshalling.
|
||||
*/
|
||||
@@ -96,7 +108,7 @@ public class VndErrors implements Iterable<VndErrors.VndError> {
|
||||
return vndErrors;
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Iterable#iterator()
|
||||
*/
|
||||
@@ -105,6 +117,34 @@ public class VndErrors implements Iterable<VndErrors.VndError> {
|
||||
return this.vndErrors.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return vndErrors.hashCode();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj instanceof VndErrors)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
VndErrors that = (VndErrors) obj;
|
||||
return this.vndErrors.equals(that.vndErrors);
|
||||
}
|
||||
|
||||
/**
|
||||
* A single {@link VndError}.
|
||||
*
|
||||
@@ -113,14 +153,14 @@ public class VndErrors implements Iterable<VndErrors.VndError> {
|
||||
@XmlType
|
||||
public static class VndError extends ResourceSupport {
|
||||
|
||||
@com.fasterxml.jackson.annotation.JsonProperty
|
||||
@org.codehaus.jackson.annotate.JsonProperty
|
||||
@XmlAttribute
|
||||
@com.fasterxml.jackson.annotation.JsonProperty//
|
||||
@org.codehaus.jackson.annotate.JsonProperty//
|
||||
@XmlAttribute//
|
||||
private final String logref;
|
||||
|
||||
@com.fasterxml.jackson.annotation.JsonProperty
|
||||
@org.codehaus.jackson.annotate.JsonProperty
|
||||
@XmlElement
|
||||
@com.fasterxml.jackson.annotation.JsonProperty//
|
||||
@org.codehaus.jackson.annotate.JsonProperty//
|
||||
@XmlElement//
|
||||
private final String message;
|
||||
|
||||
/**
|
||||
@@ -166,5 +206,40 @@ public class VndErrors implements Iterable<VndErrors.VndError> {
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.ResourceSupport#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = 17;
|
||||
|
||||
result += 31 * logref.hashCode();
|
||||
result += 31 * message.hashCode();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.ResourceSupport#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj instanceof VndError)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
VndError that = (VndError) obj;
|
||||
|
||||
return this.logref.equals(that.logref) && this.message.equals(that.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import static org.junit.Assert.*;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.MappedByteBuffer;
|
||||
@@ -28,6 +29,7 @@ import java.nio.charset.Charset;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.map.SerializationConfig.Feature;
|
||||
@@ -50,6 +52,7 @@ public class VndErrorsMarshallingTest {
|
||||
ObjectMapper jackson1Mapper;
|
||||
com.fasterxml.jackson.databind.ObjectMapper jackson2Mapper;
|
||||
Marshaller marshaller;
|
||||
Unmarshaller unmarshaller;
|
||||
|
||||
VndErrors errors;
|
||||
String jsonReference;
|
||||
@@ -74,9 +77,10 @@ public class VndErrorsMarshallingTest {
|
||||
|
||||
JAXBContext context = JAXBContext.newInstance(VndErrors.class);
|
||||
marshaller = context.createMarshaller();
|
||||
unmarshaller = context.createUnmarshaller();
|
||||
|
||||
VndError error = new VndError("42", "Validation failed!", //
|
||||
new Link("http://...", "help"), new Link("http://...", "describes"));
|
||||
new Link("http://...", "describes"), new Link("http://...", "help"));
|
||||
errors = new VndErrors(error, error, error);
|
||||
}
|
||||
|
||||
@@ -98,6 +102,32 @@ public class VndErrorsMarshallingTest {
|
||||
assertThat(writer.toString(), is(xmlReference));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #93, #94
|
||||
*/
|
||||
@Test
|
||||
public void jackson1UnMarshalling() throws Exception {
|
||||
VndErrors actual = jackson1Mapper.readValue(jsonReference, VndErrors.class);
|
||||
assertThat(actual, is(errors));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #93, #94
|
||||
*/
|
||||
@Test
|
||||
public void jackson2UnMarshalling() throws Exception {
|
||||
assertThat(jackson2Mapper.readValue(jsonReference, VndErrors.class), is(errors));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #93, #94
|
||||
*/
|
||||
@Test
|
||||
public void jaxbUnMarshalling() throws Exception {
|
||||
VndErrors actual = (VndErrors) unmarshaller.unmarshal(new StringReader(xmlReference));
|
||||
assertThat(actual, is(errors));
|
||||
}
|
||||
|
||||
private static String readFile(org.springframework.core.io.Resource resource) throws IOException {
|
||||
|
||||
FileInputStream stream = new FileInputStream(resource.getFile());
|
||||
|
||||
@@ -1 +1 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><errors xmlns:ns2="http://www.w3.org/2005/Atom"><error logref="42"><ns2:link href="http://..." rel="help"/><ns2:link href="http://..." rel="describes"/><message>Validation failed!</message></error><error logref="42"><ns2:link href="http://..." rel="help"/><ns2:link href="http://..." rel="describes"/><message>Validation failed!</message></error><error logref="42"><ns2:link href="http://..." rel="help"/><ns2:link href="http://..." rel="describes"/><message>Validation failed!</message></error></errors>
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><errors xmlns:ns2="http://www.w3.org/2005/Atom"><error logref="42"><ns2:link href="http://..." rel="describes"/><ns2:link href="http://..." rel="help"/><message>Validation failed!</message></error><error logref="42"><ns2:link href="http://..." rel="describes"/><ns2:link href="http://..." rel="help"/><message>Validation failed!</message></error><error logref="42"><ns2:link href="http://..." rel="describes"/><ns2:link href="http://..." rel="help"/><message>Validation failed!</message></error></errors>
|
||||
Reference in New Issue
Block a user