1 package com.fasterxml.jackson.failing;
2 
3 import java.beans.ConstructorProperties;
4 
5 import com.fasterxml.jackson.annotation.*;
6 
7 import com.fasterxml.jackson.databind.BaseMapTest;
8 import com.fasterxml.jackson.databind.ObjectMapper;
9 
10 public class BackReference1516Test extends BaseMapTest
11 {
12     static class ParentWithCreator {
13         String id, name;
14 
15         @JsonManagedReference
16         ChildObject1 child;
17 
18         @ConstructorProperties({ "id", "name", "child" })
ParentWithCreator(String id, String name, ChildObject1 child)19         public ParentWithCreator(String id, String name,
20                 ChildObject1 child) {
21             this.id = id;
22             this.name = name;
23             this.child = child;
24         }
25     }
26 
27     static class ChildObject1
28     {
29         public String id, name;
30 
31         @JsonBackReference
32         public ParentWithCreator parent;
33 
34         @ConstructorProperties({ "id", "name", "parent" })
ChildObject1(String id, String name, ParentWithCreator parent)35         public ChildObject1(String id, String name,
36                 ParentWithCreator parent) {
37             this.id = id;
38             this.name = name;
39             this.parent = parent;
40         }
41     }
42 
43     static class ParentWithoutCreator {
44         public String id, name;
45 
46         @JsonManagedReference
47         public ChildObject2 child;
48     }
49 
50     static class ChildObject2
51     {
52         public String id, name;
53 
54         @JsonBackReference
55         public ParentWithoutCreator parent;
56 
57         @ConstructorProperties({ "id", "name", "parent" })
ChildObject2(String id, String name, ParentWithoutCreator parent)58         public ChildObject2(String id, String name,
59                 ParentWithoutCreator parent) {
60             this.id = id;
61             this.name = name;
62             this.parent = parent;
63         }
64     }
65 
66     /*
67     /**********************************************************
68     /* Unit tests
69     /**********************************************************
70      */
71 
72     private final ObjectMapper MAPPER = new ObjectMapper();
73 
74     private final String PARENT_CHILD_JSON = aposToQuotes(
75 "{ 'id': 'abc',\n"+
76 "  'name': 'Bob',\n"+
77 "  'child': { 'id': 'def', 'name':'Bert' }\n"+
78 "}");
79 
testWithParentCreator()80     public void testWithParentCreator() throws Exception
81     {
82         ParentWithCreator result = MAPPER.readValue(PARENT_CHILD_JSON,
83                 ParentWithCreator.class);
84         assertNotNull(result);
85         assertNotNull(result.child);
86         assertSame(result, result.child.parent);
87     }
88 
testWithParentNoCreator()89     public void testWithParentNoCreator() throws Exception
90     {
91         ParentWithoutCreator result = MAPPER.readValue(PARENT_CHILD_JSON,
92                 ParentWithoutCreator.class);
93         assertNotNull(result);
94         assertNotNull(result.child);
95         assertSame(result, result.child.parent);
96     }
97 }
98