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

Private getter with different return type hides property #341

Open usulkies opened 4 years ago

usulkies commented 4 years ago

Describe the bug When a data class contains a private getter getProp with return type TypeA and there's a class property named prop with type TypeB the JSON form of the class will not contain either of the properties.

To Reproduce

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule

fun main() {
  val mapper = ObjectMapper()
      .registerModule(KotlinModule())
  println("TestMe1: " + mapper.writeValueAsString(TestMe1(
      property1 = "prop1",
      property2 = "prop2"
  )))
}

data class TestMe1(
    val property1: String,
    val property2: String
){

  private fun getProperty1(): Int = 3
}

Expected behavior TestMe1: {"property1": "prop1", "property2":"prop2"}

Actual behavior TestMe1: {"property2":"prop2"}

Versions Kotlin: 1.3.72 Jackson-module-kotlin: 2.10.4 (maybe before) Jackson-databind: 2.10.4 (maybe before)

usulkies commented 4 years ago

Workaround: Annotate the property with @field:JsonProperty("property1")

usulkies commented 4 years ago

From my check, it has started in version 2.9.7. I experienced it only now through Drpowizard bump 2.0.8 -> 2.0.9

k163377 commented 1 year ago

I tried to reproduce it, but it seemed to randomly turn into TestMe1: {"property1": "prop1", "property2": "prop2"} or TestMe1: {"property2": "prop2"}.(!?) To elaborate, Class.declaredMethods returned a random order and private getter came first, databind would ignore that property.

It appears to be very difficult to fix this behavior in jackson-module-kotlin. On the other hand, it also seems difficult to solve this problem in data-bind.

Please see the following issue for more information https://youtrack.jetbrains.com/issue/KT-23434


@cowtowncoder May I close this issue? I don't think it can be resolved until Kotlin takes care of it.