Skip to content
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

Add test for #722 #723

Merged
merged 1 commit into from
Nov 4, 2023
Merged
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
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)
}
}