-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all commits
b1ada0a
b0ae6a3
bff3865
fd36b9f
5359291
b7239dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
||
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"}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's change this to use |
||
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); | ||
} | ||
} |
There was a problem hiding this comment.
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.