Prefixes for nested JsonUnwrapped fields are not concatenated
Describe the bug Given the following classes structure:
data class A (
@get:JsonUnwrapped(prefix = "prefix1.")
val b: B
)
data class B(
@get:JsonUnwrapped(prefix = "prefix2.")
val c: C
)
data class C(
@get:JsonProperty("prop")
val prop: String
)
Trying to serialize this:
ObjectMapper().writeValueAsString(A(B(C("value"))))
Actual result:
{"prefix2.prop":"value"}
Expected result
{"prefix1.prefix2.prop":"value"}
Versions Kotlin: Jackson-module-kotlin: 2.13.1 Jackson-databind: 2.13.1
As a side effect this bug allows to write field with duplicated names
data class A (
@get:JsonUnwrapped(prefix = "prefix1.")
val b1: B,
@get:JsonUnwrapped(prefix = "prefix2.")
val b2: B
)
data class B(
@get:JsonUnwrapped(prefix = "b.")
val c: C,
)
data class C(
@get:JsonProperty("prop")
val prop: String
)
val a = A(
b1 = B(C("val1")),
b2 = B(C("val2"))
)
ObjectMapper.writeValueAsString(a)
The result:
{"b.prop":"val1","b.prop":"val2"}
Confirm. The bug is still actual for
Jackson-module-kotlin: 2.15.2 Jackson-databind: 2.15.2
It has been reproduced in the 2.16 branch.
import com.fasterxml.jackson.annotation.JsonUnwrapped
import com.fasterxml.jackson.databind.ObjectMapper
data class A (
@get:JsonUnwrapped(prefix = "prefix1.")
val b: B
)
data class B(
@get:JsonUnwrapped(prefix = "prefix2.")
val c: C
)
data class C(
val prop: String
)
fun main() {
// -> {"prefix2.prop":"value"}
println(ObjectMapper().writeValueAsString(A(B(C("value")))))
}
However, as shown in the sample code, this is a databind issue since kotlin-module is not used.
This issue is closed as a duplicate of the following
https://github.com/FasterXML/jackson-databind/issues/2461