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

Field from a Kotlin data class gets ignored if it's prefixed with "is" but not a boolean. #561

Closed
Arrem opened this issue Apr 13, 2022 · 3 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@Arrem
Copy link

Arrem commented Apr 13, 2022

Describe the bug
When attempting to serialize a Kotlin data class which has a property prefixed with "is", but not a boolean, Jackson will ignore the property.

Version information
Tested on jackson-databind:2.13.1

To Reproduce
If you have a way to reproduce this with:

  1. In a Kotlin project, create a dummy data class with a field that is not a boolean but is prefixed with is.
  2. Create a Jackson object mapper and attempt to write the object to a string.
  3. The property prefixed with is will be ignored.

For example, the following snippet is self-contained:

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

data class Person(
    val name: String,
    val isAlive: Int,
    val isDead: Boolean,
)

fun main() {
    val mapper = jacksonObjectMapper()

    val actual = mapper.writeValueAsString(Person("Dave", 0, true))
    val expected = """{"name":"Dave","isAlive":0,"isDead":true}"""

    assert(expected == actual) { "Expected $expected but got $actual." }
}

Produces

Exception in thread "main" java.lang.AssertionError: Expected {"name":"Dave","isAlive":0,"isDead":true} but got {"name":"Dave","isDead":true}.

Expected behavior
All three properties should be included, but the produced output is {"name":"Dave","isDead":true}, with the isAlive property ignored.

Additional context
I am unfamiliar with the internals of Jackson but I've found the following two pieces of information that might be helpful:

  • When looking at the generated bytecode, Kotlin generates a getter named isAlive for the integer property, instead of getIsAlive which might be expected by Jackson. Manually adding in a getter of that format to the class (fun getIsAlive() = isAlive) fixes the wrong behavior.
  • In BeanSerializerFactory.java, on line 595, properties correctly contains all three properties, but these are then removed by the call to removeIgnorableTypes, since property.getAccessor() on line 763 returns null for the offending property. I did not investigate further to see how the getters in POJOPropertyBuilder are computed, so I am unsure what the root cause is.
@Arrem Arrem added the to-evaluate Issue that has been received but not yet evaluated label Apr 13, 2022
@Arrem
Copy link
Author

Arrem commented Apr 13, 2022

I just noticed that the Kotlin module is a separate repository, so it would be nice if a maintainer could transfer the issue.

@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-databind Apr 21, 2022
@Richie94
Copy link
Contributor

Richie94 commented Oct 4, 2022

See #575

@k163377
Copy link
Contributor

k163377 commented Mar 17, 2023

I verified on branch 2.15 and the submitted test case seems to be fixed.
This is probably a duplicate of #340, which appears to have been fixed in #641.

Therefore, this issue is closed.

@k163377 k163377 closed this as completed Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

3 participants