1 package com.fasterxml.jackson.databind.objectid; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.fasterxml.jackson.annotation.JsonIdentityInfo; 7 import com.fasterxml.jackson.annotation.JsonIdentityReference; 8 import com.fasterxml.jackson.annotation.JsonProperty; 9 import com.fasterxml.jackson.annotation.ObjectIdGenerators; 10 11 import com.fasterxml.jackson.databind.*; 12 13 public class ObjectId2759Test extends BaseMapTest 14 { 15 static class Hive { 16 public String name; 17 public List<Bee> bees = new ArrayList<>(); 18 19 public Long id; 20 Hive()21 Hive() { } 22 Hive(Long id, String name)23 public Hive(Long id, String name) { 24 this.id = id; 25 this.name = name; 26 } 27 addBee(Bee bee)28 public void addBee(Bee bee) { 29 bees.add(bee); 30 } 31 } 32 33 static class Bee { 34 public Long id; 35 36 @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") 37 @JsonIdentityReference(alwaysAsId = true) 38 @JsonProperty("hiveId") 39 Hive hive; 40 Bee()41 public Bee() { } 42 Bee(Long id, Hive hive)43 public Bee(Long id, Hive hive) { 44 this.id = id; 45 this.hive = hive; 46 } 47 getHive()48 public Hive getHive() { 49 return hive; 50 } 51 setHive(Hive hive)52 public void setHive(Hive hive) { 53 this.hive = hive; 54 } 55 } 56 testObjectId2759()57 public void testObjectId2759() throws Exception 58 { 59 Hive hive = new Hive(100500L, "main hive"); 60 hive.addBee(new Bee(1L, hive)); 61 62 ObjectMapper mapper = newJsonMapper(); 63 final String json = mapper.writerWithDefaultPrettyPrinter() 64 .writeValueAsString(hive); 65 try { 66 mapper.readerFor(JsonNode.class) 67 .with(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY) 68 .readValue(json); 69 } catch (JsonMappingException e) { 70 fail("Should not have duplicates, but JSON content has: "+json); 71 } 72 } 73 } 74