vavr-jackson icon indicating copy to clipboard operation
vavr-jackson copied to clipboard

Option serialization not respecting Include.NON_NULL

Open stuartizon opened this issue 5 years ago • 1 comments

Trying to serialize an instance of a class containing an optional field, and this field is coming through as null even when JsonInclude.Include.NON_NULL is specified.

@Value @AllArgsConstructor @JsonInclude(Include.NON_NULL)
class Person {
    private String firstName;
    private Option<String> lastName;
}

@Test
public void personWithNoLastName() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new VavrModule());
    Person person = new Person("John", None());
    String json = "";
    try {
        json = objectMapper.writer().writeValueAsString(person);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }

    Assert.assertEquals("{\"firstName\":\"John\"}", json);
}

This test fails whether I put the NON_NULL setting as an annotation on the data class or if I add it manually to the objectMapper.

Not sure if this is the expected behaviour but I did see this #51 from a couple of years ago which seems to imply it should work.

Using Vavr 0.10.0, Lombok 1.18.6, Jackson 2.9.8

stuartizon avatar May 02 '19 06:05 stuartizon

Hi @stuartizon Not sure I understood you, but lastName is not null in your examples. This is a singleton Option.None.INSTANCE object. This object satisfies the JsonInclude.Include.NON_NULL condition and not satisfies the JsonInclude.Include.NON_EMPTY condition.

ruslansennov avatar May 07 '19 19:05 ruslansennov