1 package com.fasterxml.jackson.failing;
2 
3 import com.fasterxml.jackson.annotation.*;
4 import com.fasterxml.jackson.databind.*;
5 
6 // This is probably impossible to handle, in general case, since
7 // there is a cycle for Parent2/Child2... unless special handling
8 // could be made to ensure that
9 public class TestObjectIdWithInjectables639 extends BaseMapTest
10 {
11         public static final class Context { }
12 
13         @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
14         public static final class Parent1 {
15             public Child1 child;
16 
Parent1()17             public Parent1() { }
18         }
19 
20         @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
21         public static final class Child1 {
22 
23             @JsonProperty
24             private final Parent1 parent;
25 
26             @JsonCreator
Child1(@sonProperty"parent") Parent1 parent)27             public Child1(@JsonProperty("parent") Parent1 parent) {
28                 this.parent = parent;
29             }
30         }
31 
32         @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
33         public static final class Parent2 {
34 
35             protected final Context context;
36 
37             public Child2 child;
38 
39             @JsonCreator
Parent2(@acksonInject Context context)40             public Parent2(@JacksonInject Context context) {
41                 this.context = context;
42             }
43         }
44 
45         @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
46         public static final class Child2 {
47 
48             protected final Context context;
49 
50             @JsonProperty
51             protected Parent2 parent;
52 
53             @JsonCreator
Child2(@acksonInject Context context, @JsonProperty("parent") Parent2 parent)54             public Child2(@JacksonInject Context context,
55                     @JsonProperty("parent") Parent2 parent) {
56                 this.context = context;
57                 this.parent = parent;
58             }
59         }
60 
testObjectIdWithInjectables()61         public void testObjectIdWithInjectables() throws Exception
62         {
63             ObjectMapper mapper = new ObjectMapper();
64             Context context = new Context();
65             InjectableValues iv = new InjectableValues.Std().
66                     addValue(Context.class, context);
67             mapper.setInjectableValues(iv);
68 
69             Parent1 parent1 = new Parent1();
70             Child1 child1 = new Child1(parent1);
71             parent1.child = child1;
72 
73             Parent2 parent2 = new Parent2(context);
74             Child2 child2 = new Child2(context, parent2);
75             parent2.child = child2;
76 
77             String json = mapper.writeValueAsString(parent1);
78             parent1 = mapper.readValue(json, Parent1.class);
79 //            System.out.println("This works: " + json);
80 
81             json = mapper.writeValueAsString(parent2);
82 //System.out.println("This fails: " + json);
83             parent2 = mapper.readValue(json, Parent2.class);
84         }
85 }
86