From 02c7345f48c2b5cf4a6eefb51efde37ffa19f403 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 15 Jun 2020 22:40:48 -0700 Subject: [PATCH] Add failing test for #2759 --- .../failing/JsonIdentityInfo2759Test.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/test/java/com/fasterxml/jackson/failing/JsonIdentityInfo2759Test.java diff --git a/src/test/java/com/fasterxml/jackson/failing/JsonIdentityInfo2759Test.java b/src/test/java/com/fasterxml/jackson/failing/JsonIdentityInfo2759Test.java new file mode 100644 index 0000000000..d948b0c2ad --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/failing/JsonIdentityInfo2759Test.java @@ -0,0 +1,72 @@ +package com.fasterxml.jackson.failing; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; + +import com.fasterxml.jackson.databind.*; + +public class JsonIdentityInfo2759Test extends BaseMapTest +{ + static class Bee { + public Long id; + + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") +/// @JsonIdentityReference(alwaysAsId = true) +// @JsonProperty("hiveId") + Hive hive; + + public Bee() { } + + public Bee(Long id, Hive hive) { + this.id = id; + this.hive = hive; + } + + public Hive getHive() { + return hive; + } + + public void setHive(Hive hive) { + this.hive = hive; + } + } + + static class Hive { + public String name; + public List bees = new ArrayList<>(); + + public Long id; + + Hive() { } + + public Hive(Long id, String name) { + this.id = id; + this.name = name; + } + + public void addBee(Bee bee) { + bees.add(bee); + } + } + + public void testObjectId2759() throws Exception + { + Hive hive = new Hive(100500L, "main hive"); + hive.addBee(new Bee(1L, hive)); + + ObjectMapper mapper = newJsonMapper(); + final String json = mapper.writerWithDefaultPrettyPrinter() + .writeValueAsString(hive); + + try { + mapper.readerFor(JsonNode.class) + .with(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY) + .readValue(json); + } catch (JsonMappingException e) { + fail("Should not have duplicates, but JSON content has: "+json); + } + } +}