Skip to content

Commit 55a62f6

Browse files
authored
databind #3227 - Raise Exception If contentNull is Fail and content is null (#3248)
1 parent 1593650 commit 55a62f6

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1885,7 +1885,9 @@ protected Nulls findContentNullStyle(DeserializationContext ctxt, BeanProperty p
18851885
if (prop != null) {
18861886
return prop.getMetadata().getContentNulls();
18871887
}
1888-
return null;
1888+
1889+
DeserializationConfig config = ctxt.getConfig();
1890+
return config.getDefaultSetterInfo().getContentNulls();
18891891
}
18901892

18911893
// @since 2.9

src/test/java/com/fasterxml/jackson/databind/deser/NullHandlingTest.java

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package com.fasterxml.jackson.databind.deser;
22

33
import java.io.IOException;
4-
import java.util.Arrays;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
4+
import java.util.*;
85

9-
import com.fasterxml.jackson.annotation.JsonAnySetter;
10-
import com.fasterxml.jackson.annotation.JsonSubTypes;
6+
import com.fasterxml.jackson.annotation.*;
117
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
12-
import com.fasterxml.jackson.annotation.JsonTypeInfo;
138
import com.fasterxml.jackson.core.*;
9+
import com.fasterxml.jackson.core.type.TypeReference;
1410
import com.fasterxml.jackson.databind.*;
11+
import com.fasterxml.jackson.databind.exc.InvalidNullException;
12+
import com.fasterxml.jackson.databind.json.JsonMapper;
1513
import com.fasterxml.jackson.databind.module.SimpleModule;
1614

1715
public class NullHandlingTest extends BaseMapTest
@@ -78,6 +76,11 @@ public TypeB(String b) {
7876
}
7977
}
8078

79+
// [databind #3227]
80+
enum EnumMapTestEnum {
81+
A, B, C;
82+
}
83+
8184
private final ObjectMapper MAPPER = objectMapper();
8285

8386
/*
@@ -201,4 +204,59 @@ public void testPolymorphicDataNull() throws Exception
201204
RootData typeBNullData = MAPPER.readValue(typeBNull, RootData.class);
202205
assertNull("Proxy should be null!", typeBNullData.proxy);
203206
}
207+
208+
// Test for databind #3227
209+
public void testNullsFailIfContentValueIsNull() throws Exception {
210+
ObjectMapper objectMapper = JsonMapper.builder()
211+
.defaultSetterInfo(JsonSetter.Value.construct(Nulls.FAIL, Nulls.FAIL))
212+
.build();
213+
214+
try {
215+
objectMapper.readValue("{ \"field\": null, \"property\": '1' }", Map.class);
216+
fail("InvalidNullException expected");
217+
} catch (InvalidNullException e) {
218+
}
219+
220+
try {
221+
objectMapper.readValue("{ \"A\": 1, \"B\": null }", new TypeReference<EnumMap<EnumMapTestEnum, Integer>>() {});
222+
fail("InvalidNullException expected");
223+
} catch (InvalidNullException e) {
224+
}
225+
226+
try {
227+
objectMapper.readValue("[null, {\"field\": 1}]", new TypeReference<List<Object>>() {});
228+
fail("InvalidNullException expected");
229+
} catch (InvalidNullException e) {
230+
}
231+
232+
try {
233+
objectMapper.readValue("[{\"field\": 1}, null]", new TypeReference<Set<Object>>() {});
234+
fail("InvalidNullException expected");
235+
} catch (InvalidNullException e) {
236+
}
237+
238+
try {
239+
objectMapper.readValue("[null, {\"field\": 1}]", Object[].class);
240+
fail("InvalidNullException expected");
241+
} catch (InvalidNullException e) {
242+
}
243+
244+
try {
245+
objectMapper.readValue("[\"foo\", null]", new TypeReference<List<String>>() {});
246+
fail("InvalidNullException expected");
247+
} catch (InvalidNullException e) {
248+
}
249+
250+
try {
251+
objectMapper.readValue("[\"foo\", null]", new TypeReference<Set<String>>() {});
252+
fail("InvalidNullException expected");
253+
} catch (InvalidNullException e) {
254+
}
255+
256+
try {
257+
objectMapper.readValue("[null, \"foo\"]", String[].class);
258+
fail("InvalidNullException expected");
259+
} catch (InvalidNullException e) {
260+
}
261+
}
204262
}

0 commit comments

Comments
 (0)