|
| 1 | +package com.fasterxml.jackson.databind.jsontype.jdk; |
| 2 | + |
| 3 | +import static com.fasterxml.jackson.databind.BaseMapTest.jsonMapperBuilder; |
| 4 | +import static com.fasterxml.jackson.databind.BaseTest.a2q; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 10 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 11 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +/** |
| 17 | + * Unit test proving that below issue is fixed. |
| 18 | + * <p> |
| 19 | + * [databind#3133] Map deserialization results in different numeric classes based on json |
| 20 | + * ordering (BigDecimal / Double) when used in combination with @JsonSubTypes |
| 21 | + */ |
| 22 | +public class BigDecimalForFloatDisabled3133Test |
| 23 | +{ |
| 24 | + @JsonTypeInfo( |
| 25 | + use = JsonTypeInfo.Id.NAME, |
| 26 | + include = JsonTypeInfo.As.PROPERTY, |
| 27 | + property = "type") |
| 28 | + |
| 29 | + @JsonSubTypes({ |
| 30 | + @JsonSubTypes.Type(value = TestMapContainer3133.class, name = "MAP"), |
| 31 | + }) |
| 32 | + interface BaseType3133 { } |
| 33 | + |
| 34 | + static class TestMapContainer3133 implements BaseType3133 { |
| 35 | + |
| 36 | + private Map<String, ? extends Object> map = new HashMap<>(); |
| 37 | + |
| 38 | + public Map<String, ? extends Object> getMap() { |
| 39 | + return map; |
| 40 | + } |
| 41 | + |
| 42 | + public void setMap(Map<String, ? extends Object> map) { |
| 43 | + this.map = map; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private final ObjectMapper mapper = jsonMapperBuilder() |
| 48 | + .disable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 49 | + .build(); |
| 50 | + |
| 51 | + // [databind#3133] |
| 52 | + @Test |
| 53 | + public void testDeserializeWithDifferentOrdering3133() throws Exception |
| 54 | + { |
| 55 | + // case 1 : type first |
| 56 | + String ordering1 = a2q("{'type': 'MAP','map': { 'doubleValue': 0.1 }}"); |
| 57 | + TestMapContainer3133 model1 = mapper.readValue(ordering1, TestMapContainer3133.class); |
| 58 | + Assertions.assertTrue(model1.getMap().get("doubleValue") instanceof Double); |
| 59 | + |
| 60 | + // case 2 : value first |
| 61 | + String ordering2 = a2q("{'map': { 'doubleValue': 0.1 }, 'type': 'MAP'}"); |
| 62 | + TestMapContainer3133 model2 = mapper.readValue(ordering2, TestMapContainer3133.class); |
| 63 | + Assertions.assertTrue(model2.getMap().get("doubleValue") instanceof Double); |
| 64 | + } |
| 65 | +} |
0 commit comments