Skip to content

[JAVA] enums with number type are not supported #447

@le-jhe

Description

@le-jhe
Description

Given that a numerical enum is in my custom input types, openapi-generator "decides" to use BigDecimal for mapping the "type" : "number" (maybe because of something in my pom). The problem is that the java source code generated for the enum is not correct

  public enum LogLevelEnum {
    _0("0"),
    _1("1"),
    _2("2"),
    _3("3"),
    _4("4");
    private BigDecimal value;
    LogLevelEnum(BigDecimal value) {
      this.value = value;
    }

Correct code should be something like

  public enum LogLevelEnum {
    _0(BigDecimal.valueOf(0)),
    _1(BigDecimal.valueOf(1)),
    _2(BigDecimal.valueOf(2)),
    _3(BigDecimal.valueOf(3)),
    _4(BigDecimal.valueOf(4));
openapi-generator version
  • 3.0.2
  • 3.1.0-SNAPSHOT [master]
OpenAPI declaration file content or url

With the following type definition in my input .json file:

"logLevel" : {
            "type" : "number",
            "description" : "* some description",
            "enum" : [ 0, 1, 2, 3, 4 ]
          }
Command line used for generation
    <build>
        <plugins>
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>3.0.3</version>
                <dependencies>
                    <dependency>
                        <groupId>com.google.guava</groupId>
                        <artifactId>guava</artifactId>
                        <version>19.0</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <language>java</language>
                    <inputSpec>${project.basedir}/../my-client/src/test/resources/my.json</inputSpec>
                    <addCompileSourceRoot>false</addCompileSourceRoot>
                    <apiPackage>${api-package}</apiPackage>
                    <modelPackage>${model-package}</modelPackage>
                    <configOptions>
                        <sourceFolder>${generated-sources-java-path}</sourceFolder>
                        <dateLibrary>java8</dateLibrary>
                    </configOptions>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-jersey2</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <library>jersey2</library>
                            <output>${generated-output-root-jersey2}</output>
                        </configuration>
                    </execution>
Steps to reproduce

mvn package

Suggest a fix/enhancement

Check AbstractJavaCodegen.java:1045 ( toEnumValue(...) )in the list of numerical types, BigDecimal is not there, thus the processor goes to default and just wraps escaped values with string

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions