jackson-module-kotlin icon indicating copy to clipboard operation
jackson-module-kotlin copied to clipboard

Prefixes for nested JsonUnwrapped fields are not concatenated

Open Blackmorse opened this issue 3 years ago • 1 comments

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

Blackmorse avatar Jan 12 '22 13:01 Blackmorse

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"}

Blackmorse avatar Jan 12 '22 14:01 Blackmorse

Confirm. The bug is still actual for

Jackson-module-kotlin: 2.15.2 Jackson-databind: 2.15.2

dionmagnus avatar Sep 28 '23 15:09 dionmagnus

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

k163377 avatar Sep 30 '23 11:09 k163377