Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option serialization not respecting Include.NON_NULL #133

Open
stuartizon opened this issue May 2, 2019 · 1 comment
Open

Option serialization not respecting Include.NON_NULL #133

stuartizon opened this issue May 2, 2019 · 1 comment
Labels

Comments

@stuartizon
Copy link

stuartizon commented May 2, 2019

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

@ruslansennov
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants