Cowtowncoder | 1730a8f | 2015-01-08 13:28:03 -0800 | [diff] [blame] | 1 | package com.fasterxml.jackson.databind.objectid; |
Tatu Saloranta | 5d75ddc | 2012-11-23 09:56:40 -0800 | [diff] [blame] | 2 | |
| 3 | import com.fasterxml.jackson.annotation.*; |
| 4 | |
Tatu Saloranta | 2dac966 | 2012-11-23 14:27:18 -0800 | [diff] [blame] | 5 | import com.fasterxml.jackson.core.type.TypeReference; |
Tatu Saloranta | 5d75ddc | 2012-11-23 09:56:40 -0800 | [diff] [blame] | 6 | import com.fasterxml.jackson.databind.*; |
| 7 | |
| 8 | import java.util.*; |
| 9 | |
Tatu Saloranta | 2dac966 | 2012-11-23 14:27:18 -0800 | [diff] [blame] | 10 | public class TestAbstractWithObjectId extends BaseMapTest |
Tatu Saloranta | 5d75ddc | 2012-11-23 09:56:40 -0800 | [diff] [blame] | 11 | { |
| 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 Saloranta | 2dac966 | 2012-11-23 14:27:18 -0800 | [diff] [blame] | 25 | static class ListWrapper<T extends BaseInterface> { |
Tatu Saloranta | 5d75ddc | 2012-11-23 09:56:40 -0800 | [diff] [blame] | 26 | |
| 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 Saloranta | 2dac966 | 2012-11-23 14:27:18 -0800 | [diff] [blame] | 50 | ListWrapper<BaseInterfaceImpl> myList = new ListWrapper<BaseInterfaceImpl>(); |
Tatu Saloranta | 5d75ddc | 2012-11-23 09:56:40 -0800 | [diff] [blame] | 51 | 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 Saloranta | 2dac966 | 2012-11-23 14:27:18 -0800 | [diff] [blame] | 60 | ListWrapper<BaseInterfaceImpl> result; |
Tatu Saloranta | 5d75ddc | 2012-11-23 09:56:40 -0800 | [diff] [blame] | 61 | |
Tatu Saloranta | 2dac966 | 2012-11-23 14:27:18 -0800 | [diff] [blame] | 62 | result = om.readValue(json, new TypeReference<ListWrapper<BaseInterfaceImpl>>() { }); |
Tatu Saloranta | 5d75ddc | 2012-11-23 09:56:40 -0800 | [diff] [blame] | 63 | |
| 64 | assertNotNull(result); |
| 65 | // see what we get back |
Tatu Saloranta | 213803d | 2013-06-06 17:34:53 -0700 | [diff] [blame] | 66 | assertEquals(2, result.size()); |
Tatu Saloranta | 5d75ddc | 2012-11-23 09:56:40 -0800 | [diff] [blame] | 67 | } |
| 68 | } |