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 being honored when using an open class extending a constructor #638

Closed RafaRuiz closed 1 year ago

RafaRuiz commented 1 year ago

Describe the bug I'm using an open class to directly construct a model or give a morphism of another. When adding the @JsonProperty annotation to one variable, it's being honored when I directly construct this open class but not when I'm extending it as the morphism itself

Version information 2.14.1

To Reproduce With Spring Boot:

@CrossOrigin(origins = ["*"])
@RestController
@RequestMapping("/test")
class TestController(
) {
    open class TestModel<T>(
        @JsonProperty("test_name")
        val testName: String,
        @JsonProperty("info")
        val info: T,
    )

    class TestModelWrapper<T>(info: T) : TestModel<T>(
        testName = "wrapper",
        info = info
    )

    @GetMapping
    fun get(): Mono<List<TestModel<out Any>>> {
        val testModel1 = TestModel(
            testName = "fooTest",
            info = "testModel1"
        )

        val testModel2Wrapper = TestModelWrapper(
            info = 12345
        )

        return listOf(
            testModel1,
            testModel2Wrapper
        ).toMono()
    }
}

Result

[
  {
    "test_name": "fooTest",
    "info": "testModel1"
  },
  {
    "testName": "wrapper",
    "info": 12345
  }
]

Expected behavior The second result should contain the key test_name, not testName:

[
  {
    "test_name": "fooTest",
    "info": "testModel1"
  },
  {
    "test_name": "wrapper",
    "info": 12345
  }
]
pjfanning commented 1 year ago

Can you do a test scenario that just uses Jackson APIs? Jackson team are not here to debug Spring Boot.

k163377 commented 1 year ago

The root of the problem is summarized in the comment below. https://github.com/FasterXML/jackson-module-kotlin/issues/151#issuecomment-1453804338

Specify field or getter when annotating.

This issue is closed as a duplicate.