Commit 5c1a2fc1 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Workaround for fixing node type attribute issue

parent fc2d7583
Pipeline #727 passed with stages
in 1 minute and 2 seconds
package it.inaf.oats.vospace.datamodel;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import net.ivoa.xml.vospace.v2.Node;
/**
* JAXB automatically generates the xsi:type attribute, however it doesn't fill
* it for the root node (it seems that this is by design). Since we need it, we
* manually added it on the Node class, but this causes a duplication of the
* attribute in the children nodes. This adapter is applied to children nodes to
* avoid the duplication by setting the field to null.
*/
public class RemoveDuplicateTypeAdapter extends XmlAdapter<Node, Node> {
@Override
public Node unmarshal(Node node) throws Exception {
return node;
}
@Override
public Node marshal(Node node) throws Exception {
node.removeType();
return node;
}
}
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
// //
package net.ivoa.xml.vospace.v2; package net.ivoa.xml.vospace.v2;
import it.inaf.oats.vospace.datamodel.RemoveDuplicateTypeAdapter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
...@@ -14,6 +15,7 @@ import javax.xml.bind.annotation.XmlElement; ...@@ -14,6 +15,7 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
/** /**
* *
...@@ -66,6 +68,7 @@ public class ContainerNode ...@@ -66,6 +68,7 @@ public class ContainerNode
// This should simplify usage and JSON compatibility. // This should simplify usage and JSON compatibility.
@XmlElement(name = "node") @XmlElement(name = "node")
@XmlElementWrapper(name = "nodes", required = true) @XmlElementWrapper(name = "nodes", required = true)
@XmlJavaTypeAdapter(RemoveDuplicateTypeAdapter.class)
protected List<Node> nodes; protected List<Node> nodes;
public List<Node> getNodes() { public List<Node> getNodes() {
......
...@@ -59,10 +59,23 @@ public class Node { ...@@ -59,10 +59,23 @@ public class Node {
protected PropertyList properties; protected PropertyList properties;
// <edit> // <edit>
// Used for generating missing type attribute for root node. For child nodes it is filled automatically.
@XmlAttribute(name = "type", namespace = "http://www.w3.org/2001/XMLSchema-instance", required = false)
private String type;
/* This method exists to fix the issue with type attribute. See RemoveDuplicateTypeAdapter class. */
public void removeType() {
this.type = null;
}
public Node() {
type = getType();
}
// Needed for handling inheritance in JSON (in XML, type attribute is automatically generated by JAXB). // Needed for handling inheritance in JSON (in XML, type attribute is automatically generated by JAXB).
@JsonProperty @JsonProperty
@XmlTransient @XmlTransient
public String getType() { public final String getType() {
return "vos:" + getClass().getSimpleName(); return "vos:" + getClass().getSimpleName();
} }
// </edit> // </edit>
......
...@@ -36,7 +36,7 @@ public class NodeTest { ...@@ -36,7 +36,7 @@ public class NodeTest {
Document doc = loadDocument(xml); Document doc = loadDocument(xml);
assertEquals("vos:node", doc.getDocumentElement().getNodeName()); assertEquals("vos:node", doc.getDocumentElement().getNodeName());
//assertEquals("vos:ContainerNode", doc.getDocumentElement().getAttribute("xsi:type")); assertEquals("vos:ContainerNode", doc.getDocumentElement().getAttribute("xsi:type"));
assertTrue(xml.contains("<vos:nodes>")); assertTrue(xml.contains("<vos:nodes>"));
assertTrue(xml.contains("xsi:type=\"vos:DataNode\"")); assertTrue(xml.contains("xsi:type=\"vos:DataNode\""));
...@@ -58,7 +58,7 @@ public class NodeTest { ...@@ -58,7 +58,7 @@ public class NodeTest {
String json = MAPPER.writeValueAsString(root); String json = MAPPER.writeValueAsString(root);
System.out.println(json); System.out.println(json);
assertThat(json, hasJsonPath("$.type", is("vos:ContainerNode"))); assertThat(json, hasJsonPath("$.type", is("vos:ContainerNode")));
assertThat(json, hasJsonPath("$.nodes[0].type", is("vos:DataNode"))); assertThat(json, hasJsonPath("$.nodes[0].type", is("vos:DataNode")));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment