Skip to content

Fix JacksonInject priority #738

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 3 commits into from
Dec 9, 2023
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
1 change: 1 addition & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Copy link
Contributor Author

@k163377 k163377 Dec 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#739

Removed the decision because jsonProp.valueDeserializer?.getAbsentValue(ctxt) provides the same functionality as this branch.
The following test confirms that it fails if FAIL_ON_NULL_FOR_PRIMITIVES is set.
https://github.com/FasterXML/jackson-module-kotlin/pull/738/files#diff-d8cb2007657572bfaed68d85f55829d211695e1f181981499dcb6a3a72516472

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)
}
}

Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Dto>("{}")
}

assertThrows(MismatchedInputException::class.java) {
mapper.readValue<Dto>("""{"foo":null}""")
}

assertEquals(Dto(0, null), mapper.readValue<Dto>("""{"foo":0}"""))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,8 +44,7 @@ class Github722 {
.with(InjectableValues.Std(injectValues))
.readValue<FailingDto>("{}")

assertNotEquals(result, expected, "GitHubXXX fixed.")
assertEquals(FailingDto(), result)
assertEquals(expected, result)
}

data class WithoutDefaultValue(
Expand Down