You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I searched in the issues and found nothing similar.
I searched in the issues of databind and other modules used and found nothing similar.
I have confirmed that the problem only occurs when using Kotlin.
Describe the bug
The example below will not deserialize a single-field data class with a default value:
funmain() {
val mapper = jacksonObjectMapper()
val testing =Testing()
val json = mapper.writeValueAsString(testing)
println(json) // {} works correctlyprintln(mapper.readValue<Testing>(json)) // fails
}
data classTesting(
privatevalrandom:Random = Random
)
But if I add another field to class, it works as expected:
funmain() {
val mapper = jacksonObjectMapper()
val testing =Testing()
val json = mapper.writeValueAsString(testing)
println(json) // {"name":"John Doe"} works correctlyprintln(mapper.readValue<Testing>(json)) // Testing(name=John Doe, random=kotlin.random.Random$Default@2e77b8cf)
}
data classTesting(
valname:String = "John Doe",
privatevalrandom:Random = Random
)
I have also noticed that it works with a single primitive field:
funmain() {
val mapper = jacksonObjectMapper()
val testing =Testing()
val json = mapper.writeValueAsString(testing)
println(json) // {} works correctlyprintln(mapper.readValue<Testing>(json)) // Testing(name=John Doe)
}
data classTesting(
privatevalname:String = "John Doe"
)
Another example is with @JacksonInject:
funmain() {
val mapper =ObjectMapper()
.registerModule(kotlinModule {
enable(KotlinFeature.NullIsSameAsDefault)
})
.setInjectableValues(InjectableValues.Std().addValue(String::class.java, "Example"))
val testing = mapper.readValue<Testing>("{}") // failsprintln(testing.id)
}
data classTesting(
@JacksonInject valid:String
)
But adding another field do the data class fixes the problem:
funmain() {
val mapper =ObjectMapper()
.registerModule(kotlinModule {
enable(KotlinFeature.NullIsSameAsDefault)
})
.setInjectableValues(InjectableValues.Std().addValue(String::class.java, "Example"))
val testing = mapper.readValue<Testing>("{}") // worksprintln(testing.id) // Example
}
data classTesting(
valname:String = "John Doe",
@JacksonInject valid:String
)
Search before asking
Describe the bug
The example below will not deserialize a single-field data class with a default value:
But if I add another field to class, it works as expected:
I have also noticed that it works with a single primitive field:
Another example is with
@JacksonInject
:But adding another field do the data class fixes the problem:
To Reproduce
No response
Expected behavior
No response
Versions
Kotlin: 1.9.20
Jackson-module-kotlin: 2.16.0
Jackson-databind: 2.16.0
Additional context
No response
The text was updated successfully, but these errors were encountered: