diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt index 0b0c341d6..11e1d36ba 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt @@ -97,8 +97,13 @@ internal class KotlinValueInstantiator( } tempParamVal } else { - // trying to get suitable "missing" value provided by deserializer - jsonProp.valueDeserializer?.getNullValue(ctxt) + if(paramDef.type.isMarkedNullable) { + // do not try to create any object if it is nullable + null + } else { + // trying to get suitable "missing" value provided by deserializer + jsonProp.valueDeserializer?.getNullValue(ctxt) + } } if (paramVal == null && ((nullToEmptyCollection && jsonProp.type.isCollectionLikeType) || (nullToEmptyMap && jsonProp.type.isMapLikeType))) { diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github490.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github490.kt new file mode 100644 index 000000000..c8bf67229 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github490.kt @@ -0,0 +1,58 @@ +package com.fasterxml.jackson.module.kotlin.test.github + +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import org.hamcrest.CoreMatchers +import org.hamcrest.MatcherAssert.assertThat +import org.junit.Test + +class Github490 { + + @Test + fun testKotlinDeserialization() { + val mapper = jacksonObjectMapper() + val value: DataClassWithAllNullableParams = mapper.readValue("{" + + "\"jsonNodeValueWithNullAsDefaultProvidedNull\":null, " + + "\"jsonNodeValueProvidedNull\":null}") + assertThat( + "Nullable missing Int value should be deserialized as null", + value.intValue, + CoreMatchers.nullValue() + ) + assertThat( + "Nullable missing String value should be deserialized as null", + value.stringValue, + CoreMatchers.nullValue() + ) + assertThat( + "Nullable missing JsonNode value should be deserialized as null and not as NullNode", + value.jsonNodeValue, + CoreMatchers.nullValue() + ) + assertThat( + "Nullable missing JsonNode value should be deserialized as null and not as NullNode", + value.jsonNodeValueProvidedNull, + CoreMatchers.nullValue() + ) + assertThat( + "Nullable by default missing JsonNode value should be deserialized as null and not as NullNode", + value.jsonNodeValueWithNullAsDefault, + CoreMatchers.nullValue() + ) + assertThat( + "Nullable by default JsonNode with provided null value in payload should be deserialized as null and not as NullNode", + value.jsonNodeValueWithNullAsDefaultProvidedNull, + CoreMatchers.nullValue() + ) + } +} + +data class DataClassWithAllNullableParams( + val intValue: Int?, + val stringValue: String?, + val jsonNodeValue: JsonNode?, + val jsonNodeValueProvidedNull: JsonNode?, + val jsonNodeValueWithNullAsDefault: JsonNode? = null, + val jsonNodeValueWithNullAsDefaultProvidedNull: JsonNode? = null +) \ No newline at end of file