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

Optional fields do not respect the Include.NON_NULL property [2.9.4] #60

Closed
pq-yilinwei opened this issue Mar 23, 2018 · 3 comments
Closed

Comments

@pq-yilinwei
Copy link

The below output is {bar:null}. The expected output is {}.

public class Example {


  static final class Foo {
    private final Integer bar;
    private final String moo;

    Foo(Integer bar, String moo) {
      this.bar = bar;
      this.moo = moo;
    }

    public Optional<Integer> getBar() {
      return Optional.ofNullable(bar);
    }

    public String getMoo() {
      return moo;
    }
  }

  public static void main(String[] args) {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.registerModule(new Jdk8Module());
    try {
      System.out.println(objectMapper.writeValueAsString(new Foo(null, null)));
    } catch (JsonProcessingException e) {
      e.printStackTrace();
    }
  }
}
@pq-yilinwei pq-yilinwei changed the title Optional fields do not respect the Include.NON_NULL property Optional fields do not respect the Include.NON_NULL property [2.9.3] Mar 23, 2018
@pq-yilinwei pq-yilinwei changed the title Optional fields do not respect the Include.NON_NULL property [2.9.3] Optional fields do not respect the Include.NON_NULL property [2.9.4] Mar 23, 2018
@cowtowncoder
Copy link
Member

Without actual unit test it's hard to say (printing out values is a bad way to demonstrate anything) for sure, but it seems to me like it is working as expected: Optional.ofNullable(null) is NOT null but an instance of Optional. And that will not be excluded with NON_NULL.

Instead, you should probably use NON_ABSENT which will exclude null as well as Optionals without content (case in test above). NON_EMPTY would similarly work, but would also exclude empty arrays, Maps and Collections.

@pq-yilinwei
Copy link
Author

I wrote it as a main since the behaviour is simple and can be easily verified but here is a test.

NON_ABSENT works, but I would argue that NON_NULL should probably encompass the behaviour since the main use of Optional is null safety and Optional.empty signifies a null field anyway.

Regardless I suspect that this is a philosophical argument so feel free to close.

@cowtowncoder
Copy link
Member

Definition of NON_NULL is strictly that it relates to Java null value and nothing else.
NON_ABSENT was added to address the case for Optionals and similar reference types (AtomicReference, Guava, Scala Option).

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

No branches or pull requests

2 participants