FasterXML / FasterXML/jackson-modules-base

JaxbAnnotationModule Serializes Object as Ref only Instead of as Full Object In some Cases

Abierto
#46 6 comentarios 0 reacciones 0 asignados Ver en GitHub
active jaxb-annotations
Lenguaje dominante
Java
Estrellas
180
Forks
80
Merge medio
3 h 26 min
PR fusionados (30 d)
1

Descripción

When using Jackson with JaxbAnnotationModule , if objects with similar ids appears in two different fields of an outer object , it is sometimes incorrectly serialized as a ref only, instead of as a full object
Case 1:
The Inner object class has both @XMLID and @XMLElement properties
One of the inner objects appear in a list field of the outer object, and another in another field
Unit Test to show problem:

```
class HasID
{
String id;
String name;
@XmlID
@XmlElement
public String getId() {
return id;
}
@XmlElement
public String getName() { return name; }
}

@XmlRootElement
class HasIDList
{
List elements = null;
@XmlElement
public List getElements( ) { return elements; }
public void setElements( List elements ) { this.elements = elements; }
HasID parent;
@XmlElement
public HasID getParent() { return parent; }
}

public class SerializeAsFullObjectTest{

@Test
public void test() throws Exception {
ObjectMapper mapper = new ObjectMapper();
JaxbAnnotationModule jaxbAnnotationModule = new JaxbAnnotationModule();
mapper.registerModule(jaxbAnnotationModule);

HasID hasID = new HasID();
hasID.id = "1"; hasID.name="name1";
HasID hasID2 = new HasID();
hasID2.id = "1"; hasID2.name="name1";

HasIDList idList = new HasIDList();
idList.setElements(Arrays.asList(hasID,hasID2));
idList.parent = hasID2;

Assert.assertEquals("{\"elements\":[{\"id\":\"1\",\"name\":\"name1\"},{\"id\":\"1\",\"name\":\"name1\"}],\"parent\":{\"id\":\"1\",\"name\":\"name1\"}}", mapper.writeValueAsString(idList));
}
}

```
The problem only appears when
1) Using JaxbAnnotations, with @XmlID and ( @XmlElement or @XmlAttribute)
and
2A) The two inner objects have the same id, and appear in two different fields of the outer object, where one of them is a list
-OR-
2B) The same inner object is set as two different fields of the outer class
```

Unit Test for the second problem:

class HasID
{
String id;
String name;
@XmlID
@XmlElement
public String getId() {
return id;
}
@XmlElement
public String getName() { return name; }
}

@XmlRootElement
class OuterElement
{
HasID parent;
@XmlElement
public HasID getParent() { return parent; }
HasID second;
@XmlElement
public HasID getSecond() { return second; }

}

public class SameObjectSerializedFullTest {

@Test
public void test() throws Exception {
ObjectMapper mapper = new ObjectMapper();
JaxbAnnotationModule jaxbAnnotationModule = new JaxbAnnotationModule();
mapper.registerModule(jaxbAnnotationModule);
HasID hasID = new HasID();
hasID.id = "1"; hasID.name="name1";
HasID hasID2 = new HasID();
hasID2.id = "1"; hasID2.name="name1";
OuterElement outerElement = new OuterElement();
outerElement.parent = hasID;
outerElement.second = hasID;
Assert.assertEquals("{\"parent\":{\"id\":\"1\",\"name\":\"name1\"},\"second\":{\"id\":\"1\",\"name\":\"name1\"}}", mapper.writeValueAsString(outerElement));
}
```

```
This is a real ( non academic ) problem in a current production project.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.