Skip to content

Commit 00f5131

Browse files
committed
Adding reproduction test case for #1298
1 parent 06c513f commit 00f5131

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
import com.fasterxml.jackson.core.JsonEncoding;
5+
import com.fasterxml.jackson.core.json.UTF8JsonGenerator;
6+
import com.fasterxml.jackson.databind.BaseMapTest;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.ObjectWriter;
9+
10+
import java.io.ByteArrayOutputStream;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
// Test case for https://github.com/FasterXML/jackson-databind/issues/1298
15+
public class TestObjectIdWithUnwrappingBeanPropertyWriter extends BaseMapTest
16+
{
17+
18+
private static Long id = 1L;
19+
20+
public static final class ListOfParents{
21+
@JsonUnwrapped
22+
public List<Parent> parents = new ArrayList<>();
23+
24+
public void addParent( Parent parent) { parents.add(parent);}
25+
}
26+
27+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Parent.class)
28+
public static final class Parent {
29+
30+
public Long id;
31+
32+
@JsonUnwrapped
33+
public Child child;
34+
public Parent() { this.id = TestObjectIdWithUnwrappingBeanPropertyWriter.id++;}
35+
36+
}
37+
38+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Child.class)
39+
public static final class Child {
40+
41+
public Long id;
42+
43+
@JsonProperty
44+
private final String name;
45+
46+
public Child(@JsonProperty("name") String name) {
47+
this.name = name;
48+
this.id = TestObjectIdWithUnwrappingBeanPropertyWriter.id++;
49+
}
50+
}
51+
52+
public void testObjectIdWithRepeatedChild() throws Exception
53+
{
54+
ObjectMapper mapper = new ObjectMapper();
55+
ObjectWriter writer = mapper.writer();
56+
57+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
58+
59+
UTF8JsonGenerator generator = (UTF8JsonGenerator) mapper.getFactory().createGenerator(outputStream, JsonEncoding.UTF8);
60+
61+
// Equivalent to Spring _embedded for Bean w/ List property
62+
ListOfParents parents = new ListOfParents();
63+
64+
//Bean with Relationship
65+
Parent parent1 = new Parent();
66+
Child child1 = new Child("Child1");
67+
parent1.child = child1;
68+
69+
// Bean with Relationship
70+
Parent parent2 = new Parent();
71+
Child child2 = new Child( "Child2");
72+
parent2.child = child2;
73+
74+
// Bean w/ repeated relationship from parent1, should generate ObjectId
75+
Parent parent3 = new Parent();
76+
parent3.child = child1;
77+
78+
79+
parents.addParent(parent1);
80+
parents.addParent(parent2);
81+
82+
String json;
83+
// serialize parent1 and parent2
84+
writer.writeValue(generator, parents);
85+
json = outputStream.toString();
86+
outputStream.reset();
87+
System.out.println("This works: " + json);
88+
89+
// Add parent3 to create ObjectId reference
90+
parents.addParent(parent3);
91+
92+
writer.writeValue(generator, parents);
93+
json = outputStream.toString();
94+
System.out.println("This fails: " + json);
95+
}
96+
}

0 commit comments

Comments
 (0)