Skip to content

IllegalArgumentException: Conflicting setter definitions for property with more than 2 setters #3125

@mistyzyq

Description

@mistyzyq

Hi,
I am using jackson-databind:2.11.1, for some special reason, I must provide multiple compatible setters, I got failed with the following case:

public class Data {
    // the value is the index of EnumValue
    private Integer value;

    public enum EnumValue {
        EV1, EV2, EV3
    }

    public String getValue() {
        return EnumValue.values()[value].name();
    }

    public void setValue(Integer value) {
        this.value = value;
    }
    public void setValue(EnumValue value) {
        this.value = value.ordinal();
    }
    public void setValue(String value) {
        this.value = EnumValue.valueOf(value).ordinal();
    }

    @Override
    public String toString() {
        return "Data [value=" + value + "]";
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        Data data = new Data();
        data.setValue(EnumValue.EV3);

        String str = mapper.writeValueAsString(data);
        System.out.println(str);

        Data a2 = mapper.readValue(str, Data.class);
        System.out.print(a2);
    }
}

Try a few more times, sometimes it works fine, and sometimes it throws IllegalArgumentException (Conflicting setter definitions for property).

I have read this part of the source code, the course is probably in the POJOPropertyBuilder.getSetter() method. It seems prefer to the setter with String parameter, and can choose the right one (the one with String parameter) when there are 2 or less setters. In my case, there are 3, If the setter with String parameter is not first two, the exception will be thrown. but it seems that, the order of setters are indeterminate.

I've found a similar issue here #2741, but it was a different case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions