Skip to content

JsonNode? missing value should be deserialized as null and not as NullNode #491

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
merged 2 commits into from
Sep 3, 2021
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 @@ -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))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)