Skip to content

generated enums #1919

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

Closed
fehguy opened this issue Jan 18, 2016 · 7 comments
Closed

generated enums #1919

fehguy opened this issue Jan 18, 2016 · 7 comments

Comments

@fehguy
Copy link
Contributor

fehguy commented Jan 18, 2016

Currently a generated class gives enums like such:

public enum FormatEnum {
  ENUM1("enum 1"),
  ENUM2("enum 2");

  private String value;

  FormatEnum(String value) {
    this.value = value;
  }

  @Override
  public String toString() {
    return value;
  }
}

When constructing a value from the string "enum 1", you will get an error as "enum 1" is compared against the ENUM1 and ENUM2 keys, resulting in an exception.

@fehguy
Copy link
Contributor Author

fehguy commented Jan 18, 2016

There is an easy fix for this. First, the enum generation in the templates should be updated as such:

    public enum MixedCase {
        MYHOME("my-home"),
        MYWORK("my work");

        private String text;

        MixedCase(String text) {
            this.text = text;
        }

        public String toString() {
            return this.text;
        }

        @JsonCreator
        public static MixedCase fromValue(String text) {
            if (text != null) {
                for (MixedCase b : MixedCase.values()) {
                    if (text.equals(b.text)) {
                        return b;
                    }
                }
            }
            return null;
        }
    }

Note the @JsonCreator which will triggers matching with the input string, and does an equals on the value.

Next, the Serialization mapper--if Jackson--should be configured as such:

Json.mapper().enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);

With this change, the toString() method is called, allowing MYHOME to serialize as my-home.

@wing328 I can make this change but for the other libraries (gson, for example), we may need to have a different approach.

@wing328
Copy link
Contributor

wing328 commented Jan 19, 2016

@fehguy this is noted. We'll test other Java HTTP libraries.

@wing328 wing328 added this to the v2.1.6 milestone Jan 19, 2016
@ePaul
Copy link
Contributor

ePaul commented Feb 10, 2016

@fehguy I would write this fromValue method slightly differently:

        @JsonCreator
        public static MixedCase fromValue(String text) {
            for (MixedCase b : MixedCase.values()) {
                if (b.text.equals(text)) {
                    return b;
                }
            }
            return null;
        }

No need for the null check, other possibly for a performance optimization.

@wing328
Copy link
Contributor

wing328 commented Mar 19, 2016

@ePaul I like your suggestion. Do you have cycle to contribute the enhancement?

@ePaul
Copy link
Contributor

ePaul commented Mar 19, 2016

@wing328 I can't promise anything currently, sorry. (I'm on vacations until start of April, and then likely will be buried in project work again.)

@wing328
Copy link
Contributor

wing328 commented Mar 19, 2016

@ePaul enjoy your vacation! We'll take care of it. No worry.

@wing328 wing328 modified the milestones: v2.2.0, v2.1.6 Mar 19, 2016
@wing328 wing328 modified the milestones: v2.3.0, v2.2.0 Jul 13, 2016
@wing328
Copy link
Contributor

wing328 commented Aug 5, 2016

Added via #3531

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

No branches or pull requests

3 participants