Skip to content

ConfigOverride using Nulls.AS_EMPTY does not work with @JsonManagedReference #4758

Description

@robrat

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

configOverride for Lists work as expected, but if any of the lists is a ManagedReference, then the list is initialized with null instead of an empty list.

Version Information

2.17.1

Reproduction

Without JsonManagedReference and JsonBackReference the children list is initialized with a non null value, but with these annotations it becomes null, but only after I add Value.forContentNulls, otherwise it would throw an IllegalStateException.

class JacksonManagedRefTest {

  @lombok.Getter @lombok.Setter
  static class Parent {
    private String name;

    @JsonManagedReference
    private List<Item> children = new ArrayList<>();
  }

  @lombok.Getter @lombok.Setter
  static class Item {
    private String name;

    @JsonBackReference
    private Parent parent;
  }

  @Test
  void test_nonEmptyChildren() throws Exception {
    ObjectMapper om = new ObjectMapper();

    String str = "{ \"name\": \"parent\", \"children\":[{\"name\":\"child1\"},{\"name\":\"child1\"}]}";
    Parent obj = om.readValue(str, Parent.class);
    Assertions.assertNotNull(obj.children);
  }

  // java.lang.IllegalStateException: Should never try to reset delegate
  @Test
  void test_nullChildren() throws Exception {
    ObjectMapper om = new ObjectMapper();
    om.configOverride(List.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));

    String str = "{ \"name\": \"parent\", \"children\": null }";
    Parent obj = om.readValue(str, Parent.class);
    Assertions.assertNotNull(obj.children);
  }

  // not null assertion fails
  @Test
  void test_nullChildren_contentNullConfig() throws Exception {
    ObjectMapper om = new ObjectMapper();
    om.configOverride(List.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
    om.configOverride(List.class).setSetterInfo(JsonSetter.Value.forContentNulls(Nulls.AS_EMPTY));

    String str = "{ \"name\": \"parent\", \"children\": null }";
    Parent obj = om.readValue(str, Parent.class);
    Assertions.assertNotNull(obj.children);
  }
}

Expected behavior

List property children is initialized with an empty list

Additional context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.2has-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issuelombokIssue (likely) related to use of Lombok

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions