FasterXML / jackson-module-kotlin

Module that adds support for serialization/deserialization of Kotlin (http://kotlinlang.org) classes and data classes.
Apache License 2.0
1.12k stars 175 forks source link

JsonProperty not respected when using custom serializer with inline class #748

Closed markitovtr1 closed 8 months ago

markitovtr1 commented 8 months ago

Search before asking

Describe the bug

First, I was not aware where to report this, so I chose this repo.

I'm aware that inline classes are not supported out-of-the-box yet, but this seems different than what is being discussed in issue 199, so I decided to create this new issue.

When annotating both @JsonSerialize and @JsonProperty in an inline class property inside data class, for some reason, all JsonProperty annotations in that class are not respected.

Scenarios I tested:

  1. In my original code, I annotated inline class with JsonSerialize and JsonDeserlize.
  2. I tested annotating property directly and it had the same result.

Scenarios I tested against:

  1. Use custom serialization on a regular property: works as expected
  2. Use a normal class as a wrapper instead of inline class: works as expected
  3. Use directly wrapped value: works as expected

To Reproduce

I created a repo to reproduce this bug. I used spring-boot-web to be able to use @JsonTest with JacksonTester to make the test clearer. Just run tests with ./gradlew test

Just for the sake of documentation, assume SampleInline is a BigDecimal inline class and is annotated with a correct JsonSerializer that just does a toString in internal big decimal.

data class WithInlineClass(
  @JsonProperty("testField1") val otherName1: SampleInline,
  @JsonProperty("testField2") val otherName2: OtherClass,
)

data class OtherClass(
  @JsonProperty("foo1") val bar1: Int,
  @JsonProperty("foo2") val bar2: String,
)

Expected behavior

I expect that to seralize to something like:

{"testField1":1,"testField2":{"foo1":1,"foo2":"2"}}

But it is actually serializing to this:

{"otherName1":1,"otherName2":{"foo1":1,"foo2":"2"}}

Versions

Kotlin: Jackson-module-kotlin: Jackson-databind:

Additional context

In case this is expected not to work, as value class support is still WIP, no problem closing this issue. Just wanted to share this.

k163377 commented 8 months ago

see #651

markitovtr1 commented 8 months ago

Hi!

Thanks for pointing to that issue. I definitely missed it. I'll I'll add my bug sample as a comment there!