Skip to content

Setter confusion on assignable types #158

@tsquared2763

Description

@tsquared2763

I'm running into a problem where multiple setters with assignable arguments cause confusion in handling annotations. Consider:

@JsonAutoDetect(fieldVisibility= JsonAutoDetect.Visibility.NONE,getterVisibility= JsonAutoDetect.Visibility.NONE, setterVisibility= JsonAutoDetect.Visibility.NONE, isGetterVisibility= JsonAutoDetect.Visibility.NONE)
Class Foo {
    private String bar;

    @JsonProperty
    public String getBar() {
        return bar;
    }

    @JsonProperty
    public void setBar(String bar) {
        this.bar = bar;
    }

    public void setBar(Serializable bar) {
        this.bar = bar.toString();
    }
}

I would expect to be able to marshall to/from Json using the annotated methods, but what happens is that (somewhat randomly based on the reflection iteration) I may get the setBar(Serializable) method mapped to the deserializer, which of course fails as an abstract mutator. Both methods occupy the same slot in the properties list during parser initialization.

The source of the problem seems to be at the following location - including foreshadowing comment by tatu on April 7 2009.. it didn't work for me sadly.

Looking for workaround ideas, other than 'don't do that'. Any way I can kick the setBar(Serialiazable) out of the list before the properties are created? Setting @JsonIgnore is too late unfortunately, the method list is already created when that is considered.

In com.fasterxml.jackson.databind.introspect.MemberKey.java

    @Override
    public boolean equals(Object o)
    {
        if (o == this) return true;
        if (o == null) return false;
        if (o.getClass() != getClass()) {
            return false;
        }
        MemberKey other = (MemberKey) o;
        if (!_name.equals(other._name)) {
            return false;
        }
        Class<?>[] otherArgs = other._argTypes;
        int len = _argTypes.length;
        if (otherArgs.length != len) {
            return false;
        }
        for (int i = 0; i < len; ++i) {
            Class<?> type1 = otherArgs[i];
            Class<?> type2 = _argTypes[i];
            if (type1 == type2) {
                continue;
            }
            /* 23-Feb-2009, tatu: Are there any cases where we would have to
             *   consider some narrowing conversions or such? For now let's
             *   assume exact type match is enough
             */
            /* 07-Apr-2009, tatu: Indeed there are (see [JACKSON-97]).
             *    This happens with generics when a bound is specified.
             *    I hope this works; check here must be transitive
             */
            if (type1.isAssignableFrom(type2) || type2.isAssignableFrom(type1)) {
                continue;
            }
            return false;
        }
        return true;
    }

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