jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

View inclusion on private fields is not used for inherited fields

Open frederic-kneier opened this issue 1 year ago • 3 comments

Describe the bug Given a class Awith a private field a that extends a class B with a private field b and both fields having the @JsonView annotation the annotation of field b is not processed when serializing with a given view.

Version information 2.13.3

To Reproduce

interface View

open class B(
    @JsonView(View::class)
    val b: String = "b",
)

open class A(
    @JsonView(View::class)
    val a: String = "a",
) : B()

@Test
fun serialize() {
    val mapper = jacksonObjectMapper().apply {
        disable(MapperFeature.DEFAULT_VIEW_INCLUSION)
    }

    println(mapper.writerWithView(View::class.java).writeValueAsString(A()))
}

Expected behavior Serialized output is {"a": "a", "b": "b"} but is {"a": "a"}

Additional context

frederic-kneier avatar Jul 27 '22 14:07 frederic-kneier

Is this Kotlin? If so, I'll need to transfer this to Kotlin module, or, alternatively, if it can be reproduced with plain Java it may remain here.

cowtowncoder avatar Jul 27 '22 17:07 cowtowncoder

This problem is not Kotlin specific. Properties are just a combination of private field and a getter for val and getter+setter for var. In this case the annotation will be added to the private field.

I could transfer the code to Java but the general gist is the same.

frederic-kneier avatar Jul 28 '22 14:07 frederic-kneier

Just because the general idea is the same does not necessarily mean this would not be Kotlin-specific (in the sense I meant it), mostly because Kotlin module's handling of (Kotlin) classes differs in some ways from general databind handling of POJOs.

So: I can move this to Kotlin module for checking there (which is fine), or, if there is Java-only version that shows the issue without Kotlin module, it can be worked on here. The practical difference is that I am not fluent enough with Kotlin to work on Kotlin module (beyond trivial changes) so I can only help with pure Java reproduction. But the Kotlin module maintainers and contributors can help there as necessary.

cowtowncoder avatar Jul 31 '22 20:07 cowtowncoder