11package com .fasterxml .jackson .dataformat .xml .types ;
22
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ import com .fasterxml .jackson .annotation .JsonIdentityInfo ;
37import com .fasterxml .jackson .annotation .JsonTypeInfo ;
8+ import com .fasterxml .jackson .annotation .ObjectIdGenerators ;
49import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
510import com .fasterxml .jackson .dataformat .xml .XmlTestBase ;
611
@@ -22,9 +27,6 @@ public SubTypeWithClassProperty() { }
2227 public SubTypeWithClassProperty (String s ) { name = s ; }
2328 }
2429
25- @ JsonTypeInfo (use =JsonTypeInfo .Id .CLASS , include =JsonTypeInfo .As .WRAPPER_ARRAY )
26- static class BaseTypeWithClassArray { }
27-
2830 @ JsonTypeInfo (use =JsonTypeInfo .Id .CLASS , include =JsonTypeInfo .As .WRAPPER_OBJECT )
2931 protected static class BaseTypeWithClassObject { }
3032
@@ -35,6 +37,22 @@ public SubTypeWithClassObject() { }
3537 public SubTypeWithClassObject (String s ) { name = s ; }
3638 }
3739
40+ @ JsonTypeInfo (use = JsonTypeInfo .Id .CLASS , include = JsonTypeInfo .As .PROPERTY )
41+ @ JsonIdentityInfo (generator = ObjectIdGenerators .PropertyGenerator .class , property = "id" )
42+ protected static class TypeWithClassPropertyAndObjectId {
43+ public String id ;
44+
45+ public TypeWithClassPropertyAndObjectId () {}
46+ public TypeWithClassPropertyAndObjectId (String id ) { this .id = id ; }
47+ }
48+
49+ protected static class Wrapper {
50+ public List <TypeWithClassPropertyAndObjectId > data ;
51+
52+ public Wrapper (){}
53+ public Wrapper (List <TypeWithClassPropertyAndObjectId > data ) { this .data = data ; }
54+ }
55+
3856 /*
3957 /**********************************************************
4058 /* Set up
@@ -85,5 +103,22 @@ public void testAsClassObject() throws Exception
85103 assertEquals (SubTypeWithClassObject .class , result .getClass ());
86104 assertEquals ("Foobar" , ((SubTypeWithClassObject ) result ).name );
87105 }
106+
107+ /**
108+ * Test for issue 81
109+ */
110+ public void testAsPropertyWithObjectId () throws Exception
111+ {
112+ List <TypeWithClassPropertyAndObjectId > data = new ArrayList <TestPolymorphic .TypeWithClassPropertyAndObjectId >();
113+ TypeWithClassPropertyAndObjectId object = new TypeWithClassPropertyAndObjectId ("Foobar" );
114+ data .add (object );
115+ // This will be written as an id reference instead of object; as such, no type info will be written.
116+ data .add (object );
117+ String xml = _xmlMapper .writeValueAsString (new Wrapper (data ));
118+ Wrapper result = _xmlMapper .readValue (xml , Wrapper .class );
119+ assertNotNull (result );
120+ assertSame (result .data .get (0 ), result .data .get (1 ));
121+ assertEquals ("Foobar" , result .data .get (0 ).id );
122+ }
88123}
89124
0 commit comments