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

@JsonIgnore on interface takes precedence over @JsonIgnore(false) on subclass

Open rymarm opened this issue 3 months ago • 1 comments

I found that a getter annotated with @JsonIgnore(false) in the subclass does not override @JsonIgnore in the implemented interface, even though @JsonIgnore(false) in the subclass should logically take precedence over the interface-level ignore.

Minimal reproduction:

public class Person extends BasicPerson implements PersonInterface{
    public Person(String name) {
        super(name);
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Person demoObject = new Person("Maksym");
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

        String json = objectMapper.writeValueAsString(demoObject);
        System.out.println(json);
    }
}

abstract class BasicPerson {
    protected String name;

    public BasicPerson(String name) {
        this.name = name;
    }

    @JsonIgnore(false)
    public String getName() {
        return name;
    }
}

interface PersonInterface {
    @JsonIgnore
    String getName();
}

Expected output:

{"name":"Maksym"}

Current result:

{}

Jackson version 2.19.2

Correct me if this behavior is apparent to others, but I believe that since a Java class can have only one superclass and can implement multiple interfaces, it makes sense that configuration from the subclass should take precedence over the interface.

rymarm avatar Aug 28 '25 15:08 rymarm

Precedence order is debatable (pros and cons for alternative ordering), but I think the way things are implemented, interface class X DIRECTLY implements do have precedence over base class. So for case like:

class Impl extends Base implements A {  }
interface A extends B { }
abstract class Base implements C { }

precedence for annotations for methods would be:

Impl > A > B > Base > C

This is the intent at any rate.

While I can see other precedences might have their benefits, this is the resolution (and has been for about 15 years) and will not be changed at this point in time.

cowtowncoder avatar Aug 28 '25 22:08 cowtowncoder