FasterXML / FasterXML/jackson-modules-base

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

オープン
#46 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る
active jaxb-annotations
主要言語
Java
スター
180
フォーク
80
平均マージ
3時間 26分
マージ済み PR(30日)
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 はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。