Skip to content

Commit cb22a45

Browse files
authored
add config to allow override of deserializeNullCollectionsAsEmpty (#745)
* add config to allow override of deserializeNullCollectionsAsEmpty * Update CaseClassDeserializerTest.scala
1 parent 0a7489f commit cb22a45

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
@@ -206,6 +206,17 @@ class CaseClassDeserializerTest extends DeserializerTest {
206206
result.list shouldBe List.empty
207207
}
208208

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

249+
it should "deserialize map as null if deserializeNullCollectionsAsEmpty config is false" in {
250+
val input = """{}"""
251+
try {
252+
ScalaModule.deserializeNullCollectionsAsEmpty(false)
253+
val result = deserialize(input, classOf[MapHolder[Int, String]])
254+
result.map shouldBe null
255+
} finally {
256+
ScalaModule.deserializeNullCollectionsAsEmpty(true) // reset to default
257+
}
258+
}
238259
}

0 commit comments

Comments
 (0)