Skip to content

Commit 556a5bb

Browse files
authored
Add now-passing test for #3133 (#4231)
1 parent 3035c34 commit 556a5bb

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public static ObjectMapper newJsonMapper() {
244244
}
245245

246246
// @since 2.10
247-
protected static JsonMapper.Builder jsonMapperBuilder() {
247+
public static JsonMapper.Builder jsonMapperBuilder() {
248248
return JsonMapper.builder();
249249
}
250250

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.fasterxml.jackson.databind.deser.jdk;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import com.fasterxml.jackson.annotation.JsonSubTypes;
7+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
8+
import com.fasterxml.jackson.databind.DeserializationFeature;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import org.junit.jupiter.api.Assertions;
11+
import org.junit.jupiter.api.Test;
12+
13+
import static com.fasterxml.jackson.databind.BaseMapTest.jsonMapperBuilder;
14+
import static com.fasterxml.jackson.databind.BaseTest.a2q;
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 = TestMapContainer.class, name = "MAP"),
31+
})
32+
interface TestJsonTypeInfoInterface { }
33+
34+
static class TestMapContainer implements TestJsonTypeInfoInterface {
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+
@Test
52+
public void testDeserializeWithDifferentOrdering() throws Exception {
53+
// case 1 : type first
54+
String ordering1 = a2q("{'type': 'MAP','map': { 'doubleValue': 0.1 }}");
55+
TestMapContainer model1 = mapper.readValue(ordering1, TestMapContainer.class);
56+
Assertions.assertTrue(model1.getMap().get("doubleValue") instanceof Double);
57+
58+
// case 2 : value first
59+
String ordering2 = a2q("{'map': { 'doubleValue': 0.1 }, 'type': 'MAP'}");
60+
TestMapContainer model2 = mapper.readValue(ordering2, TestMapContainer.class);
61+
Assertions.assertTrue(model2.getMap().get("doubleValue") instanceof Double);
62+
}
63+
}

0 commit comments

Comments
 (0)