-
-
Notifications
You must be signed in to change notification settings - Fork 178
@JsonCreator that worked in 2.17.3 no longer works in 2.19.0 #997
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
Comments
I couldn't work out what the Java equivalent would be to see if it were a databind change. |
I do not understand how you expect this Perhaps due to the import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class GitHub997 {
data class Limit(
@field:JsonValue
val limit: Long?,
) {
companion object {
val UNLIMITED = Limit(null)
@JvmStatic
@JsonCreator
fun fromNullable(limit: Long?): Limit = if (limit == null) UNLIMITED else Limit(limit)
}
}
data class Wrapper(
val limit: Limit = Limit.Companion.UNLIMITED,
)
@Test
fun test() {
assertEquals(Wrapper(), jacksonObjectMapper().readValue<Wrapper>("{}"))
}
} |
I think I've got a minimum viable reproducer... For me this passes with import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonSetter
import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.annotation.Nulls.AS_EMPTY
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import mocklab.GitHub997.Limit.Companion.UNLIMITED
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class GitHub997 {
data class Limit(
@field:JsonValue
val limit: Long?,
) {
companion object {
val UNLIMITED = Limit(null)
@JvmStatic
@JvmOverloads
@JsonCreator
fun fromNullable(limit: Long? = null): Limit = if (limit == null) UNLIMITED else Limit(limit)
}
}
data class Wrapper(
@JsonSetter(nulls = AS_EMPTY)
val limit: Limit = UNLIMITED,
)
val objectMapper = jacksonObjectMapper()
@Test
fun testWithNoLimit() {
val deserialized = objectMapper.readValue<Wrapper>("""{ }""")
assertEquals(UNLIMITED, deserialized.limit)
}
@Test
fun testWithNullLimit() {
val deserialized = objectMapper.readValue<Wrapper>("""{ "limit": null }""")
assertEquals(UNLIMITED, deserialized.limit)
}
@Test
fun testWithSpecificLimit() {
val deserialized = objectMapper.readValue<Wrapper>("""{ "limit": 12 }""")
assertEquals(Wrapper(limit = Limit(12)), deserialized)
}
} |
Thank you for submitting a detailed test case. It is true that a destructive change in behavior has occurred, but I personally feel that the problem is a deficiency in the definition of class GitHub997 {
data class Limit(
@JsonValue
val limit: Long?,
) {
companion object {
val UNLIMITED = Limit(null)
@JvmStatic
@JsonCreator
fun creator(limit: Long): Limit = Limit(limit)
}
}
data class Wrapper(
@JsonSetter(nulls = Nulls.SKIP)
val limit: Limit = Limit.UNLIMITED,
)
val objectMapper = jacksonObjectMapper()
@Test
fun testWithNoLimit() {
val deserialized = objectMapper.readValue<Wrapper>("""{ }""")
assertEquals(Limit.UNLIMITED, deserialized.limit)
}
@Test
fun testWithNullLimit() {
val deserialized = objectMapper.readValue<Wrapper>("""{ "limit": null }""")
assertEquals(Limit.UNLIMITED, deserialized.limit)
}
@Test
fun testWithSpecificLimit() {
val deserialized = objectMapper.readValue<Wrapper>("""{ "limit": 12 }""")
assertEquals(Wrapper(limit = Limit(12)), deserialized)
}
} First, if you want to use default arguments when the value is |
Yup, that worked, thanks. Would you like me to close this as won't fix then? |
Uh oh!
There was an error while loading. Please reload this page.
Search before asking
Describe the bug
A
null
json property mapped to this class:used to work in 2.17.3, returning the
UNLIMITED
instance, if the property on the parent class was marked@JsonSetter(nulls = AS_EMPTY)
.In 2.19.0 it throws this exception:
To Reproduce
Expected behavior
Prints
Wrapper(limit=unlimited)
Versions
Kotlin: 2.1.21
Jackson-module-kotlin: 2.19.0
Jackson-databind: 2.19.0
Additional context
N/A
The text was updated successfully, but these errors were encountered: