|
1 | 1 | package tools.jackson.databind.objectid; |
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
| 4 | +import java.util.List; |
4 | 5 |
|
5 | 6 | import org.junit.jupiter.api.Test; |
6 | 7 |
|
@@ -124,6 +125,32 @@ static class V extends AbstractData { |
124 | 125 | private static final long serialVersionUID = 1L; |
125 | 126 | } |
126 | 127 |
|
| 128 | + // // // [databind#2780]: Default Typing + @JsonIdentityInfo in untyped collections |
| 129 | + |
| 130 | + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") |
| 131 | + static class User2780 { |
| 132 | + public int id; |
| 133 | + public String login; |
| 134 | + |
| 135 | + public User2780() {} |
| 136 | + public User2780(int id, String login) { |
| 137 | + this.id = id; |
| 138 | + this.login = login; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") |
| 143 | + static class UserContainer2780 { |
| 144 | + public int id; |
| 145 | + public User2780 user; |
| 146 | + |
| 147 | + public UserContainer2780() {} |
| 148 | + public UserContainer2780(int id, User2780 user) { |
| 149 | + this.id = id; |
| 150 | + this.user = user; |
| 151 | + } |
| 152 | + } |
| 153 | + |
127 | 154 | /* |
128 | 155 | /********************************************************** |
129 | 156 | /* Unit tests, simple hierarchy [databind#825] |
@@ -224,4 +251,74 @@ public void testFull825() throws Exception |
224 | 251 | CTC result = mapper.readValue(INPUT, CTC.class); |
225 | 252 | assertNotNull(result); |
226 | 253 | } |
| 254 | + |
| 255 | + /* |
| 256 | + /********************************************************************** |
| 257 | + /* Unit tests, default typing + untyped collections [databind#2780] |
| 258 | + /********************************************************************** |
| 259 | + */ |
| 260 | + |
| 261 | + // User appears first: back-reference resolves correctly |
| 262 | + @Test |
| 263 | + public void testUserFirstThenContainer2780() throws Exception |
| 264 | + { |
| 265 | + ObjectMapper mapper = jsonMapperBuilder() |
| 266 | + .activateDefaultTyping(NoCheckSubTypeValidator.instance, DefaultTyping.NON_FINAL) |
| 267 | + .build(); |
| 268 | + |
| 269 | + User2780 user = new User2780(42, "cool_man"); |
| 270 | + UserContainer2780 container = new UserContainer2780(1, user); |
| 271 | + |
| 272 | + List<Object> list = new ArrayList<>(); |
| 273 | + list.add(user); |
| 274 | + list.add(container); |
| 275 | + |
| 276 | + String json = mapper.writeValueAsString(list); |
| 277 | + List<?> result = mapper.readValue(json, List.class); |
| 278 | + |
| 279 | + assertEquals(2, result.size()); |
| 280 | + assertInstanceOf(User2780.class, result.get(0), "First element should be User2780"); |
| 281 | + assertInstanceOf(UserContainer2780.class, result.get(1), "Second element should be UserContainer2780"); |
| 282 | + |
| 283 | + User2780 resultUser = (User2780) result.get(0); |
| 284 | + UserContainer2780 resultContainer = (UserContainer2780) result.get(1); |
| 285 | + |
| 286 | + assertEquals(42, resultUser.id); |
| 287 | + assertEquals("cool_man", resultUser.login); |
| 288 | + assertSame(resultUser, resultContainer.user, |
| 289 | + "Back-reference in container should point to the same User2780 instance"); |
| 290 | + } |
| 291 | + |
| 292 | + // [databind#2780]: Container appears first, bare id back-reference must resolve with type info |
| 293 | + @Test |
| 294 | + public void testContainerFirstThenUser2780() throws Exception |
| 295 | + { |
| 296 | + ObjectMapper mapper = jsonMapperBuilder() |
| 297 | + .activateDefaultTyping(NoCheckSubTypeValidator.instance, DefaultTyping.NON_FINAL) |
| 298 | + .build(); |
| 299 | + |
| 300 | + User2780 user = new User2780(42, "cool_man"); |
| 301 | + UserContainer2780 container = new UserContainer2780(1, user); |
| 302 | + |
| 303 | + List<Object> list = new ArrayList<>(); |
| 304 | + list.add(container); |
| 305 | + list.add(user); |
| 306 | + |
| 307 | + String json = mapper.writeValueAsString(list); |
| 308 | + List<?> result = mapper.readValue(json, List.class); |
| 309 | + |
| 310 | + assertEquals(2, result.size()); |
| 311 | + assertInstanceOf(UserContainer2780.class, result.get(0), |
| 312 | + "First element should be UserContainer2780"); |
| 313 | + assertInstanceOf(User2780.class, result.get(1), |
| 314 | + "Second element should be User2780 (not Integer) -- bare id back-reference must be resolved with type info"); |
| 315 | + |
| 316 | + UserContainer2780 resultContainer = (UserContainer2780) result.get(0); |
| 317 | + User2780 resultUser = (User2780) result.get(1); |
| 318 | + |
| 319 | + assertEquals(42, resultUser.id); |
| 320 | + assertEquals("cool_man", resultUser.login); |
| 321 | + assertSame(resultUser, resultContainer.user, |
| 322 | + "Container's user field and the list's second element should be the same instance"); |
| 323 | + } |
227 | 324 | } |
0 commit comments