|
1 | | -package com.fasterxml.jackson.databind.tofix; |
| 1 | +package com.fasterxml.jackson.databind.deser.enums; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.util.EnumSet; |
5 | 5 |
|
6 | | -import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
7 | 6 | import org.junit.jupiter.api.Test; |
8 | 7 |
|
9 | 8 | import com.fasterxml.jackson.annotation.JsonSetter; |
|
16 | 15 | import com.fasterxml.jackson.databind.exc.InvalidNullException; |
17 | 16 | import com.fasterxml.jackson.databind.json.JsonMapper; |
18 | 17 | import com.fasterxml.jackson.databind.module.SimpleModule; |
| 18 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
19 | 19 |
|
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 21 | import static org.junit.jupiter.api.Assertions.assertThrows; |
21 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
22 | 22 |
|
23 | 23 | // For [databind#5203] |
24 | 24 | public class EnumSetDeserializer5203Test |
| 25 | + extends DatabindTestUtil |
25 | 26 | { |
26 | | - public enum MyEnum { |
| 27 | + enum MyEnum { |
27 | 28 | FOO |
28 | 29 | } |
29 | 30 |
|
@@ -57,40 +58,45 @@ public MyEnum deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx |
57 | 58 | } |
58 | 59 | } |
59 | 60 |
|
60 | | - private ObjectMapper createMapperWithCustomDeserializer() { |
61 | | - SimpleModule module = new SimpleModule(); |
62 | | - module.addDeserializer(MyEnum.class, new EmptyStringToNullDeserializer()); |
63 | | - |
64 | | - return JsonMapper.builder() |
65 | | - .addModule(module) |
66 | | - .defaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.FAIL)) |
67 | | - .build(); |
| 61 | + @Test |
| 62 | + public void nullsDefaultTest() throws Exception { |
| 63 | + // In 2.x, default is to skip nulls (for backwards-compatibility) |
| 64 | + final ObjectMapper mapper = createMapperWithCustomDeserializer(Nulls.DEFAULT); |
| 65 | + _verifySkipResult(mapper); |
68 | 66 | } |
69 | 67 |
|
70 | | - @JacksonTestFailureExpected |
71 | 68 | @Test |
72 | | - public void nullsFailTest() { |
73 | | - ObjectMapper mapper = createMapperWithCustomDeserializer(); |
| 69 | + public void nullsFailTest() throws Exception { |
| 70 | + final ObjectMapper mapper = createMapperWithCustomDeserializer(Nulls.FAIL); |
74 | 71 |
|
75 | 72 | assertThrows( |
76 | 73 | InvalidNullException.class, |
77 | 74 | () -> mapper.readValue("{\"set\":[\"\"]}", new TypeReference<Dst>(){}) |
78 | 75 | ); |
79 | 76 | } |
80 | | - |
81 | | - @JacksonTestFailureExpected |
| 77 | + |
82 | 78 | @Test |
83 | 79 | public void nullsSkipTest() throws Exception { |
| 80 | + final ObjectMapper mapper = createMapperWithCustomDeserializer(Nulls.SKIP); |
| 81 | + _verifySkipResult(mapper); |
| 82 | + } |
| 83 | + |
| 84 | + private ObjectMapper createMapperWithCustomDeserializer(Nulls nullHandling) { |
84 | 85 | SimpleModule module = new SimpleModule(); |
85 | 86 | module.addDeserializer(MyEnum.class, new EmptyStringToNullDeserializer()); |
86 | 87 |
|
87 | | - ObjectMapper mapper = JsonMapper.builder() |
| 88 | + return JsonMapper.builder() |
88 | 89 | .addModule(module) |
89 | | - .defaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP)) |
| 90 | + .defaultSetterInfo(JsonSetter.Value.forContentNulls(nullHandling)) |
90 | 91 | .build(); |
| 92 | + } |
91 | 93 |
|
| 94 | + private void _verifySkipResult(ObjectMapper mapper) throws Exception |
| 95 | + { |
92 | 96 | Dst dst = mapper.readValue("{\"set\":[\"FOO\",\"\"]}", new TypeReference<Dst>() {}); |
93 | 97 |
|
94 | | - assertTrue(dst.getSet().isEmpty(), "Null values should be skipped"); |
| 98 | + // Null value (from empty string) should be skipped, but FOO should be present |
| 99 | + assertEquals(1, dst.getSet().size()); |
| 100 | + assertEquals(MyEnum.FOO, dst.getSet().iterator().next()); |
95 | 101 | } |
96 | 102 | } |
0 commit comments