ConfigOverride using Nulls.AS_EMPTY does not work with @JsonManagedReference (original) (raw)

Search before asking

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