Skip to content

Commit 5a35bae

Browse files
committed
add config to allow override of deserializeNullCollectionsAsEmpty (#745)
* add config to allow override of deserializeNullCollectionsAsEmpty * Update CaseClassDeserializerTest.scala
1 parent 647111c commit 5a35bae

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/main/java/com/fasterxml/jackson/module/scala/deser/ContainerDeserializerWithNullValueAsEmpty.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.databind.JavaType;
66
import com.fasterxml.jackson.databind.JsonMappingException;
77
import com.fasterxml.jackson.databind.deser.std.ContainerDeserializerBase;
8+
import com.fasterxml.jackson.module.scala.ScalaModule;
89

910
/**
1011
* Internal Usage only
@@ -17,7 +18,8 @@ protected ContainerDeserializerWithNullValueAsEmpty(JavaType selfType) {
1718

1819
@Override
1920
public T getNullValue(DeserializationContext ctxt) throws JsonMappingException {
20-
if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)) {
21+
if (!ScalaModule.shouldDeserializeNullCollectionsAsEmpty() ||
22+
ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)) {
2123
return super.getNullValue(ctxt);
2224
} else {
2325
return (T) getEmptyValue(ctxt);

src/main/scala/com/fasterxml/jackson/module/scala/ScalaModule.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ object ScalaModule {
4949
}
5050
}
5151

52+
private var deserializeNullCollectionsAsEmpty = true
53+
5254
def builder(): Builder = new Builder()
55+
56+
/**
57+
* @return whether the module should support deserializing null collections as empty (default: true)
58+
*/
59+
def shouldDeserializeNullCollectionsAsEmpty(): Boolean = deserializeNullCollectionsAsEmpty
60+
61+
def deserializeNullCollectionsAsEmpty(asEmpty: Boolean): Unit = {
62+
deserializeNullCollectionsAsEmpty = asEmpty
63+
}
5364
}
5465

src/test/scala/com/fasterxml/jackson/module/scala/deser/CaseClassDeserializerTest.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ class CaseClassDeserializerTest extends DeserializerTest {
207207
result.list shouldBe List.empty
208208
}
209209

210+
it should "deserialize list as null if deserializeNullCollectionsAsEmpty config is false" in {
211+
val input = """{}"""
212+
try {
213+
ScalaModule.deserializeNullCollectionsAsEmpty(false)
214+
val result = deserialize(input, classOf[ListHolder[String]])
215+
result.list shouldBe null
216+
} finally {
217+
ScalaModule.deserializeNullCollectionsAsEmpty(true) // reset to default
218+
}
219+
}
220+
210221
it should "fail when deserializing null input for list if FAIL_ON_NULL_CREATOR_PROPERTIES enabled" in {
211222
val input = """{}"""
212223
val mapper = newBuilder.enable(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES).build()
@@ -248,4 +259,14 @@ class CaseClassDeserializerTest extends DeserializerTest {
248259
}
249260
}
250261

262+
it should "deserialize map as null if deserializeNullCollectionsAsEmpty config is false" in {
263+
val input = """{}"""
264+
try {
265+
ScalaModule.deserializeNullCollectionsAsEmpty(false)
266+
val result = deserialize(input, classOf[MapHolder[Int, String]])
267+
result.map shouldBe null
268+
} finally {
269+
ScalaModule.deserializeNullCollectionsAsEmpty(true) // reset to default
270+
}
271+
}
251272
}

0 commit comments

Comments
 (0)