Skip to content

Commit 32a1c96

Browse files
committed
Add failing test for #2573
1 parent b53fa57 commit 32a1c96

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.annotation.JsonInclude;
6+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
7+
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.json.JsonMapper;
9+
10+
public class MapInclusion2573Test extends BaseMapTest
11+
{
12+
@JsonPropertyOrder({ "model", "properties" })
13+
static class Car
14+
{
15+
public String model;
16+
public Map<String, Integer> properties;
17+
}
18+
19+
/*
20+
/**********************************************************
21+
/* Test methods
22+
/**********************************************************
23+
*/
24+
25+
private final Map<String, Integer> CAR_PROPERTIES = new LinkedHashMap<>();
26+
{
27+
CAR_PROPERTIES.put("Speed", 100);
28+
CAR_PROPERTIES.put("Weight", null);
29+
}
30+
31+
private final Car CAR = new Car();
32+
{
33+
CAR.model = "F60";
34+
CAR.properties = CAR_PROPERTIES;
35+
}
36+
37+
private final JsonInclude.Value BOTH_NON_NULL = JsonInclude.Value.construct(JsonInclude.Include.NON_NULL,
38+
JsonInclude.Include.NON_NULL);
39+
40+
// final private ObjectMapper MAPPER = objectMapper();
41+
42+
// [databind#2572]
43+
public void test2572MapDefault() throws Exception
44+
{
45+
46+
ObjectMapper mapper = JsonMapper.builder()
47+
.defaultPropertyInclusion(BOTH_NON_NULL)
48+
.build();
49+
assertEquals(aposToQuotes("{'Speed':100}"),
50+
mapper.writeValueAsString(CAR_PROPERTIES));
51+
assertEquals(aposToQuotes("{'model':'F60','properties':{'Speed':100}}"),
52+
mapper.writeValueAsString(CAR));
53+
}
54+
55+
// [databind#2572]
56+
public void test2572MapOverrideUseDefaults() throws Exception
57+
{
58+
ObjectMapper mapper = JsonMapper.builder()
59+
.defaultPropertyInclusion(BOTH_NON_NULL)
60+
.build();
61+
mapper.configOverride(Map.class)
62+
.setInclude(JsonInclude.Value.construct(JsonInclude.Include.USE_DEFAULTS,
63+
JsonInclude.Include.USE_DEFAULTS));
64+
assertEquals(aposToQuotes("{'Speed':100}"),
65+
mapper.writeValueAsString(CAR_PROPERTIES));
66+
assertEquals(aposToQuotes("{'model':'F60','properties':{'Speed':100}}"),
67+
mapper.writeValueAsString(CAR));
68+
}
69+
70+
// [databind#2572]
71+
public void test2572MapOverrideInclAlways() throws Exception
72+
{
73+
ObjectMapper mapper = JsonMapper.builder()
74+
.defaultPropertyInclusion(BOTH_NON_NULL)
75+
.build();
76+
mapper.configOverride(Map.class)
77+
.setInclude(JsonInclude.Value.construct(JsonInclude.Include.ALWAYS,
78+
JsonInclude.Include.ALWAYS));
79+
assertEquals(aposToQuotes("{'Speed':100,'Weight':null}"),
80+
mapper.writeValueAsString(CAR_PROPERTIES));
81+
assertEquals(aposToQuotes("{'model':'F60','properties':{'Speed':100,'Weight':null}}}"),
82+
mapper.writeValueAsString(CAR));
83+
}
84+
}

0 commit comments

Comments
 (0)