Skip to content
Merged
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 @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.std.ContainerDeserializerBase;
import com.fasterxml.jackson.module.scala.ScalaModule;

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

@Override
public T getNullValue(DeserializationContext ctxt) throws JsonMappingException {
if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)) {
if (!ScalaModule.shouldDeserializeNullCollectionsAsEmpty() ||
ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)) {
return super.getNullValue(ctxt);
} else {
return (T) getEmptyValue(ctxt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ object ScalaModule {
}
}

private var deserializeNullCollectionsAsEmpty = true

def builder(): Builder = new Builder()

/**
* @return whether the module should support deserializing null collections as empty (default: true)
*/
def shouldDeserializeNullCollectionsAsEmpty(): Boolean = deserializeNullCollectionsAsEmpty

def deserializeNullCollectionsAsEmpty(asEmpty: Boolean): Unit = {
deserializeNullCollectionsAsEmpty = asEmpty
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ class CaseClassDeserializerTest extends DeserializerTest {
result.list shouldBe List.empty
}

it should "deserialize list as null if deserializeNullCollectionsAsEmpty config is false" in {
val input = """{}"""
try {
ScalaModule.deserializeNullCollectionsAsEmpty(false)
val result = deserialize(input, classOf[ListHolder[String]])
result.list shouldBe null
} finally {
ScalaModule.deserializeNullCollectionsAsEmpty(true) // reset to default
}
}

it should "fail when deserializing null input for list if FAIL_ON_NULL_CREATOR_PROPERTIES enabled" in {
val input = """{}"""
val mapper = newBuilder.enable(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES).build()
Expand Down Expand Up @@ -235,4 +246,14 @@ class CaseClassDeserializerTest extends DeserializerTest {
}
}

it should "deserialize map as null if deserializeNullCollectionsAsEmpty config is false" in {
val input = """{}"""
try {
ScalaModule.deserializeNullCollectionsAsEmpty(false)
val result = deserialize(input, classOf[MapHolder[Int, String]])
result.map shouldBe null
} finally {
ScalaModule.deserializeNullCollectionsAsEmpty(true) // reset to default
}
}
}