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

View inclusion on private fields is not used for inherited fields #620

Closed
frederic-kneier opened this issue Jul 27, 2022 · 5 comments
Closed
Labels
need-test-case To work on issue, a reproduction (ideally unit test) needed

Comments

@frederic-kneier
Copy link

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 frederic-kneier added the to-evaluate Issue that has been received but not yet evaluated label Jul 27, 2022
@cowtowncoder
Copy link
Member

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.

@frederic-kneier
Copy link
Author

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.

@cowtowncoder
Copy link
Member

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 cowtowncoder added need-test-case To work on issue, a reproduction (ideally unit test) needed and removed to-evaluate Issue that has been received but not yet evaluated labels Jul 31, 2022
@frederic-kneier
Copy link
Author

Sorry for the long delay. I took a deeper dive into the problem and created a implementation in java:

public class Java {

    public interface View {}

    public static class B {
        @JsonView(View.class)
        private String b = "b";


        public String getB() {
            return b;
        }

        public void setB(String b) {
            this.b = b;
        }
    }

    public static class A extends B {
        @JsonView(View.class)
        private String a = "a";


        public String getA() {
            return a;
        }

        public void setA(String a) {
            this.a = a;
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        final var mapper = new ObjectMapper().disable(MapperFeature.DEFAULT_VIEW_INCLUSION);

        System.out.println(mapper.writerWithView(View.class).writeValueAsString(new A()));
    }

}

This solution works. It also works if I apply the View annotation to the getter functions in Kotlin so the problem seem to be with inheritence of the View annoation for private fields in Kotlin only.

Can this ticket be transfered to the Kotlin module?

@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-databind Jan 28, 2023
@k163377
Copy link
Contributor

k163377 commented Apr 2, 2023

It is closed as a duplicate of #658.

@k163377 k163377 closed this as completed Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-test-case To work on issue, a reproduction (ideally unit test) needed
Projects
None yet
Development

No branches or pull requests

3 participants