-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Deserialization behavior change with Records, @JsonCreator
and @JsonValue
between 2.17 and 2.18
#4724
Comments
This also happens in Kotlin. It seems to have something to do with data classes that have secondary constructors. This test reproduces it: import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import java.util.UUID
import java.util.UUID.randomUUID
data class MyThing(val id: UUID, val status: ThingStatus, val child: ChildThing) {
constructor(status: ThingStatus, child: ChildThing) : this(randomUUID(), status, child)
}
enum class ThingStatus {
COOL,
NOT_COOL,
}
data class ChildThing(val id: UUID)
class MyThingTest {
private val json = jacksonObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
@Test
fun fromJson() {
assertDoesNotThrow {
json.readValue<MyThing>(
"""
{
"id": "${randomUUID()}",
"status": "NOT_COOL",
"child": {
"id": "${randomUUID()}"
}
}
""".trimIndent(),
)
}
}
} That test passes on 2.17; fails on 2.18. |
That looks like a bug for sure: I would expect:
One to note tho: it may (altho should not!) be necessary to use
if the idea is to deserialize from JSON String (instead of single-property JSON Object). |
One quick thing to check: would behavior be different if I also assume this same case would work for regular POJOs. |
@ianfp Let's not assume it's same thing, your case looks very different from OP's. You may want to file separate issue against jackson-module-kotlin wrt your issue. |
|
What we know:
While #2 is out of their control, a (desperate) user can do something with #1 to work around the regression: public record Something(...) {
@JsonCreator(mode = Mode.DISABLED)
public Something {
...
}
...
} |
Do you think there is chance that solution is as simple as swapping back the precedence, @yihtserns? |
Changing precedence not intentional; but this wasn't a case covered by unit tests it seems. Records have "default" or "preferred" constructor, but that should have lower precedence than explicitly annotated constructor or factory method. |
Yes, unless drastic changes were needed, fix for 2.18.1 it all possible. |
@JsonCreator
and @JsonValue
between 2.17 and 2.18
Search before asking
Describe the bug
Using both JsonCreator and JsonValue annotations with Java Records makes ObjectMapper seemingly skip single-arg static JsonCreator method and try to contract instance directly. I couldn't find any hint that this was a planned change.
Version Information
2.18.0
Reproduction
The following code works with 2.17.2, but fails with 2.18.0
Removing JsonValue annotation makes deserialization to work again.
Expected behavior
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: