Skip to content

Commit

Permalink
Fix #3070: change SerializationFeature.FAIL_ON_EMPTY_BEANS default …
Browse files Browse the repository at this point in the history
…to `false` (#4817)
  • Loading branch information
cowtowncoder authored Dec 1, 2024
1 parent 0206e87 commit bacc38e
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 34 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Versions: 3.x (for earlier see VERSION-2.x)
#3044: Rename `JsonDeserializer`/`JsonSerializer` as `ValueDeserializer`/`ValueSerializer`
#3046: Rename `JsonSerializable` as `JacksonSerializable`
#3047: Rename `Bean[De]SerializerModifier` as `Value[De]SerializerModifier`
#3070: Change default for `SerializationFeature.FAIL_ON_EMPTY_BEANS` to `false`
#3536: Create new exception type `JsonNodeException` for use by `JsonNode`-related problems
#3542: Rename "com.fasterxml.jackson" -> "tools.jackson"
#3601: Change `Optional` deserialization from "absent" value into `null`, from "empty"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ public enum DeserializationFeature implements ConfigFeature

/**
* Feature that can be enabled to allow empty JSON Array
* value (that is, <code>[ ]</code>) to be bound to POJOs (and
* with 2.9, other values too) as `null`.
* value (that is, <code>[ ]</code>) to be bound to POJOs as `null`.
* If disabled, standard POJOs can only be bound from JSON `null` or
* JSON Object (standard meaning that no custom deserializers or
* constructors are defined; both of which can add support for other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public enum SerializationFeature implements ConfigFeature
* (like <code>@JsonSerialize</code>): ones that do have annotations
* do not result in an exception being thrown.
*<p>
* Feature is enabled by default.
* Feature is disabled by default as of Jackson 3.0 (in 2.x it was enabled).
*/
FAIL_ON_EMPTY_BEANS(true),
FAIL_ON_EMPTY_BEANS(false),

/**
* Feature that determines what happens when a direct self-reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import tools.jackson.databind.*;
import tools.jackson.databind.annotation.JsonDeserialize;
import tools.jackson.databind.annotation.JsonSerialize;
import tools.jackson.databind.exc.InvalidDefinitionException;
import tools.jackson.databind.exc.InvalidFormatException;
import tools.jackson.databind.exc.UnrecognizedPropertyException;
import tools.jackson.databind.util.StdConverter;
Expand Down Expand Up @@ -248,25 +247,10 @@ public void testIssue11() throws Exception

// And one more: this time with a minor twist
final Object plaino = new Object();
// first, a failed attempt:
try {
m = MAPPER.convertValue(plaino, Map.class);
fail("Conversion should have failed");
} catch (InvalidDefinitionException e) {
verifyException(e, "no properties discovered");
}

ObjectMapper mapper = jsonMapperBuilder()
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.build();
try {
assertEquals("{}", mapper.writer()
.writeValueAsString(plaino));
} catch (Exception e) {
throw (Exception) e.getCause();
}
assertEquals("{}", MAPPER.writer()
.writeValueAsString(plaino));
// should now work, via serialization/deserialization:
m = mapper.convertValue(plaino, Map.class);
m = MAPPER.convertValue(plaino, Map.class);
assertNotNull(m);
assertEquals(0, m.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ static class VoidBean {
*/

private final ObjectMapper VOID_MAPPER = jsonMapperBuilder()
.enable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES)
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.build();

private final ObjectMapper NO_VOID_MAPPER = jsonMapperBuilder()
.disable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES)
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.build();

@Test
Expand All @@ -42,8 +45,8 @@ public void testVoidBeanSerialization() throws Exception
// with 3.x enabled by default, but may disable
assertEquals("{\"value\":null}", VOID_MAPPER.writeValueAsString(new VoidBean()));
try {
NO_VOID_MAPPER.writeValueAsString(new VoidBean());
fail("Should not pass");
String json = NO_VOID_MAPPER.writeValueAsString(new VoidBean());
fail("Should not pass; got: "+json);
} catch (InvalidDefinitionException e) {
verifyException(e, "no properties discovered");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ public void serialize(Test3787Bean value, JsonGenerator gen, SerializationContex
@Test
public void testWithoutModule()
{
ObjectMapper mapper = jsonMapperBuilder().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).build();
ObjectMapper mapper = jsonMapperBuilder()
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
// since 3.0 not enabled by default
.enable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.build();
// first: serialization failure:
try {
mapper.writeValueAsString(new CustomBean("foo", 3));
Expand Down
9 changes: 5 additions & 4 deletions src/test/java/tools/jackson/databind/ser/TestEmptyClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public void testEmptyWithAnnotations() throws Exception
{
// First: without annotations, should complain
try {
MAPPER.writeValueAsString(new Empty());
MAPPER.writer()
.with(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.writeValueAsString(new Empty());
fail("Should fail");
} catch (InvalidDefinitionException e) {
verifyException(e, "No serializer found for class");
Expand All @@ -87,11 +89,10 @@ public void testEmptyWithAnnotations() throws Exception
@Test
public void testEmptyWithFeature() throws Exception
{
// should be enabled by default
assertTrue(MAPPER.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
// should be disabled by default as of 3.x
assertFalse(MAPPER.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
assertEquals("{}",
MAPPER.writer()
.without(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.writeValueAsString(new Empty()));
}

Expand Down
9 changes: 5 additions & 4 deletions src/test/java/tools/jackson/databind/ser/TestSerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,17 @@ public void testDefaults()
{
SerializationConfig cfg = MAPPER.serializationConfig();

// First, defaults:
assertTrue(cfg.isEnabled(MapperFeature.USE_ANNOTATIONS));
assertTrue(cfg.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS));

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

assertFalse(cfg.isEnabled(SerializationFeature.INDENT_OUTPUT));
assertEquals(MapperFeature.DEFAULT_VIEW_INCLUSION.enabledByDefault(),
cfg.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(cfg.isEnabled(MapperFeature.USE_STATIC_TYPING));
assertTrue(cfg.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
assertTrue(cfg.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertEquals(SerializationFeature.FAIL_ON_EMPTY_BEANS.enabledByDefault(),
cfg.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
assertFalse(cfg.isEnabled(SerializationFeature.INDENT_OUTPUT));
}

@Test
Expand Down

0 comments on commit bacc38e

Please sign in to comment.