Skip to content

[Java] Option to generate Enum values as public static final constants #6828

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

Open
hleb-albau opened this issue Jul 1, 2020 · 1 comment
Open

Comments

@hleb-albau
Copy link

hleb-albau commented Jul 1, 2020

There are some cases, when you want to generate enums as simple constants, to get move flexibility on future releases.
Let assume response body contains list of next object:

Field {
  FieldType type;
  String value;
}

Your customer takes current version of client, parses that list, extracts interested fields into his/her system.
If you update server to return list with new object types, it will break de-serialization of that customer, even if he not interested in those new object types. That cause, that addition of new enum value breaks backward compatibility.

It definitely make sense, in cases where enums are big dictionaries(100+ values) and constantly updating to generate that as raw constants.

also, there is opposite case.
When you create custom package for customer with addition FieldType, but don't want to add it to client. All you want is just send them mail with "read with type value XXXX" message.

also#2, some android folks still recommends to use simple constants over enums.

Additional context

@see swagger-api/swagger-codegen#6286

@hleb-albau
Copy link
Author

hleb-albau commented Jul 3, 2020

There is workaround - you can change enum template to produce class with static constants.

public class {{classname}} {
{{#allowableValues}}{{#enumVars}}
    {{#enumDescription}}
        /**
        * {{enumDescription}}
        */
    {{/enumDescription}}
    public static final {{{dataType}}} {{{name}}} = {{{value}}};
{{/enumVars}}{{/allowableValues}}
}

And run one more time generator using --import-model FieldType=Integer,Scenario=String addition options .

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

1 participant