-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
JsonSetter.contentNulls
ignored for Object[]
, String[]
and Collection<String>
#5202
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
Merged
cowtowncoder
merged 11 commits into
FasterXML:2.19
from
k163377:kotlin976-followup-for-collection-like
Jul 9, 2025
+393
−10
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4e7257c
Add failing tests for ObjectArrayDeserializer
k163377 7d74e71
Fix #5165 on ObjectArrayDeserializer
k163377 d1b4ba3
Add failing tests for StringArrayDeserializer
k163377 bdc4df0
Fix #5165 on StringArrayDeserializer
k163377 6f98b24
Add failing tests for StringArrayDeserializer
k163377 0cff123
Fix #5165 on StringCollectionDeserializer
k163377 6b0c5f2
Add failing tests for EnumSetDeserializer
k163377 1ab4702
Merge branch '2.19' into kotlin976-followup-for-collection-like
cowtowncoder 9a819be
Minor simplification of tests
cowtowncoder a776722
Add comments, streamline
cowtowncoder 9b7c4ea
Add release notes
cowtowncoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/java/com/fasterxml/jackson/databind/deser/jdk/ObjectArrayDeserializer5165Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.fasterxml.jackson.databind.deser.jdk; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSetter; | ||
import com.fasterxml.jackson.annotation.Nulls; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.exc.InvalidNullException; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
// For [databind#5165] | ||
public class ObjectArrayDeserializer5165Test | ||
{ | ||
static class Dst { | ||
public Integer[] array; | ||
} | ||
|
||
@Test | ||
public void nullsFailTest() { | ||
ObjectMapper mapper = JsonMapper.builder() | ||
.defaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.FAIL)) | ||
.build(); | ||
|
||
// NOTE! Relies on default coercion of "" into `null` for `Integer`s... | ||
assertThrows( | ||
InvalidNullException.class, | ||
() -> mapper.readValue("{\"array\":[\"\"]}", Dst.class) | ||
); | ||
} | ||
|
||
@Test | ||
public void nullsSkipTest() throws Exception { | ||
ObjectMapper mapper = JsonMapper.builder() | ||
.defaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP)) | ||
.build(); | ||
|
||
Dst dst = mapper.readValue("{\"array\":[\"\"]}", Dst.class); | ||
// NOTE! Relies on default coercion of "" into `null` for `Integer`s... | ||
assertEquals(0, dst.array.length, "Null values should be skipped"); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/test/java/com/fasterxml/jackson/databind/deser/jdk/StringArrayDeserializer5165Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.fasterxml.jackson.databind.deser.jdk; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSetter; | ||
import com.fasterxml.jackson.annotation.Nulls; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.fasterxml.jackson.databind.exc.InvalidNullException; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
// For [databind#5165] | ||
public class StringArrayDeserializer5165Test | ||
{ | ||
static class Dst { | ||
public String[] array; | ||
} | ||
|
||
// Custom deserializer that converts empty strings to null | ||
static class EmptyStringToNullDeserializer extends StdDeserializer<String> { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public EmptyStringToNullDeserializer() { | ||
super(String.class); | ||
} | ||
|
||
@Override | ||
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
String value = p.getValueAsString(); | ||
if (value != null && value.isEmpty()) { | ||
return null; | ||
} | ||
return value; | ||
} | ||
} | ||
|
||
private ObjectMapper createMapperWithCustomDeserializer() { | ||
SimpleModule module = new SimpleModule() | ||
.addDeserializer(String.class, new EmptyStringToNullDeserializer()); | ||
|
||
return JsonMapper.builder() | ||
.addModule(module) | ||
.defaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.FAIL)) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void nullsFailTest() { | ||
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. Same as with |
||
ObjectMapper mapper = createMapperWithCustomDeserializer(); | ||
|
||
assertThrows( | ||
InvalidNullException.class, | ||
() -> mapper.readValue("{\"array\":[\"\"]}", Dst.class) | ||
); | ||
} | ||
|
||
@Test | ||
public void nullsSkipTest() throws Exception { | ||
SimpleModule module = new SimpleModule() | ||
.addDeserializer(String.class, new EmptyStringToNullDeserializer()); | ||
|
||
ObjectMapper mapper = JsonMapper.builder() | ||
.addModule(module) | ||
.defaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP)) | ||
.build(); | ||
|
||
Dst dst = mapper.readValue("{\"array\":[\"\"]}", Dst.class); | ||
|
||
assertEquals(0, dst.array.length, "Null values should be skipped"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.