|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 7 | +import com.fasterxml.jackson.core.JsonParser; |
| 8 | +import com.fasterxml.jackson.databind.*; |
| 9 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 10 | + |
| 11 | +public class NoTypeInfo1654Test extends BaseMapTest |
| 12 | +{ |
| 13 | + // [databind#1654] |
| 14 | + |
| 15 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME) |
| 16 | + static class Value1654 { |
| 17 | + public int x; |
| 18 | + |
| 19 | + protected Value1654() { } |
| 20 | + public Value1654(int x) { this.x = x; } |
| 21 | + } |
| 22 | + |
| 23 | + static class Value1654TypedContainer { |
| 24 | + public List<Value1654> values; |
| 25 | + |
| 26 | + protected Value1654TypedContainer() { } |
| 27 | + public Value1654TypedContainer(Value1654... v) { |
| 28 | + values = Arrays.asList(v); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + static class Value1654UntypedContainer { |
| 33 | + @JsonDeserialize(contentUsing = Value1654Deserializer.class) |
| 34 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NONE) |
| 35 | + public List<Value1654> values; |
| 36 | + } |
| 37 | + |
| 38 | + static class Value1654Deserializer extends JsonDeserializer<Value1654> { |
| 39 | + @Override |
| 40 | + public Value1654 deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { |
| 41 | + p.skipChildren(); |
| 42 | + return new Value1654(13); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /* |
| 47 | + /********************************************************** |
| 48 | + /* Test methods |
| 49 | + /********************************************************** |
| 50 | + */ |
| 51 | + |
| 52 | + // [databind#1654] |
| 53 | + public void testNoTypeElementOverride() throws Exception |
| 54 | + { |
| 55 | + final ObjectMapper mapper = newObjectMapper(); |
| 56 | + |
| 57 | + // First: regular typed case |
| 58 | + String json = mapper.writeValueAsString(new Value1654TypedContainer( |
| 59 | + new Value1654(1), |
| 60 | + new Value1654(2), |
| 61 | + new Value1654(3) |
| 62 | + )); |
| 63 | + Value1654TypedContainer result = mapper.readValue(json, Value1654TypedContainer.class); |
| 64 | + assertEquals(3, result.values.size()); |
| 65 | + assertEquals(2, result.values.get(1).x); |
| 66 | + |
| 67 | + // and then actual failing case |
| 68 | + final String noTypeJson = aposToQuotes( |
| 69 | + "{'values':[{'x':3},{'x': 7}] }" |
| 70 | + ); |
| 71 | + Value1654UntypedContainer unResult = mapper.readValue(noTypeJson, Value1654UntypedContainer.class); |
| 72 | + assertEquals(2, unResult.values.size()); |
| 73 | + assertEquals(7, unResult.values.get(1).x); |
| 74 | + } |
| 75 | +} |
0 commit comments