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

Backward-incompatible behaviour of 2.8: deserializing enum types with two static factory methods fail by default #1291

Closed
flozano opened this issue Jul 7, 2016 · 2 comments
Milestone

Comments

@flozano
Copy link
Contributor

flozano commented Jul 7, 2016

After upgrading to 2.8, I'm getting:

com.fasterxml.jackson.databind.JsonMappingException: Conflicting String creators: already had explicitly marked [method com.company.MyType#valueOf(1 params)], encountered [method com.company.MyType#fromString(1 params)]

Whereas in previous (2.7.x) it worked well.

The enum is defined as:

public enum MyType {

    V1("val1"),
    V2("val2"),
    V3("val3"),
    V4("val4"),
    V5("val5"),
    V6("val6");

    private final String name;

    MyType(String name) {
        this.name = name;
    }

    public static MyType fromString(String name) {
        for (MyType type : MyType.values()) {
            if (type.name.equals(name)) {
                return type;
            }
        }
        // In case no matching delegate to default parser which will throw a
        // IllegalArgumentException if it's unable to parse the name
        return MyType.valueOf(name.toUpperCase());
    }

    @Override
    public String toString() {
        return name;
    }
}
@flozano flozano changed the title Backward-incompatible behaviour of 2.8 Backward-incompatible behaviour of 2.8: deserializing enum types with two static factory methods fail by default Jul 7, 2016
@cowtowncoder
Copy link
Member

cowtowncoder commented Jul 7, 2016

Interesting. Thank you for reporting this; it is a flaw and change is unintentional. Not sure how it came about: static methods should not be auto-detected (unlike possibly single-string-constructors) but should require @JsonCreator. Possibly this is due to now skipping of constructor detection for enums (which caused another issue).

cowtowncoder added a commit that referenced this issue Jul 13, 2016
@cowtowncoder
Copy link
Member

One quick note: a work-around is to explicitly annotate the creator with @JsonCreator: this removes ambiguity.

Problem itself comes from valueOf(), generated automatically by Java compiler (for enum types), being implicitly found; and then Jackson also accepting fromString(String), and because both are implicit, deserializer creator considers it ambiguous. What is less clear is how and why did 2.7 and earlier NOT fail with this one.

I'll try to find an improvement to alleviate the need, but I think addition of @JsonCreator actually makes sense here regardless.

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

2 participants