Skip to content
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

Prefixes for nested JsonUnwrapped fields are not concatenated #539

Closed
Blackmorse opened this issue Jan 12, 2022 · 3 comments
Closed

Prefixes for nested JsonUnwrapped fields are not concatenated #539

Blackmorse opened this issue Jan 12, 2022 · 3 comments
Labels

Comments

@Blackmorse
Copy link

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 Blackmorse added the bug label Jan 12, 2022
@Blackmorse
Copy link
Author

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

@dionmagnus
Copy link

Confirm.
The bug is still actual for

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

@k163377
Copy link
Contributor

k163377 commented Sep 30, 2023

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
FasterXML/jackson-databind#2461

@k163377 k163377 closed this as completed Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants