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

[BUG] The subclass field does not overwrite the parent class #3769

Open
qq31715879 opened this issue Oct 28, 2024 · 6 comments
Open

[BUG] The subclass field does not overwrite the parent class #3769

qq31715879 opened this issue Oct 28, 2024 · 6 comments

Comments

@qq31715879
Copy link

qq31715879 commented Oct 28, 2024

package com.kael.test.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;


@Setter
@Getter
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class TestParentDTO {

    @JsonIgnore
    private String demo;
    private String ddd;
}

package com.kael.test.dto;

import com.jugg.agile.framework.core.util.io.serialize.json.JaJson;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;


@Setter
@Getter
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class TestChildrenDTO extends TestParentDTO {

    private String demo;

    public static void main(String[] args) {
        TestChildrenDTO testChildrenDTO = new TestChildrenDTO();
        testChildrenDTO.setDemo("ddemo");
        testChildrenDTO.setDdd("ddd");

        System.out.println(JaJson.toString(testChildrenDTO));
    }
}

Subclass fields have removed annotations @JsonIgnore


version 1.8.12 can runsuccess
image


but High version failed to run
image

@janrieke
Copy link
Contributor

janrieke commented Oct 28, 2024

Things probably changed when Lombok started copying certain annotation to the setter with 1.18.16, including @JsonIgnore. Jackson can sometimes be confusing when it comes to detecting which annotations belong to a property, because it builds a combination of annotations on the field and its corresponding setters and getters.

With 1.18.12, the @JsonIgnore annotation was only on the field of the superclass, and Jackson correctly ignores only the field from the superclass. However, once Lombok copies it to the setter, I suspect that Jackson considers this setter method as if it belongs to the property defined in the subclass.

I tend towards "this is a Jackson bug", although you could argue that the setter method is overridden and thus it's the same method. On the other hand, @JsonIgnore is not inheritable (in a JLS sense), so Jackson should not consider it for the subclass property. But there is some indication in the JavaDoc of @JsonIgnore (last paragraph) that Jackson in fact considers it as inheritable.

From a lombok POV, this is another example why it's a bad idea to copy those Jackson annotations in the first place. (Except, of course, for builders, because Jackson does not combine the builder's setter methods with the property.)

@qq31715879
Copy link
Author

@janrieke
I looked at the class, and in higher versions of Lombok, @ JsonIgnore will be added to the setDemo method of the parent class
I'm not sure if it's a bug with Lombok or Jackson either, but I'm curious why higher versions of Lombok add @ JsonIgnore to the set method

@qq31715879
Copy link
Author

If @ JsonIgnore must be added to the method, should both the get and set methods be added; However, I personally feel that @ JsonIgnore should not be added to the method. The historical code encountered problems and now I want to upgrade to a higher version of Lombok, but I cannot do it

@janrieke
Copy link
Contributor

You should be able to mitigate by adding @JsonIgnore(false) to the field in the subclass.

@qq31715879
Copy link
Author

It's a bit awkward, the historical code is that the parent class defaults to @ JsonIgnore, and some special subclasses need to remove @ JsonIgnore

@Rawi01
Copy link
Collaborator

Rawi01 commented Oct 29, 2024

You can always add a custom setter without annotation to the parent class.

I agree with @janrieke that this is probably a jackson bug/issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants