Skip to content

Commit 0a594a7

Browse files
authored
Merge pull request #193 from ProjectMapK/github-722
Fixed `JacksonInject` to take precedence over default arguments.
2 parents cf189eb + 477d414 commit 0a594a7

File tree

2 files changed

+71
-8
lines changed
  • src
    • main/kotlin/io/github/projectmapk/jackson/module/kogera/deser/valueInstantiator
    • test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github

2 files changed

+71
-8
lines changed

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/deser/valueInstantiator/KotlinValueInstantiator.kt

+4-8
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,17 @@ internal class KotlinValueInstantiator(
6666
val jsonProp = props[idx]
6767
val isMissing = !buffer.hasParameter(jsonProp)
6868

69-
if (isMissing && paramDef.isOptional) {
70-
return@forEachIndexed
71-
}
72-
7369
var paramVal = if (!isMissing || jsonProp.hasInjectableValueId()) {
7470
buffer.getParameter(jsonProp).apply {
7571
if (this == null && jsonProp.skipNulls() && paramDef.isOptional) return@forEachIndexed
7672
}
7773
} else {
78-
if (paramDef.isNullable) {
74+
when {
75+
paramDef.isOptional -> return@forEachIndexed
7976
// do not try to create any object if it is nullable and the value is missing
80-
null
81-
} else {
77+
paramDef.isNullable -> null
8278
// to get suitable "missing" value provided by deserializer
83-
jsonProp.valueDeserializer?.getAbsentValue(ctxt)
79+
else -> jsonProp.valueDeserializer?.getAbsentValue(ctxt)
8480
}
8581
}
8682

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.github.projectmapk.jackson.module.kogera.zPorted.test.github
2+
3+
import com.fasterxml.jackson.annotation.JacksonInject
4+
import com.fasterxml.jackson.annotation.JsonCreator
5+
import com.fasterxml.jackson.annotation.JsonProperty
6+
import com.fasterxml.jackson.databind.InjectableValues
7+
import com.fasterxml.jackson.databind.ObjectMapper
8+
import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper
9+
import org.junit.jupiter.api.Assertions.assertEquals
10+
import org.junit.jupiter.api.Test
11+
12+
private class Github722 {
13+
data class FailingDto @JsonCreator constructor(
14+
@JacksonInject("foo")
15+
@JsonProperty("foo")
16+
val foo: Int = 100,
17+
@JacksonInject("bar")
18+
@JsonProperty("bar")
19+
val bar: Int? = 200
20+
)
21+
22+
val injectValues = mapOf("foo" to 1, "bar" to 2)
23+
val expected = FailingDto(1, 2)
24+
25+
@Test
26+
fun onPlainMapper() {
27+
// Succeeds in plain mapper
28+
val plainMapper = ObjectMapper()
29+
assertEquals(
30+
expected,
31+
plainMapper.readerFor(FailingDto::class.java)
32+
.with(InjectableValues.Std(injectValues))
33+
.readValue("{}")
34+
)
35+
}
36+
37+
@Test
38+
fun failing() {
39+
// The kotlin mapper uses the Kotlin default value instead of the Inject value.
40+
val kotlinMapper = jacksonObjectMapper()
41+
val result = kotlinMapper.readerFor(FailingDto::class.java)
42+
.with(InjectableValues.Std(injectValues))
43+
.readValue<FailingDto>("{}")
44+
45+
// fixed
46+
// assertNotEquals(result, expected, "GitHubXXX fixed.")
47+
assertEquals(expected, result)
48+
}
49+
50+
data class WithoutDefaultValue(
51+
@JacksonInject("foo")
52+
val foo: Int,
53+
@JacksonInject("bar")
54+
val bar: Int?
55+
)
56+
57+
@Test
58+
fun withoutDefaultValue() {
59+
val kotlinMapper = jacksonObjectMapper()
60+
val result = kotlinMapper.readerFor(WithoutDefaultValue::class.java)
61+
.with(InjectableValues.Std(injectValues))
62+
.readValue<WithoutDefaultValue>("{}")
63+
64+
// If there is no default value, the problem does not occur.
65+
assertEquals(WithoutDefaultValue(1, 2), result)
66+
}
67+
}

0 commit comments

Comments
 (0)