Skip to content

Commit cf890c6

Browse files
authored
Merge pull request #738 from k163377/fix-github722
Fix JacksonInject priority
2 parents 0cd8a7e + 389c284 commit cf890c6

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

release-notes/CREDITS-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Contributors:
1818
# 2.17.0 (not yet released)
1919

2020
WrongWrong (@k163377)
21+
* #738: Fix JacksonInject priority.
2122
* #732: SequenceSerializer removed.
2223
* #727: Fixed overriding findCreatorAnnotation instead of hasCreatorAnnotation
2324

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Co-maintainers:
1818

1919
2.17.0 (not yet released)
2020

21+
#738: JacksonInject is now preferred over the default argument(fixes #722).
2122
#732: SequenceSerializer removed.
2223
#727: Fixed overriding findCreatorAnnotation instead of hasCreatorAnnotation.
2324

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

+5-17
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import com.fasterxml.jackson.databind.deser.ValueInstantiators
1111
import com.fasterxml.jackson.databind.deser.impl.NullsAsEmptyProvider
1212
import com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer
1313
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator
14-
import com.fasterxml.jackson.databind.exc.MismatchedInputException
1514
import java.lang.reflect.TypeVariable
1615
import kotlin.reflect.KParameter
1716
import kotlin.reflect.KType
@@ -67,24 +66,20 @@ internal class KotlinValueInstantiator(
6766
val jsonProp = props[idx]
6867
val isMissing = !buffer.hasParameter(jsonProp)
6968

70-
if (isMissing && paramDef.isOptional) {
71-
return@forEachIndexed
72-
}
73-
7469
val paramType = paramDef.type
75-
var paramVal = if (!isMissing || paramDef.isPrimitive() || jsonProp.hasInjectableValueId()) {
70+
var paramVal = if (!isMissing || jsonProp.hasInjectableValueId()) {
7671
val tempParamVal = buffer.getParameter(jsonProp)
7772
if (tempParamVal == null && jsonProp.skipNulls() && paramDef.isOptional) {
7873
return@forEachIndexed
7974
}
8075
tempParamVal
8176
} else {
82-
if(paramType.isMarkedNullable) {
77+
when {
78+
paramDef.isOptional -> return@forEachIndexed
8379
// do not try to create any object if it is nullable and the value is missing
84-
null
85-
} else {
80+
paramType.isMarkedNullable -> null
8681
// to get suitable "missing" value provided by deserializer
87-
jsonProp.valueDeserializer?.getAbsentValue(ctxt)
82+
else -> jsonProp.valueDeserializer?.getAbsentValue(ctxt)
8883
}
8984
}
9085

@@ -157,13 +152,6 @@ internal class KotlinValueInstantiator(
157152

158153
}
159154

160-
private fun KParameter.isPrimitive(): Boolean {
161-
return when (val javaType = type.javaType) {
162-
is Class<*> -> javaType.isPrimitive
163-
else -> false
164-
}
165-
}
166-
167155
private fun SettableBeanProperty.hasInjectableValueId(): Boolean = injectableValueId != null
168156
}
169157

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fasterxml.jackson.module.kotlin.test
2+
3+
import com.fasterxml.jackson.databind.DeserializationFeature
4+
import com.fasterxml.jackson.databind.exc.MismatchedInputException
5+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
6+
import com.fasterxml.jackson.module.kotlin.readValue
7+
import junit.framework.TestCase.assertEquals
8+
import org.junit.Assert.assertThrows
9+
import kotlin.test.Test
10+
11+
class FailNullForPrimitiveTest {
12+
data class Dto(
13+
val foo: Int,
14+
val bar: Int?
15+
)
16+
17+
@Test
18+
fun test() {
19+
val mapper = jacksonObjectMapper()
20+
.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
21+
22+
assertThrows(MismatchedInputException::class.java) {
23+
mapper.readValue<Dto>("{}")
24+
}
25+
26+
assertThrows(MismatchedInputException::class.java) {
27+
mapper.readValue<Dto>("""{"foo":null}""")
28+
}
29+
30+
assertEquals(Dto(0, null), mapper.readValue<Dto>("""{"foo":0}"""))
31+
}
32+
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github722.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonProperty
66
import com.fasterxml.jackson.databind.InjectableValues
77
import com.fasterxml.jackson.databind.ObjectMapper
88
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
9+
import kotlin.math.exp
910
import kotlin.test.Test
1011
import kotlin.test.assertEquals
1112
import kotlin.test.assertNotEquals
@@ -43,8 +44,7 @@ class Github722 {
4344
.with(InjectableValues.Std(injectValues))
4445
.readValue<FailingDto>("{}")
4546

46-
assertNotEquals(result, expected, "GitHubXXX fixed.")
47-
assertEquals(FailingDto(), result)
47+
assertEquals(expected, result)
4848
}
4949

5050
data class WithoutDefaultValue(

0 commit comments

Comments
 (0)