blob: b2267fb59706f4b219c4ff5b1581a8d7d163af01 [file] [log] [blame]
Cowtowncoder1730a8f2015-01-08 13:28:03 -08001package com.fasterxml.jackson.databind.objectid;
Tatu Saloranta5d75ddc2012-11-23 09:56:40 -08002
3import com.fasterxml.jackson.annotation.*;
4
Tatu Saloranta2dac9662012-11-23 14:27:18 -08005import com.fasterxml.jackson.core.type.TypeReference;
Tatu Saloranta5d75ddc2012-11-23 09:56:40 -08006import com.fasterxml.jackson.databind.*;
7
8import java.util.*;
9
Tatu Saloranta2dac9662012-11-23 14:27:18 -080010public class TestAbstractWithObjectId extends BaseMapTest
Tatu Saloranta5d75ddc2012-11-23 09:56:40 -080011{
12 interface BaseInterface { }
13
14 @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
15 static class BaseInterfaceImpl implements BaseInterface {
16
17 @JsonProperty
18 private List<BaseInterfaceImpl> myInstances = new ArrayList<BaseInterfaceImpl>();
19
20 void addInstance(BaseInterfaceImpl instance) {
21 myInstances.add(instance);
22 }
23 }
24
Tatu Saloranta2dac9662012-11-23 14:27:18 -080025 static class ListWrapper<T extends BaseInterface> {
Tatu Saloranta5d75ddc2012-11-23 09:56:40 -080026
27 @JsonProperty
28 private List<T> myList = new ArrayList<T>();
29
30 void add(T t) {
31 myList.add(t);
32 }
33
34 int size() {
35 return myList.size();
36 }
37 }
38
39 public void testIssue877() throws Exception
40 {
41 // make two instances
42 BaseInterfaceImpl one = new BaseInterfaceImpl();
43 BaseInterfaceImpl two = new BaseInterfaceImpl();
44
45 // add them to each other's list to show identify info being used
46 one.addInstance(two);
47 two.addInstance(one);
48
49 // make a typed version of the list and add the 2 instances to it
Tatu Saloranta2dac9662012-11-23 14:27:18 -080050 ListWrapper<BaseInterfaceImpl> myList = new ListWrapper<BaseInterfaceImpl>();
Tatu Saloranta5d75ddc2012-11-23 09:56:40 -080051 myList.add(one);
52 myList.add(two);
53
54 // make an object mapper that will add class info in so deserialisation works
55 ObjectMapper om = new ObjectMapper();
56 om.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "@class");
57
58 // write and print the JSON
59 String json = om.writerWithDefaultPrettyPrinter().writeValueAsString(myList);
Tatu Saloranta2dac9662012-11-23 14:27:18 -080060 ListWrapper<BaseInterfaceImpl> result;
Tatu Saloranta5d75ddc2012-11-23 09:56:40 -080061
Tatu Saloranta2dac9662012-11-23 14:27:18 -080062 result = om.readValue(json, new TypeReference<ListWrapper<BaseInterfaceImpl>>() { });
Tatu Saloranta5d75ddc2012-11-23 09:56:40 -080063
64 assertNotNull(result);
65 // see what we get back
Tatu Saloranta213803d2013-06-06 17:34:53 -070066 assertEquals(2, result.size());
Tatu Saloranta5d75ddc2012-11-23 09:56:40 -080067 }
68}