11package tools .jackson .databind .tofix ;
22
3+ import java .util .List ;
34import java .util .concurrent .ConcurrentHashMap ;
45
56import org .junit .jupiter .api .Test ;
1314import tools .jackson .databind .testutil .DatabindTestUtil ;
1415import tools .jackson .databind .testutil .failure .JacksonTestFailureExpected ;
1516
16- import static org .junit .jupiter .api .Assertions .assertNotNull ;
17+ import static org .junit .jupiter .api .Assertions .* ;
1718
1819class ObjectIdWithBuilder1496Test extends DatabindTestUtil {
1920 @ JsonIdentityInfo (generator = ObjectIdGenerators .PropertyGenerator .class , property = "id" )
@@ -79,8 +80,54 @@ public POJO readFromCacheOrBuild() {
7980 }
8081 }
8182
83+ @ JsonIdentityInfo (generator = ObjectIdGenerators .PropertyGenerator .class , property = "id" )
84+ @ JsonDeserialize (builder = EntityBuilder .class )
85+ static class Entity
86+ {
87+ private long id ;
88+ private Entity ref ;
89+ private List <Entity > refs ;
90+
91+ Entity (long id , Entity ref , List <Entity > refs ) {
92+ this .id = id ;
93+ this .ref = ref ;
94+ this .refs = refs ;
95+ }
96+
97+ public long getId () { return id ; }
98+ public Entity getRef () { return ref ; }
99+ public List <Entity > getRefs () { return refs ; }
100+ }
101+
102+ @ JsonPOJOBuilder (withPrefix = "" )
103+ static class EntityBuilder
104+ {
105+ private long id ;
106+ private Entity ref ;
107+ private List <Entity > refs ;
108+
109+ public EntityBuilder id (long id ) { this .id = id ; return this ; }
110+ public EntityBuilder ref (Entity ref ) { this .ref = ref ; return this ; }
111+ public EntityBuilder refs (List <Entity > refs ) { this .refs = refs ; return this ; }
112+
113+ public Entity build () {
114+ return new Entity (id , ref , refs );
115+ }
116+ }
117+
118+ static class EntityContainer
119+ {
120+ public List <Entity > entities ;
121+ }
122+
82123 private final ObjectMapper MAPPER = newJsonMapper ();
83124
125+ /*
126+ /**********************************************************************
127+ /* Test cases
128+ /**********************************************************************
129+ */
130+
84131 @ JacksonTestFailureExpected
85132 @ Test
86133 void builderId1496 () throws Exception {
@@ -89,4 +136,58 @@ void builderId1496() throws Exception {
89136 POJO result = MAPPER .readValue (json , POJO .class );
90137 assertNotNull (result );
91138 }
139+
140+ // Forward reference: first entity references second entity (which comes later)
141+ @ JacksonTestFailureExpected
142+ @ Test
143+ public void testForwardReference () throws Exception
144+ {
145+ String json = a2q ("{'entities':["
146+ + "{'id':1,'ref':2,'refs':[]},"
147+ + "{'id':2,'refs':[1]}"
148+ + "]}" );
149+
150+ EntityContainer container = MAPPER .readValue (json , EntityContainer .class );
151+ assertNotNull (container );
152+ assertEquals (2 , container .entities .size ());
153+
154+ Entity first = container .entities .get (0 );
155+ Entity second = container .entities .get (1 );
156+
157+ assertEquals (1 , first .getId ());
158+ assertEquals (2 , second .getId ());
159+
160+ // first.ref -> second (forward reference)
161+ assertSame (second , first .getRef ());
162+ // second.refs[0] -> first (back reference in collection)
163+ assertEquals (1 , second .getRefs ().size ());
164+ assertSame (first , second .getRefs ().get (0 ));
165+ }
166+
167+ // Back reference: second entity references first entity (which came earlier)
168+ @ JacksonTestFailureExpected
169+ @ Test
170+ public void testBackReference () throws Exception
171+ {
172+ String json = a2q ("{'entities':["
173+ + "{'id':1,'refs':[2]},"
174+ + "{'id':2,'ref':1,'refs':[]}"
175+ + "]}" );
176+
177+ EntityContainer container = MAPPER .readValue (json , EntityContainer .class );
178+ assertNotNull (container );
179+ assertEquals (2 , container .entities .size ());
180+
181+ Entity first = container .entities .get (0 );
182+ Entity second = container .entities .get (1 );
183+
184+ assertEquals (1 , first .getId ());
185+ assertEquals (2 , second .getId ());
186+
187+ // second.ref -> first (back reference)
188+ assertSame (first , second .getRef ());
189+ // first.refs[0] -> second (forward reference in collection)
190+ assertEquals (1 , first .getRefs ().size ());
191+ assertSame (second , first .getRefs ().get (0 ));
192+ }
92193}
0 commit comments