FasterXML / FasterXML/jackson-modules-base
JaxbAnnotationModule Serializes Object as Ref only Instead of as Full Object In some Cases
- 主要语言
- Java
- 星标
- 180
- 派生
- 80
- 平均合并
- 3 小时 26 分钟
- 30 天内合并 PR
- 1
描述
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.
贡献指南
这个仓库没有索引到贡献指南
评估
这个 Issue 还没有评估数据。