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

Make FieldProperty skip nulls on set() method #4455

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ public Object deserializeSetAndReturn(JsonParser p,
@Override
public void set(Object instance, Object value) throws IOException
{
// [databind#4441] 2.17 Fix `@JsonSetter(Nulls.SKIP)` field is not skipped up to this point
if (value == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same also needed for setAndReturn() method, I think.

if (_skipNulls) {
return;
}
}
try {
_field.set(instance, value);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ public Object deserializeSetAndReturn(JsonParser p,
@Override
public final void set(Object instance, Object value) throws IOException
{
// [databind#4441] 2.17 Fix `@JsonSetter(Nulls.SKIP)` Method is not skipped up to this point
if (value == null) {
if (_skipNulls) {
return;
}
}
try {
_setter.invoke(instance, value);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package com.fasterxml.jackson.databind.deser;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Nulls.SKIP tests are under src/test/java/com/fasterxml/jackson/databind/deser/filter/ typically?


import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.junit.jupiter.api.Test;

import java.beans.ConstructorProperties;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.fasterxml.jackson.databind.BaseTest.a2q;
import static org.junit.jupiter.api.Assertions.assertNotNull;

// [databind#4441] @JsonSetter(nulls = Nulls.SKIP) doesn't work in some situations
public class SkipNulls4441Test {

static class Middle {
@JsonSetter(nulls = Nulls.SKIP)
private final List<Inner> listInner = new ArrayList<>();
private final String field1;

@ConstructorProperties({"field1"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this to use @JsonCreator instead to reduce use of @ConstructorProperties (in JPMS it's in extra package)

public Middle(String field1) {
this.field1 = field1;
}

public List<Inner> getListInner() {
return listInner;
}

public String getField1() {
return field1;
}
}

static class Inner {
private final String field1;

@ConstructorProperties({"field1"})
public Inner(String field1) {
this.field1 = field1;
}

public String getField1() {
return field1;
}
}

static class MiddleSetter {
private List<InnerSetter> listInner = new ArrayList<>();
private final String field1;

@ConstructorProperties({"field1"})
public MiddleSetter(String field1) {
this.field1 = field1;
}

@JsonSetter(nulls = Nulls.SKIP)
public void setListInner(List<InnerSetter> listInner) {
// null passed here
Objects.requireNonNull(listInner);
this.listInner = listInner;
}

public List<InnerSetter> getListInner() {
return listInner;
}

public String getField1() {
return field1;
}
}

static class InnerSetter {
private final String field1;

@ConstructorProperties({"field1"})
public InnerSetter(String field1) {
this.field1 = field1;
}

public String getField1() {
return field1;
}
}

private final ObjectMapper objectMapper = JsonMapper.builder().build();

private final String NULL_ENDING_JSON = a2q("{" +
" 'field1': 'data', " +
" 'listInner': null " +
"}");

private final String NULL_BEGINNING_JSON = a2q("{" +
" 'listInner': null, " +
" 'field1': 'data' " +
"}");

@Test
public void testFields() throws Exception {
// Passes
// For some reason, if most-inner "list1" field is null in the end, it works
_testFieldNullSkip(NULL_ENDING_JSON);
// Fails
// But if it's null in the beginning, it doesn't work
_testFieldNullSkip(NULL_BEGINNING_JSON);
}

@Test
public void testMethods() throws Exception {
// Passes
// For some reason, if most-inner "list1" field is null in the end, it works
_testMethodNullSkip(NULL_ENDING_JSON);
// Fails
// But if it's null in the beginning, it doesn't work
_testMethodNullSkip(NULL_BEGINNING_JSON);
}

private void _testMethodNullSkip(String s) throws Exception {
MiddleSetter middle = objectMapper.readValue(s, MiddleSetter.class);

testMiddleSetter(middle);
}

private void _testFieldNullSkip(String s) throws Exception {
Middle middle = objectMapper.readValue(s, Middle.class);

testMiddle(middle);
}

private void testMiddle(Middle middle) {
validateNotNull(middle);
validateNotNull(middle.getField1());
validateNotNull(middle.getListInner());
}

private void testMiddleSetter(MiddleSetter middle) {
validateNotNull(middle);
validateNotNull(middle.getField1());
validateNotNull(middle.getListInner());
}

private static void validateNotNull(Object o) {
assertNotNull(o);
}
}