|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 10 | +import com.fasterxml.jackson.databind.*; |
| 11 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test to reproduce [databind#1622]: Race condition in deserialization with |
| 17 | + * {@code @JsonIgnoreProperties} when deserializing child objects before parent |
| 18 | + * objects in cyclic references. |
| 19 | + */ |
| 20 | +public class JsonIgnoreProperties1622Test |
| 21 | + extends DatabindTestUtil |
| 22 | +{ |
| 23 | + // Classes for reproducing the issue |
| 24 | + static class Parent { |
| 25 | + private String name; |
| 26 | + |
| 27 | + @JsonIgnoreProperties("parent") |
| 28 | + private List<Child> children = new ArrayList<>(); |
| 29 | + |
| 30 | + public Parent() {} |
| 31 | + |
| 32 | + public Parent(String name) { |
| 33 | + this.name = name; |
| 34 | + } |
| 35 | + |
| 36 | + public String getName() { |
| 37 | + return name; |
| 38 | + } |
| 39 | + |
| 40 | + public void setName(String name) { |
| 41 | + this.name = name; |
| 42 | + } |
| 43 | + |
| 44 | + public List<Child> getChildren() { |
| 45 | + return children; |
| 46 | + } |
| 47 | + |
| 48 | + public void setChildren(List<Child> children) { |
| 49 | + this.children = children; |
| 50 | + } |
| 51 | + |
| 52 | + public void addChild(Child child) { |
| 53 | + children.add(child); |
| 54 | + child.setParent(this); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + static class Child { |
| 59 | + private String name; |
| 60 | + private Parent parent; |
| 61 | + |
| 62 | + public Child() {} |
| 63 | + |
| 64 | + public Child(String name) { |
| 65 | + this.name = name; |
| 66 | + } |
| 67 | + |
| 68 | + public String getName() { |
| 69 | + return name; |
| 70 | + } |
| 71 | + |
| 72 | + public void setName(String name) { |
| 73 | + this.name = name; |
| 74 | + } |
| 75 | + |
| 76 | + public Parent getParent() { |
| 77 | + return parent; |
| 78 | + } |
| 79 | + |
| 80 | + public void setParent(Parent parent) { |
| 81 | + this.parent = parent; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Variant with allowSetters workaround |
| 86 | + static class ParentWithWorkaround { |
| 87 | + private String name; |
| 88 | + |
| 89 | + @JsonIgnoreProperties(value = "parent", allowSetters = true) |
| 90 | + private List<ChildForWorkaround> children = new ArrayList<>(); |
| 91 | + |
| 92 | + public ParentWithWorkaround() {} |
| 93 | + |
| 94 | + public String getName() { |
| 95 | + return name; |
| 96 | + } |
| 97 | + |
| 98 | + public void setName(String name) { |
| 99 | + this.name = name; |
| 100 | + } |
| 101 | + |
| 102 | + public List<ChildForWorkaround> getChildren() { |
| 103 | + return children; |
| 104 | + } |
| 105 | + |
| 106 | + public void setChildren(List<ChildForWorkaround> children) { |
| 107 | + this.children = children; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + static class ChildForWorkaround { |
| 112 | + private String name; |
| 113 | + private ParentWithWorkaround parent; |
| 114 | + |
| 115 | + public ChildForWorkaround() {} |
| 116 | + |
| 117 | + public String getName() { |
| 118 | + return name; |
| 119 | + } |
| 120 | + |
| 121 | + public void setName(String name) { |
| 122 | + this.name = name; |
| 123 | + } |
| 124 | + |
| 125 | + public ParentWithWorkaround getParent() { |
| 126 | + return parent; |
| 127 | + } |
| 128 | + |
| 129 | + public void setParent(ParentWithWorkaround parent) { |
| 130 | + this.parent = parent; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 135 | + |
| 136 | + /** |
| 137 | + * This test demonstrates the race condition: deserializing a child first |
| 138 | + * causes the parent deserialization to fail with "No _valueDeserializer assigned" |
| 139 | + */ |
| 140 | + @Test |
| 141 | + public void raceConditionWithChildFirst() throws Exception |
| 142 | + { |
| 143 | + // First create and serialize the objects |
| 144 | + Parent parent = new Parent("Parent1"); |
| 145 | + Child child = new Child("Child1"); |
| 146 | + parent.addChild(child); |
| 147 | + |
| 148 | + String parentJson = MAPPER.writeValueAsString(parent); |
| 149 | + String childJson = MAPPER.writeValueAsString(child); |
| 150 | + |
| 151 | + // Deserialize child first - this triggers the race condition |
| 152 | + Child deserializedChild = MAPPER.readValue(childJson, Child.class); |
| 153 | + assertNotNull(deserializedChild); |
| 154 | + assertEquals("Child1", deserializedChild.getName()); |
| 155 | + |
| 156 | + // Now try to deserialize parent - this should fail with the race condition |
| 157 | + // Expected error: "No _valueDeserializer assigned" |
| 158 | + Parent deserializedParent = MAPPER.readValue(parentJson, Parent.class); |
| 159 | + assertNotNull(deserializedParent); |
| 160 | + assertEquals("Parent1", deserializedParent.getName()); |
| 161 | + assertEquals(1, deserializedParent.getChildren().size()); |
| 162 | + assertEquals("Child1", deserializedParent.getChildren().get(0).getName()); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Control test: deserializing parent first works fine |
| 167 | + */ |
| 168 | + @Test |
| 169 | + public void noRaceConditionWithParentFirst() throws Exception |
| 170 | + { |
| 171 | + Parent parent = new Parent("Parent1"); |
| 172 | + Child child = new Child("Child1"); |
| 173 | + parent.addChild(child); |
| 174 | + |
| 175 | + String parentJson = MAPPER.writeValueAsString(parent); |
| 176 | + |
| 177 | + // Deserialize parent first - this should work |
| 178 | + Parent deserializedParent = MAPPER.readValue(parentJson, Parent.class); |
| 179 | + assertNotNull(deserializedParent); |
| 180 | + assertEquals("Parent1", deserializedParent.getName()); |
| 181 | + assertEquals(1, deserializedParent.getChildren().size()); |
| 182 | + assertEquals("Child1", deserializedParent.getChildren().get(0).getName()); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Test that the workaround with allowSetters = true resolves the issue |
| 187 | + */ |
| 188 | + @Test |
| 189 | + public void workaroundWithAllowSetters() throws Exception |
| 190 | + { |
| 191 | + ParentWithWorkaround parent = new ParentWithWorkaround(); |
| 192 | + parent.setName("Parent1"); |
| 193 | + |
| 194 | + ChildForWorkaround child = new ChildForWorkaround(); |
| 195 | + child.setName("Child1"); |
| 196 | + child.setParent(parent); |
| 197 | + |
| 198 | + parent.setChildren(Arrays.asList(child)); |
| 199 | + |
| 200 | + String parentJson = MAPPER.writeValueAsString(parent); |
| 201 | + String childJson = MAPPER.writeValueAsString(child); |
| 202 | + |
| 203 | + // Deserialize child first |
| 204 | + ChildForWorkaround deserializedChild = MAPPER.readValue(childJson, ChildForWorkaround.class); |
| 205 | + assertNotNull(deserializedChild); |
| 206 | + assertEquals("Child1", deserializedChild.getName()); |
| 207 | + |
| 208 | + // Now deserialize parent - should work with allowSetters workaround |
| 209 | + ParentWithWorkaround deserializedParent = MAPPER.readValue(parentJson, ParentWithWorkaround.class); |
| 210 | + assertNotNull(deserializedParent); |
| 211 | + assertEquals("Parent1", deserializedParent.getName()); |
| 212 | + assertEquals(1, deserializedParent.getChildren().size()); |
| 213 | + assertEquals("Child1", deserializedParent.getChildren().get(0).getName()); |
| 214 | + } |
| 215 | +} |
0 commit comments