|
| 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.JsonGenerator; |
| 9 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | + |
| 12 | +// Test case for https://github.com/FasterXML/jackson-databind/issues/1298 |
| 13 | +public class TestObjectIdWithUnwrapping1298 extends BaseMapTest |
| 14 | +{ |
| 15 | + private static Long nextId = 1L; |
| 16 | + |
| 17 | + public static final class ListOfParents{ |
| 18 | + @JsonUnwrapped |
| 19 | + public List<Parent> parents = new ArrayList<>(); |
| 20 | + |
| 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; |
| 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 | + @JsonProperty |
| 39 | + private final String name; |
| 40 | + |
| 41 | + public Child(@JsonProperty("name") String name) { |
| 42 | + this.name = name; |
| 43 | + this.id = TestObjectIdWithUnwrapping1298.nextId++; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public void testObjectIdWithRepeatedChild() throws Exception |
| 48 | + { |
| 49 | + ObjectMapper mapper = new ObjectMapper(); |
| 50 | + mapper.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT); |
| 51 | + |
| 52 | + // Equivalent to Spring _embedded for Bean w/ List property |
| 53 | + ListOfParents parents = new ListOfParents(); |
| 54 | + |
| 55 | + //Bean with Relationship |
| 56 | + Parent parent1 = new Parent(); |
| 57 | + Child child1 = new Child("Child1"); |
| 58 | + parent1.child = child1; |
| 59 | + parents.addParent(parent1); |
| 60 | + |
| 61 | + // serialize parent1 and parent2 |
| 62 | + String json = mapper |
| 63 | +// .writerWithDefaultPrettyPrinter() |
| 64 | + .writeValueAsString(parents); |
| 65 | + System.out.println("This works: " + json); |
| 66 | + |
| 67 | + // Add parent3 to create ObjectId reference |
| 68 | + // Bean w/ repeated relationship from parent1, should generate ObjectId |
| 69 | + Parent parent3 = new Parent(); |
| 70 | + parent3.child = child1; |
| 71 | + parents.addParent(parent3); |
| 72 | + StringWriter sw = new StringWriter(); |
| 73 | + |
| 74 | + try { |
| 75 | + mapper |
| 76 | +// .writerWithDefaultPrettyPrinter() |
| 77 | + .writeValue(sw, parents); |
| 78 | + } catch (Exception e) { |
| 79 | + System.out.println("Failed output so far: " + json); |
| 80 | + throw e; |
| 81 | + } |
| 82 | + |
| 83 | +// System.out.println("Also works: " + sw); |
| 84 | + } |
| 85 | +} |
0 commit comments