javaenumsjax-rsopenapiopenapi-generator

OpenAPI generator jaxrs-spec stop shorterning my enum values


Given this OpenAPI specification for the sizeRange field of a Project model:

size:
  type: string
  description: Size range for this project
  enum: [RANGE_0_10M,RANGE_10M_50M,RANGE_50M_100M]
  example: RANGE_10M_50M

The jaxrs-spec OpenAPI generator results in a Project.class like:

public enum SizeEnum {
_0_10M(String.valueOf("RANGE_0_10M")), _10M_50M(String.valueOf("RANGE_10M_50M")), _50M_100M(String.valueOf("RANGE_50M_100M"));

The resulting enum values are shortened (for use in Java):

Project.SizeEnum._0_10M

I was prepared to ignore this inconvenience, but it's resulting in an exception in this case:

Enum.valueOf(Project.SizeEnum.class, "RANGE_0_10M");

No enum constant com.mycompany.my_project.Project.SizeEnum.RANGE_0_10M

Notes

If I add a value into the enum that begins with something other than RANGE_ this causes all of the enum values to appear correctly. I infer that some process is automagically shortening all of them because they share a prefix.

Is this an OpenAPI generator configuration I have access to? I can't find it:


Solution

  • You can get rid of your unwanted RANGE_ prefix by adding the flag removeEnumValuePrefix=false to the additionalProperties part of your OpenAPI generator configuration.


    Complete example (maven):

    <execution>
        <id>generate-id</id>
        <phase>generate-sources</phase>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <additionalProperties>
                removeEnumValuePrefix=false
            </additionalProperties>
            <inputSpec>spec.yaml</inputSpec>
            <generatorName>spring</generatorName>
            <configOptions>
                <identifierNamingConvention>snake_case</identifierNamingConvention>
            </configOptions>
            <output>${project.build.directory}/generated-sources/output</output>
        </configuration>
    </execution>