Skip to content

Commit a77efa2

Browse files
authored
(complete #4858): actually change default FAIL_ON_NULL_FOR_PRIMITIVES to true (#4890)
1 parent 1d85089 commit a77efa2

15 files changed

+54
-23
lines changed

src/main/java/tools/jackson/databind/DeserializationFeature.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public enum DeserializationFeature implements ConfigFeature
126126
*<p>
127127
* Feature is enabled by default as of Jackson 3.0 (in 2.x it was disabled).
128128
*/
129-
FAIL_ON_NULL_FOR_PRIMITIVES(false),
129+
FAIL_ON_NULL_FOR_PRIMITIVES(true),
130130

131131
/**
132132
* Feature that determines whether JSON integer numbers are valid

src/test/java/tools/jackson/databind/convert/CoerceToBooleanTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public BooleanWrapper(@JsonProperty("ctor") Boolean foo) {
5252
public void setPrimitive(boolean v) { primitive = v; }
5353
}
5454

55-
private final ObjectMapper DEFAULT_MAPPER = newJsonMapper();
55+
private final ObjectMapper DEFAULT_MAPPER = jsonMapperBuilder()
56+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
57+
.build();
5658

5759
private final ObjectMapper LEGACY_NONCOERCING_MAPPER = jsonMapperBuilder()
5860
.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS)

src/test/java/tools/jackson/databind/deser/builder/BuilderInfiniteLoop1979Test.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import com.fasterxml.jackson.annotation.JsonProperty;
66
import com.fasterxml.jackson.annotation.JsonUnwrapped;
77

8-
import tools.jackson.databind.*;
98
import tools.jackson.databind.annotation.JsonDeserialize;
9+
import tools.jackson.databind.testutil.DatabindTestUtil;
1010

1111
import static org.junit.jupiter.api.Assertions.assertNotNull;
1212

1313
//first for [databind#1978] but follow up for [databind#1979]
1414
public class BuilderInfiniteLoop1979Test
15+
extends DatabindTestUtil
1516
{
1617
static class Builder
1718
{
@@ -93,8 +94,7 @@ static class SubBean
9394
public void testInfiniteLoop1978() throws Exception
9495
{
9596
String json = "{\"sub.el1\":34,\"sub.el2\":\"some text\"}";
96-
ObjectMapper mapper = new ObjectMapper();
97-
Bean bean = mapper.readValue( json, Bean.class );
97+
Bean bean = sharedMapper().readValue( json, Bean.class );
9898
assertNotNull(bean);
9999
}
100100
}

src/test/java/tools/jackson/databind/deser/creators/FailOnNullCreatorTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public Person(@JsonProperty(value="name") String name,
3232
}
3333
}
3434

35-
private final ObjectReader PERSON_READER = sharedMapper().readerFor(Person.class);
35+
private final ObjectReader PERSON_READER = sharedMapper()
36+
.readerFor(Person.class)
37+
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
3638

3739
@Test
3840
public void testRequiredNonNullParam() throws Exception

src/test/java/tools/jackson/databind/deser/jdk/JDKNumberDeserTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public MyBeanValue deserialize(JsonParser jp, DeserializationContext ctxt)
102102
/**********************************************************************
103103
*/
104104

105-
private final ObjectMapper MAPPER = new ObjectMapper();
105+
private final ObjectMapper MAPPER = newJsonMapper();
106106

107107
@Test
108108
public void testNaN() throws Exception

src/test/java/tools/jackson/databind/introspect/DefaultCreatorDetection4584Test.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ public void testCanonicalConstructor2ArgPropertiesCreator() throws Exception
189189
assertEquals(POJO4584.factoryString(null),
190190
readerWith(new PrimaryCreatorFindingIntrospector(JsonCreator.Mode.PROPERTIES,
191191
String.class, Integer.TYPE))
192-
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
193-
.readValue(a2q("{}")));
192+
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
193+
.readValue(a2q("{}")));
194194
}
195195

