-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #723 from k163377/add-inject-test
Add test for #722
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github722.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.fasterxml.jackson.module.kotlin.test.github | ||
|
||
import com.fasterxml.jackson.annotation.JacksonInject | ||
import com.fasterxml.jackson.annotation.JsonCreator | ||
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.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotEquals | ||
|
||
class Github722 { | ||
data class FailingDto @JsonCreator constructor( | ||
@JacksonInject("foo") | ||
@JsonProperty("foo") | ||
val foo: Int = 100, | ||
@JacksonInject("bar") | ||
@JsonProperty("bar") | ||
val bar: Int? = 200 | ||
) | ||
|
||
val injectValues = mapOf("foo" to 1, "bar" to 2) | ||
val expected = FailingDto(1, 2) | ||
|
||
@Test | ||
fun onPlainMapper() { | ||
// Succeeds in plain mapper | ||
val plainMapper = ObjectMapper() | ||
assertEquals( | ||
expected, | ||
plainMapper.readerFor(FailingDto::class.java) | ||
.with(InjectableValues.Std(injectValues)) | ||
.readValue("{}") | ||
) | ||
} | ||
|
||
@Test | ||
fun failing() { | ||
// The kotlin mapper uses the Kotlin default value instead of the Inject value. | ||
val kotlinMapper = jacksonObjectMapper() | ||
val result = kotlinMapper.readerFor(FailingDto::class.java) | ||
.with(InjectableValues.Std(injectValues)) | ||
.readValue<FailingDto>("{}") | ||
|
||
assertNotEquals(result, expected, "GitHubXXX fixed.") | ||
assertEquals(FailingDto(), result) | ||
} | ||
|
||
data class WithoutDefaultValue( | ||
@JacksonInject("foo") | ||
val foo: Int, | ||
@JacksonInject("bar") | ||
val bar: Int? | ||
) | ||
|
||
@Test | ||
fun withoutDefaultValue() { | ||
val kotlinMapper = jacksonObjectMapper() | ||
val result = kotlinMapper.readerFor(WithoutDefaultValue::class.java) | ||
.with(InjectableValues.Std(injectValues)) | ||
.readValue<WithoutDefaultValue>("{}") | ||
|
||
// If there is no default value, the problem does not occur. | ||
assertEquals(WithoutDefaultValue(1, 2), result) | ||
} | ||
} |