From c9d70926ec33d71a3af3fec86c0875126ce01ec8 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 9 Dec 2023 19:46:33 +0900 Subject: [PATCH 1/3] Fix JacksonInject priority fixes https://github.com/FasterXML/jackson-module-kotlin/issues/722 --- .../module/kotlin/KotlinValueInstantiator.kt | 22 +++++-------------- .../module/kotlin/test/github/Github722.kt | 4 ++-- 2 files changed, 7 insertions(+), 19 deletions(-) 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 8c473717d..bc167abc1 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt @@ -11,7 +11,6 @@ import com.fasterxml.jackson.databind.deser.ValueInstantiators import com.fasterxml.jackson.databind.deser.impl.NullsAsEmptyProvider import com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator -import com.fasterxml.jackson.databind.exc.MismatchedInputException import java.lang.reflect.TypeVariable import kotlin.reflect.KParameter import kotlin.reflect.KType @@ -67,24 +66,20 @@ internal class KotlinValueInstantiator( val jsonProp = props[idx] val isMissing = !buffer.hasParameter(jsonProp) - if (isMissing && paramDef.isOptional) { - return@forEachIndexed - } - val paramType = paramDef.type - var paramVal = if (!isMissing || paramDef.isPrimitive() || jsonProp.hasInjectableValueId()) { + var paramVal = if (!isMissing || jsonProp.hasInjectableValueId()) { val tempParamVal = buffer.getParameter(jsonProp) if (tempParamVal == null && jsonProp.skipNulls() && paramDef.isOptional) { return@forEachIndexed } tempParamVal } else { - if(paramType.isMarkedNullable) { + when { + paramDef.isOptional -> return@forEachIndexed // do not try to create any object if it is nullable and the value is missing - null - } else { + paramType.isMarkedNullable -> null // to get suitable "missing" value provided by deserializer - jsonProp.valueDeserializer?.getAbsentValue(ctxt) + else -> jsonProp.valueDeserializer?.getAbsentValue(ctxt) } } @@ -157,13 +152,6 @@ internal class KotlinValueInstantiator( } - private fun KParameter.isPrimitive(): Boolean { - return when (val javaType = type.javaType) { - is Class<*> -> javaType.isPrimitive - else -> false - } - } - private fun SettableBeanProperty.hasInjectableValueId(): Boolean = injectableValueId != null } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github722.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github722.kt index 483f1caa4..344abb3d1 100644 --- a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github722.kt +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github722.kt @@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.databind.InjectableValues import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import kotlin.math.exp import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotEquals @@ -43,8 +44,7 @@ class Github722 { .with(InjectableValues.Std(injectValues)) .readValue("{}") - assertNotEquals(result, expected, "GitHubXXX fixed.") - assertEquals(FailingDto(), result) + assertEquals(expected, result) } data class WithoutDefaultValue( From 86e17005145d8ef1db2bdc65bc0a113e57dab10c Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 9 Dec 2023 19:51:58 +0900 Subject: [PATCH 2/3] Add FailNullForPrimitive test --- .../kotlin/test/FailNullForPrimitiveTest.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt new file mode 100644 index 000000000..40fe0eccd --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/FailNullForPrimitiveTest.kt @@ -0,0 +1,32 @@ +package com.fasterxml.jackson.module.kotlin.test + +import com.fasterxml.jackson.databind.DeserializationFeature +import com.fasterxml.jackson.databind.exc.MismatchedInputException +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import junit.framework.TestCase.assertEquals +import org.junit.Assert.assertThrows +import kotlin.test.Test + +class FailNullForPrimitiveTest { + data class Dto( + val foo: Int, + val bar: Int? + ) + + @Test + fun test() { + val mapper = jacksonObjectMapper() + .enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) + + assertThrows(MismatchedInputException::class.java) { + mapper.readValue("{}") + } + + assertThrows(MismatchedInputException::class.java) { + mapper.readValue("""{"foo":null}""") + } + + assertEquals(Dto(0, null), mapper.readValue("""{"foo":0}""")) + } +} From 389c2846afdc62927b4a81d6998234f219485351 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 9 Dec 2023 20:02:53 +0900 Subject: [PATCH 3/3] Update release notes wrt #738 --- release-notes/CREDITS-2.x | 1 + release-notes/VERSION-2.x | 1 + 2 files changed, 2 insertions(+) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index fa5f1a0a6..ca9d0d676 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -18,6 +18,7 @@ Contributors: # 2.17.0 (not yet released) WrongWrong (@k163377) +* #738: Fix JacksonInject priority. * #732: SequenceSerializer removed. * #727: Fixed overriding findCreatorAnnotation instead of hasCreatorAnnotation diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 66a3e8a03..628d8be79 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -18,6 +18,7 @@ Co-maintainers: 2.17.0 (not yet released) +#738: JacksonInject is now preferred over the default argument(fixes #722). #732: SequenceSerializer removed. #727: Fixed overriding findCreatorAnnotation instead of hasCreatorAnnotation.