jackson-module-kotlin
jackson-module-kotlin copied to clipboard
Serialization failure for fields after updating to Jackson 2.18.0
Search before asking
- [X] I searched in the issues and found nothing similar.
- [X] I searched in the issues of databind and other modules used and found nothing similar.
- [X] I have confirmed that the problem only occurs when using Kotlin.
Describe the bug
After upgrading Jackson from version 2.17.2 to 2.18.0, the serialization process for SecondObject and ThirdObject has completely broken. The fields are either missing or partially serialized, making the resulting JSON incomplete.
I expected that only the field order might change, but now fields that were present in the input JSON are missing from the serialized output entirely.
To Reproduce
Tested objects:
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE
)
class FirstObject {
@field:JsonAnySetter
@field:JsonAnyGetter
val data: Map<String, Any?> = LinkedHashMap()
val transactionId: String
get() = data["transactionId"] as String
}
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE
)
class SecondObject(
val transactionId: String
) {
@field:JsonAnySetter
@field:JsonAnyGetter
val data: Map<String, Any?> = LinkedHashMap()
}
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE
)
class ThirdObject(
val transactionId: String,
@field:JsonAnySetter
@field:JsonAnyGetter
val data: Map<String, Any?> = LinkedHashMap()
)
Input JSON for tests:
{
"b": 2,
"a": 1,
"transactionId": "test",
"c": [
{
"id": "3",
"value": "c"
},
{
"id": "1",
"value": "a"
},
{
"id": "2",
"value": "b"
}
]
}
Test Case:
Here’s a test case to reproduce the issue. The following Kotlin test serializes and deserializes three different classes (FirstObject, SecondObject, and ThirdObject), comparing the serialized JSON with the original input JSON. JacksonSortingTest.kt branch 2.18.0.
For FirstObject, however, the test passed successfully. All fields were present in the serialized JSON, and they appeared in the expected order, as in the original input JSON.
Failing Test Results:
Expected:
{
"b": 2,
"a": 1,
"transactionId": "test",
"c": [
{
"id": "3",
"value": "c"
},
{
"id": "1",
"value": "a"
},
{
"id": "2",
"value": "b"
}
]
}
For SecondObject was:
{
"transactionId": "test",
"c": [
{
"id": "3",
"value": "c"
},
{
"id": "1",
"value": "a"
},
{
"id": "2",
"value": "b"
}
]
}
For ThirdObject was:
{
"transactionId": "test"
}
Repository for Reproduction: You can find a repository with the full reproduction of the issue at jackson-databind-sorting-issue - branch 2.18.0.
Expected behavior
After updating from Jackson 2.17.2 to 2.18.0, the serialization for SecondObject and ThirdObject should work as it did in the previous version (except for field ordering after deserialization and serialization). The expected JSON output after deserialization and serialization should contain all fields, with their values exactly as they are in the original input, regardless of field order.
Versions
Kotlin: 2.0.0 Jackson-module-kotlin: 2.18.0 Jackson-databind: 2.18.0
Additional context
This issue is a duplicate of 4752. I'm trying to reproduce it with Java, but it seems like all the described cases are specific to Kotlin