Skip to content

Commit 05955a4

Browse files
authored
Merge pull request #707 from k163377/fix-#526
Changed to use default argument on `null` if `JsonSetter(nulls = Nulls.SKIP)` is specified.
2 parents 0ce2894 + 0466751 commit 05955a4

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

release-notes/CREDITS-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ kkurczewski
2121
* #689: Add KotlinDuration support
2222

2323
WrongWrong (@k163377)
24+
* #707: Changed to use default argument on null if JsonSetter(nulls = Nulls.SKIP) is specified.
2425
* #700: Reduce the load on the search process for serializers
2526
* #687: Optimize and Refactor KotlinValueInstantiator.createFromObjectWith
2627
* #686: Add KotlinPropertyNameAsImplicitName option

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Co-maintainers:
1818

1919
2.16.0 (not yet released)
2020

21+
#707: If JsonSetter(nulls = Nulls.SKIP) is specified, the default argument is now used when null.
2122
#700: Reduce the load on the search process for serializers.
2223
#689: Added UseJavaDurationConversion feature.
2324
By enabling this feature and adding the Java Time module, Kotlin Duration can be handled in the same way as Java Duration.

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.module.kotlin
22

3+
import com.fasterxml.jackson.annotation.Nulls
34
import com.fasterxml.jackson.databind.BeanDescription
45
import com.fasterxml.jackson.databind.DeserializationConfig
56
import com.fasterxml.jackson.databind.DeserializationContext
@@ -32,6 +33,9 @@ internal class KotlinValueInstantiator(
3233

3334
private fun List<KTypeProjection>.markedNonNullAt(index: Int) = getOrNull(index)?.type?.isMarkedNullable == false
3435

36+
private fun SettableBeanProperty.skipNulls(): Boolean =
37+
nullIsSameAsDefault || (metadata.valueNulls == Nulls.SKIP)
38+
3539
override fun createFromObjectWith(
3640
ctxt: DeserializationContext,
3741
props: Array<out SettableBeanProperty>,
@@ -70,7 +74,7 @@ internal class KotlinValueInstantiator(
7074
val paramType = paramDef.type
7175
var paramVal = if (!isMissing || paramDef.isPrimitive() || jsonProp.hasInjectableValueId()) {
7276
val tempParamVal = buffer.getParameter(jsonProp)
73-
if (nullIsSameAsDefault && tempParamVal == null && paramDef.isOptional) {
77+
if (tempParamVal == null && jsonProp.skipNulls() && paramDef.isOptional) {
7478
return@forEachIndexed
7579
}
7680
tempParamVal
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.fasterxml.jackson.module.kotlin.test.github
2+
3+
import com.fasterxml.jackson.annotation.JsonSetter
4+
import com.fasterxml.jackson.annotation.Nulls
5+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
6+
import com.fasterxml.jackson.module.kotlin.readValue
7+
import org.junit.Test
8+
import kotlin.test.assertEquals
9+
10+
class Github526 {
11+
data class D(@JsonSetter(nulls = Nulls.SKIP) val v: Int = -1)
12+
13+
@Test
14+
fun test() {
15+
val mapper = jacksonObjectMapper()
16+
val d = mapper.readValue<D>("""{"v":null}""")
17+
18+
assertEquals(-1, d.v)
19+
}
20+
}

0 commit comments

Comments
 (0)