|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.*; |
| 6 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | +import com.fasterxml.jackson.databind.*; |
| 8 | + |
| 9 | +public class JsonIdentiyInfo1388Test extends BaseMapTest |
| 10 | +{ |
| 11 | + @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class) |
| 12 | + public static class NamedThing { |
| 13 | + private final UUID id; |
| 14 | + private final String name; |
| 15 | + |
| 16 | + @JsonCreator |
| 17 | + public NamedThing(@JsonProperty("id") UUID id, @JsonProperty("name") String name) { |
| 18 | + this.id = id; |
| 19 | + this.name = name; |
| 20 | + } |
| 21 | + |
| 22 | + public UUID getId() { |
| 23 | + return id; |
| 24 | + } |
| 25 | + |
| 26 | + public String getName() { |
| 27 | + return name; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public boolean equals(Object o) { |
| 32 | + if (this == o) return true; |
| 33 | + if (o == null || getClass() != o.getClass()) return false; |
| 34 | + NamedThing that = (NamedThing) o; |
| 35 | + return that.id.equals(id) && that.name.equals(name); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public int hashCode() { |
| 40 | + return name.hashCode(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public void testDeserializationFinalClassJSOG() throws Exception |
| 46 | + { |
| 47 | + final ObjectMapper mapper = new ObjectMapper(); |
| 48 | + final UUID id = UUID.fromString("a59aa02c-fe3c-43f8-9b5a-5fe01878a818"); |
| 49 | + final NamedThing thing = new NamedThing(id, "Hello"); |
| 50 | + |
| 51 | + final TypeReference<?> namedThingListType = new TypeReference<List<NamedThing>>() { }; |
| 52 | + |
| 53 | + final String jsog = mapper.writeValueAsString(Arrays.asList(thing, thing, thing)); |
| 54 | + { |
| 55 | + final List<NamedThing> list = mapper.readValue(jsog, namedThingListType); |
| 56 | + _assertAllSame(list); |
| 57 | + } |
| 58 | + |
| 59 | + // this is the jsog representation of the list of 3 of the same item |
| 60 | + assertTrue(jsog.equals("[{\"@id\":1,\"id\":\"a59aa02c-fe3c-43f8-9b5a-5fe01878a818\",\"name\":\"Hello\"},1,1]")); |
| 61 | + |
| 62 | + // now move it around it have forward references |
| 63 | + final String forwardReferences = "[1,1,{\"@id\":1,\"id\":\"a59aa02c-fe3c-43f8-9b5a-5fe01878a818\",\"name\":\"Hello\"}]"; |
| 64 | + // this works |
| 65 | + { |
| 66 | + final List<NamedThing> forward = mapper.readValue(forwardReferences, namedThingListType); |
| 67 | + _assertAllSame(forward); |
| 68 | + } |
| 69 | + |
| 70 | + // now move the @id to be not the first key in the object |
| 71 | + final String notFirstKey = "[1,1,{\"id\":\"a59aa02c-fe3c-43f8-9b5a-5fe01878a818\",\"name\":\"Hello\",\"@id\":1}]"; |
| 72 | + // this fails |
| 73 | + { |
| 74 | + final List<NamedThing> forward = mapper.readValue(notFirstKey, namedThingListType); |
| 75 | + _assertAllSame(forward); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private void _assertAllSame(List<?> entries) { |
| 80 | + Object first = entries.get(0); |
| 81 | + for (int i = 0, end = entries.size(); i < end; ++i) { |
| 82 | + if (first != entries.get(i)) { |
| 83 | + fail("Mismatch: entry #"+i+" not same as #0"); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments