Skip to content

Commit bacc38e

Browse files
authored
Fix #3070: change SerializationFeature.FAIL_ON_EMPTY_BEANS default to false (#4817)
1 parent 0206e87 commit bacc38e

File tree

8 files changed

+27
-34
lines changed

8 files changed

+27
-34
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Versions: 3.x (for earlier see VERSION-2.x)
5555
#3044: Rename `JsonDeserializer`/`JsonSerializer` as `ValueDeserializer`/`ValueSerializer`
5656
#3046: Rename `JsonSerializable` as `JacksonSerializable`
5757
#3047: Rename `Bean[De]SerializerModifier` as `Value[De]SerializerModifier`
58+
#3070: Change default for `SerializationFeature.FAIL_ON_EMPTY_BEANS` to `false`
5859
#3536: Create new exception type `JsonNodeException` for use by `JsonNode`-related problems
5960
#3542: Rename "com.fasterxml.jackson" -> "tools.jackson"
6061
#3601: Change `Optional` deserialization from "absent" value into `null`, from "empty"

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,7 @@ public enum DeserializationFeature implements ConfigFeature
363363

364364
/**
365365
* Feature that can be enabled to allow empty JSON Array
366-
* value (that is, <code>[ ]</code>) to be bound to POJOs (and
367-
* with 2.9, other values too) as `null`.
366+
* value (that is, <code>[ ]</code>) to be bound to POJOs as `null`.
368367
* If disabled, standard POJOs can only be bound from JSON `null` or
369368
* JSON Object (standard meaning that no custom deserializers or
370369
* constructors are defined; both of which can add support for other

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public enum SerializationFeature implements ConfigFeature
6868
* (like <code>@JsonSerialize</code>): ones that do have annotations
6969
* do not result in an exception being thrown.
7070
*<p>
71-
* Feature is enabled by default.
71+
* Feature is disabled by default as of Jackson 3.0 (in 2.x it was enabled).
7272
*/
73-
FAIL_ON_EMPTY_BEANS(true),
73+
FAIL_ON_EMPTY_BEANS(false),
7474

7575
/**
7676
* Feature that determines what happens when a direct self-reference

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

+3-19
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import tools.jackson.databind.*;
1212
import tools.jackson.databind.annotation.JsonDeserialize;
1313
import tools.jackson.databind.annotation.JsonSerialize;
14-
import tools.jackson.databind.exc.InvalidDefinitionException;
1514
import tools.jackson.databind.exc.InvalidFormatException;
1615
import tools.jackson.databind.exc.UnrecognizedPropertyException;
1716
import tools.jackson.databind.util.StdConverter;
@@ -248,25 +247,10 @@ public void testIssue11() throws Exception
248247

249248
// And one more: this time with a minor twist
250249
final Object plaino = new Object();
251-
// first, a failed attempt:
252-
try {
253-
m = MAPPER.convertValue(plaino, Map.class);
254-
fail("Conversion should have failed");
255-
} catch (InvalidDefinitionException e) {
256-
verifyException(e, "no properties discovered");
257-
}
258-
259-
ObjectMapper mapper = jsonMapperBuilder()
260-
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
261-
.build();
262-
try {
263-
assertEquals("{}", mapper.writer()
264-
.writeValueAsString(plaino));
265-
} catch (Exception e) {
266-
throw (Exception) e.getCause();
267-
}
250+
assertEquals("{}", MAPPER.writer()
251+
.writeValueAsString(plaino));
268252
// should now work, via serialization/deserialization:
269-
m = mapper.convertValue(plaino, Map.class);
253+
m = MAPPER.convertValue(plaino, Map.class);
270254
assertNotNull(m);
271255
assertEquals(0, m.size());
272256
}

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ static class VoidBean {
2828
*/
2929

3030
private final ObjectMapper VOID_MAPPER = jsonMapperBuilder()
31+
.enable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES)
3132
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
33+
.enable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
3234
.build();
3335

3436
private final ObjectMapper NO_VOID_MAPPER = jsonMapperBuilder()
3537
.disable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES)
3638
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
39+
.enable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
3740
.build();
3841

3942
@Test
@@ -42,8 +45,8 @@ public void testVoidBeanSerialization() throws Exception
4245
// with 3.x enabled by default, but may disable
4346
assertEquals("{\"value\":null}", VOID_MAPPER.writeValueAsString(new VoidBean()));
4447
try {
45-
NO_VOID_MAPPER.writeValueAsString(new VoidBean());
46-
fail("Should not pass");
48+
String json = NO_VOID_MAPPER.writeValueAsString(new VoidBean());
49+
fail("Should not pass; got: "+json);
4750
} catch (InvalidDefinitionException e) {
4851
verifyException(e, "no properties discovered");
4952
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ public void serialize(Test3787Bean value, JsonGenerator gen, SerializationContex
210210
@Test
211211
public void testWithoutModule()
212212
{
213-
ObjectMapper mapper = jsonMapperBuilder().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).build();
213+
ObjectMapper mapper = jsonMapperBuilder()
214+
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
215+
// since 3.0 not enabled by default
216+
.enable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
217+
.build();
214218
// first: serialization failure:
215219
try {
216220
mapper.writeValueAsString(new CustomBean("foo", 3));

src/test/java/tools/jackson/databind/ser/TestEmptyClass.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public void testEmptyWithAnnotations() throws Exception
6464
{
6565
// First: without annotations, should complain
6666
try {
67-
MAPPER.writeValueAsString(new Empty());
67+
MAPPER.writer()
68+
.with(SerializationFeature.FAIL_ON_EMPTY_BEANS)
69+
.writeValueAsString(new Empty());
6870
fail("Should fail");
6971
} catch (InvalidDefinitionException e) {
7072
verifyException(e, "No serializer found for class");
@@ -87,11 +89,10 @@ public void testEmptyWithAnnotations() throws Exception
8789
@Test
8890
public void testEmptyWithFeature() throws Exception
8991
{
90-
// should be enabled by default
91-
assertTrue(MAPPER.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
92+
// should be disabled by default as of 3.x
93+
assertFalse(MAPPER.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
9294
assertEquals("{}",
9395
MAPPER.writer()
94-
.without(SerializationFeature.FAIL_ON_EMPTY_BEANS)
9596
.writeValueAsString(new Empty()));
9697
}
9798

src/test/java/tools/jackson/databind/ser/TestSerConfig.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,17 @@ public void testDefaults()
7777
{
7878
SerializationConfig cfg = MAPPER.serializationConfig();
7979

80-
// First, defaults:
8180
assertTrue(cfg.isEnabled(MapperFeature.USE_ANNOTATIONS));
8281
assertTrue(cfg.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS));
8382

8483
assertTrue(cfg.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS));
8584

86-
assertFalse(cfg.isEnabled(SerializationFeature.INDENT_OUTPUT));
85+
assertEquals(MapperFeature.DEFAULT_VIEW_INCLUSION.enabledByDefault(),
86+
cfg.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
8787
assertFalse(cfg.isEnabled(MapperFeature.USE_STATIC_TYPING));
88-
assertTrue(cfg.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
89-
assertTrue(cfg.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
88+
assertEquals(SerializationFeature.FAIL_ON_EMPTY_BEANS.enabledByDefault(),
89+
cfg.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
90+
assertFalse(cfg.isEnabled(SerializationFeature.INDENT_OUTPUT));
9091
}
9192

9293
@Test

0 commit comments

Comments
 (0)