• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.failing;
2 
3 import java.io.StringWriter;
4 import java.util.ArrayList;
5 import java.util.List;
6 
7 import com.fasterxml.jackson.annotation.*;
8 import com.fasterxml.jackson.core.StreamWriteFeature;
9 import com.fasterxml.jackson.databind.BaseMapTest;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.json.JsonMapper;
12 
13 // Test case for https://github.com/FasterXML/jackson-databind/issues/1298
14 public class TestObjectIdWithUnwrapping1298 extends BaseMapTest
15 {
16     private static Long nextId = 1L;
17 
18     public static final class ListOfParents{
19         public List<Parent> parents = new ArrayList<>();
20 
addParent( Parent parent)21         public void addParent( Parent parent) { parents.add(parent);}
22     }
23 
24     @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Parent.class)
25     public static final class Parent {
26         public Long id;
27 
28         @JsonUnwrapped
29         public Child child;
Parent()30         public Parent() { this.id = nextId++;}
31     }
32 
33     @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Child.class)
34     public static final class Child
35     {
36         public Long id;
37 
38         public final String name;
39 
Child(@sonProperty"name") String name)40         public Child(@JsonProperty("name") String name) {
41             this.name = name;
42             this.id = TestObjectIdWithUnwrapping1298.nextId++;
43         }
44     }
45 
testObjectIdWithRepeatedChild()46     public void testObjectIdWithRepeatedChild() throws Exception
47     {
48         ObjectMapper mapper = JsonMapper.builder()
49                 // to keep output faithful to original, prevent auto-closing...
50                 .disable(StreamWriteFeature.AUTO_CLOSE_CONTENT)
51                 .build();
52 
53         // Equivalent to Spring _embedded for Bean w/ List property
54         ListOfParents parents = new ListOfParents();
55 
56         //Bean with Relationship
57         Parent parent1 = new Parent();
58         Child child1 = new Child("Child1");
59         parent1.child = child1;
60         parents.addParent(parent1);
61 
62         // serialize parent1 and parent2
63         String json = mapper
64                 .writerWithDefaultPrettyPrinter()
65                 .writeValueAsString(parents);
66         assertNotNull(json);
67 //        System.out.println("This works: " + json);
68 
69         // Add parent3 to create ObjectId reference
70         // Bean w/ repeated relationship from parent1, should generate ObjectId
71         Parent parent3 = new Parent();
72         parent3.child = child1;
73         parents.addParent(parent3);
74         StringWriter sw = new StringWriter();
75 
76         try {
77             mapper
78 //                .writerWithDefaultPrettyPrinter()
79                 .writeValue(sw, parents);
80         } catch (Exception e) {
81             fail("Failed with "+e.getClass().getName()+", output so far: " + sw);
82         }
83     }
84 }
85