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

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

Closed
1 task done
markitovtr1 opened this issue Dec 30, 2023 · 2 comments
Closed
1 task done
Labels

Comments

@markitovtr1
Copy link

Search before asking

  • I searched in the issues and found nothing similar.

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
Copy link
Contributor

k163377 commented Dec 31, 2023

see #651

@k163377 k163377 closed this as completed Dec 31, 2023
@markitovtr1
Copy link
Author

Hi!

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

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

2 participants