196196
/*

src/test/java/tools/jackson/databind/jsontype/ext/TestPropertyCreatorSubtypesExternalPropertyMissingProperty.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ public static Orange getOrange(@JsonProperty("name") String name, @JsonProperty(
127127
{
128128
final ObjectMapper mapper = new ObjectMapper();
129129
BOX_READER_PASS = mapper.readerFor(Box.class)
130-
.without(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY);
130+
.without(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY)
131+
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
131132
BOX_READER_FAIL = mapper.readerFor(Box.class)
132-
.with(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY);
133+
.with(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY)
134+
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
133135
}
134136

135137
/**

src/test/java/tools/jackson/databind/module/SimpleModuleTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public class SimpleModuleTest extends DatabindTestUtil
2727
final static class CustomBean
2828
{
2929
protected String str;
30-
protected int num;
30+
protected Integer num;
3131

32-
public CustomBean(String s, int i) {
32+
public CustomBean(String s, Integer i) {
3333
str = s;
3434
num = i;
3535
}

src/test/java/tools/jackson/databind/records/RecordDeserialization3906Test.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.annotation.JsonCreator;
88
import com.fasterxml.jackson.annotation.PropertyAccessor;
99

10+
import tools.jackson.databind.DeserializationFeature;
1011
import tools.jackson.databind.ObjectMapper;
1112
import tools.jackson.databind.cfg.MapperConfig;
1213
import tools.jackson.databind.introspect.AnnotatedClass;
@@ -60,7 +61,8 @@ private record PrivateRecord3906(String string, int integer) {
6061
@Test
6162
public void testEmptyJsonToRecordWorkAround() throws Exception {
6263
ObjectMapper mapper = jsonMapperBuilder()
63-
.changeDefaultVisibility(vc ->
64+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
65+
.changeDefaultVisibility(vc ->
6466
vc.withVisibility(PropertyAccessor.ALL, Visibility.NONE)
6567
.withVisibility(PropertyAccessor.CREATOR, Visibility.ANY))
6668
.build();
@@ -72,7 +74,8 @@ public void testEmptyJsonToRecordWorkAround() throws Exception {
7274
@Test
7375
public void testEmptyJsonToRecordCreatorsVisible() throws Exception {
7476
ObjectMapper mapper = jsonMapperBuilder()
75-
.changeDefaultVisibility(vc ->
77+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
78+
.changeDefaultVisibility(vc ->
7679
vc.withVisibility(PropertyAccessor.CREATOR, Visibility.NON_PRIVATE))
7780
.build();
7881

@@ -97,23 +100,29 @@ public VisibilityChecker findAutoDetectVisibility(MapperConfig<?> cfg,
97100
}
98101
});
99102
}
100-
}).build();
103+
})
104+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
105+
.build();
101106

102107
Record3906 recordDeser = mapper.readValue("{}", Record3906.class);
103108
assertEquals(new Record3906(null, 0), recordDeser);
104109
}
105110

106111
@Test
107112
public void testEmptyJsonToRecordDirectAutoDetectConfig() throws Exception {
108-
ObjectMapper mapper = newJsonMapper();
113+
ObjectMapper mapper = jsonMapperBuilder()
114+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
115+
.build();
109116

110117
Record3906Annotated recordDeser = mapper.readValue("{}", Record3906Annotated.class);
111118
assertEquals(new Record3906Annotated(null, 0), recordDeser);
112119
}
113120

114121
@Test
115122
public void testEmptyJsonToRecordJsonCreator() throws Exception {
116-
ObjectMapper mapper = newJsonMapper();
123+
ObjectMapper mapper = jsonMapperBuilder()
124+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
125+
.build();
117126

118127
Record3906Creator recordDeser = mapper.readValue("{}", Record3906Creator.class);
119128
assertEquals(new Record3906Creator(null, 0), recordDeser);
@@ -143,6 +152,7 @@ public VisibilityChecker findAutoDetectVisibility(MapperConfig<?> cfg,
143152
});
144153
}
145154
})
155+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
146156
.build();
147157

148158
assertEquals(new Record3906(null, 0),

src/test/java/tools/jackson/databind/records/RecordExplicitCreatorsTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public static RecordWithExplicitFactoryMethod valueOf(String value) {
108108
private final ObjectMapper MAPPER = jsonMapperBuilder()
109109
.disable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS) // So that test cases don't have to assert weird error messages
110110
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
111+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
111112
.build();
112113

113114
/*

src/test/java/tools/jackson/databind/records/RecordNullHandling3847Test.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonProperty;
66
import com.fasterxml.jackson.annotation.Nulls;
77

8+
import tools.jackson.databind.DeserializationFeature;
89
import tools.jackson.databind.ObjectMapper;
910
import tools.jackson.databind.cfg.CoercionAction;
1011
import tools.jackson.databind.cfg.CoercionInputShape;
@@ -43,6 +44,10 @@ public record FixedRecord(@JsonProperty("field_name") String fieldName) {}
4344
.withCoercionConfigDefaults(config -> config.setCoercion(CoercionInputShape.String, CoercionAction.Fail))
4445
.build();
4546

47+
private final ObjectMapper DEFAULT_MAPPER = jsonMapperBuilder()
48+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
49+
.build();
50+
4651
@Test
4752
public void testPojoNullHandlingValid() throws Exception {
4853
Pojo3847 pojo = NULL_MAPPER.readValue(a2q("{'fieldName': 'value'}"), Pojo3847.class); // expected
@@ -118,13 +123,14 @@ public void testRecordFixerNullHandlingEmptyJson() throws Exception {
118123

119124
@Test
120125
public void testRecordDefaultNullDeserialization() throws Exception {
121-
PlainRecord pr = new ObjectMapper().readValue("{}", PlainRecord.class);
126+
PlainRecord pr = DEFAULT_MAPPER.readValue("{}", PlainRecord.class);
122127
assertNull(pr.fieldName);
123128
}
124129

125130
@Test
126131
public void testIntRecordDefaultNullDeserialization() throws Exception {
127-
IntRecord ir = new ObjectMapper().readValue("{}", IntRecord.class);
132+
IntRecord ir = DEFAULT_MAPPER.readerFor(IntRecord.class)
133+
.readValue("{}");
128134
assertNull(ir.description);
129135
assertEquals(0, ir.value);
130136
}

src/test/java/tools/jackson/databind/records/RecordWithJsonIgnoreTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonIgnore;
66
import com.fasterxml.jackson.annotation.JsonProperty;
77

8+
import tools.jackson.databind.DeserializationFeature;
89
import tools.jackson.databind.ObjectMapper;
910
import tools.jackson.databind.testutil.DatabindTestUtil;
1011

@@ -104,7 +105,10 @@ public void testSerializeJsonIgnorePrimitiveTypeRecord() throws Exception {
104105

105106
@Test
106107
public void testDeserializeJsonIgnorePrimitiveTypeRecord() throws Exception {
107-
RecordWithIgnorePrimitiveType value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnorePrimitiveType.class);
108+
RecordWithIgnorePrimitiveType value = jsonMapperBuilder()
109+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
110+
.build()
111+
.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnorePrimitiveType.class);
108112
assertEquals(new RecordWithIgnorePrimitiveType(0, "Bob"), value);
109113
}
110114
}

src/test/java/tools/jackson/databind/records/RecordWithReadOnlyTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonProperty;
66
import com.fasterxml.jackson.annotation.JsonProperty.Access;
77

8+
import tools.jackson.databind.DeserializationFeature;
89
import tools.jackson.databind.ObjectMapper;
910
import tools.jackson.databind.testutil.DatabindTestUtil;
1011

@@ -52,7 +53,9 @@ public RecordWithReadOnlyAllAndNoArgConstructor() {
5253
}
5354
}
5455

55-
private final ObjectMapper MAPPER = newJsonMapper();
56+
private final ObjectMapper MAPPER = jsonMapperBuilder()
57+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
58+
.build();
5659

5760
/*
5861
/**********************************************************************

src/test/java/tools/jackson/databind/struct/TestPOJOAsArrayWithBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void testWithCreator() throws Exception
145145
{
146146
ObjectReader r = MAPPER.readerFor(CreatorValue.class)
147147
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
148-
148+
149149
CreatorValue value = r.readValue("[1,2,3]");
150150
assertEquals(1, value.a);
151151
assertEquals(2, value.b);

src/test/java/tools/jackson/databind/struct/UnwrapSingleArrayScalarsTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public void testBooleanPrimitiveArrayUnwrap() throws Exception
4040
// [databind#381]
4141
ObjectMapper mapper = jsonMapperBuilder()
4242
.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
43+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
4344
.build();
4445
BooleanBean result = mapper.readValue(new StringReader("{\"v\":[true]}"), BooleanBean.class);
4546
assertTrue(result._v);

0 commit comments

Comments
 (0)