OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.84k stars 6.59k forks source link

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

Closed le-jhe closed 5 years ago

le-jhe commented 6 years ago
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
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

jmini commented 6 years ago

Thank you a lot for this Issue.

Initial issue #49 was fixed by @Zomzog in PR #75 and is available with 3.1.0-SNAPSHOT. In the 3.0.x version this was not supported at all.

You ave spotted the right place in code and "type" : "number" should be supported.

le-jhe commented 6 years ago

Actually, looking at the initial issue gave me an idea to test. What I can say now is that, with 3.0.3, my problem is fixed if I use "type" : "integer" instead of "type" : "number" So thank's, my problem is worked around even in 3.0.3. I will test the fix when 3.1.x becomes available.

jmini commented 6 years ago

@le-jhe I am glad your problem is solved, but I still think that we should support number