Skip to content

Fix missing nullable constructor parameters deserialization #500

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 5 commits into from
Oct 15, 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-base</artifactId>
<version>2.13.0-rc2-SNAPSHOT</version>
<version>2.13.0-SNAPSHOT</version>
</parent>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ wrongwrong (k163377@github)
* Contributed #438: Fixed mapping failure when `private` `companion object` is named
(2.13)

Dmitri Domanine (novtor@github)
* Contributed fix for #490: Missing value of type JsonNode? is deserialized as NullNode instead of null
(2.13)

Marshall Pierce (marshallpierce@github)
* #474: Reported disrespect for @JsonProperty on parent class
(2.12.NEXT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,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 and the value is missing
null
} else {
// to get suitable "missing" value provided by deserializer
jsonProp.valueDeserializer?.getAbsentValue(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,81 @@
package com.fasterxml.jackson.module.kotlin.test.github

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.NullNode
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 TestGithub490 {
val mapper = jacksonObjectMapper()
val value: DataClassWithAllNullableParams = mapper.readValue(
"{" +
"\"jsonNodeValueWithNullAsDefaultProvidedNull\":null, " +
"\"jsonNodeValueProvidedNull\":null}"
)

@Test
fun testKotlinDeserialization_intValue() {
assertThat(
"Nullable missing Int value should be deserialized as null",
value.intValue,
CoreMatchers.nullValue()
)
}

@Test
fun testKotlinDeserialization_stringValue() {
assertThat(
"Nullable missing String value should be deserialized as null",
value.stringValue,
CoreMatchers.nullValue()
)
}

@Test
fun testKotlinDeserialization_jsonNodeValue() {
assertThat(
"Nullable missing JsonNode value should be deserialized as null and not as NullNode",
value.jsonNodeValue,
CoreMatchers.nullValue()
)
}

@Test
fun testKotlinDeserialization_jsonNodeValueProvidedNull() {
assertThat(
"Nullable JsonNode value provided as null should be deserialized as NullNode",
value.jsonNodeValueProvidedNull,
CoreMatchers.equalTo(NullNode.instance)
)
}

@Test
fun testKotlinDeserialization_jsonNodeValueWithNullAsDefault() {
assertThat(
"Nullable by default missing JsonNode value should be deserialized as null and not as NullNode",
value.jsonNodeValueWithNullAsDefault,
CoreMatchers.nullValue()
)
}

@Test
fun testKotlinDeserialization_jsonNodeValueWithNullAsDefaultProvidedNull() {
assertThat(
"Nullable by default JsonNode with provided null value in payload should be deserialized as NullNode",
value.jsonNodeValueWithNullAsDefaultProvidedNull,
CoreMatchers.equalTo(NullNode.instance)
)
}
}

data class DataClassWithAllNullableParams(
val intValue: Int?,
val stringValue: String?,
val jsonNodeValue: JsonNode?,
val jsonNodeValueProvidedNull: JsonNode?,
val jsonNodeValueWithNullAsDefault: JsonNode? = null,
val jsonNodeValueWithNullAsDefaultProvidedNull: JsonNode? = null
)