Skip to content

Commit fc47a94

Browse files
authored
Merge pull request #723 from k163377/add-inject-test
Add test for #722
2 parents 16e523a + 07552a5 commit fc47a94

File tree

1 file changed

+67
-0
lines changed
  • src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.fasterxml.jackson.module.kotlin.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 com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
import kotlin.test.assertNotEquals
12+
13+
class Github722 {
14+
data class FailingDto @JsonCreator constructor(
15+
@JacksonInject("foo")
16+
@JsonProperty("foo")
17+
val foo: Int = 100,
18+
@JacksonInject("bar")
19+
@JsonProperty("bar")
20+
val bar: Int? = 200
21+
)
22+
23+
val injectValues = mapOf("foo" to 1, "bar" to 2)
24+
val expected = FailingDto(1, 2)
25+
26+
@Test
27+
fun onPlainMapper() {
28+
// Succeeds in plain mapper
29+
val plainMapper = ObjectMapper()
30+
assertEquals(
31+
expected,
32+
plainMapper.readerFor(FailingDto::class.java)
33+
.with(InjectableValues.Std(injectValues))
34+
.readValue("{}")
35+
)
36+
}
37+
38+
@Test
39+
fun failing() {
40+
// The kotlin mapper uses the Kotlin default value instead of the Inject value.
41+
val kotlinMapper = jacksonObjectMapper()
42+
val result = kotlinMapper.readerFor(FailingDto::class.java)
43+
.with(InjectableValues.Std(injectValues))
44+
.readValue<FailingDto>("{}")
45+
46+
assertNotEquals(result, expected, "GitHubXXX fixed.")
47+
assertEquals(FailingDto(), 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)