Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #9607
Closed
Open
Issue created May 27, 2021 by Administrator@rootContributor4 of 6 checklist items completed4/6 checklist items

[BUG][JAVA] Single-Space String in enum produces invalid identifier in Java 9+

Created by: aelfric

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I am using the openapi-generator-maven-plugin with a Java 14 project. It seems that in versions of Java later than 9, a single underscore is no longer a valid identifier. I was trying to generate classes for an API that uses enums where one of the possible values is a single space.

"audio_quality": {
  "type": "string",
  "description": "Audio quality of the participant.",
  "enum": [
    " ",
    "good",
    "fair",
    "poor",
    "bad"
  ]
}

The Java code that is generated cannot be compiled because of that first enum value _(" ").

public enum AudioQualityEnum {
    _(" "),
    
    GOOD("good"),
    
    FAIR("fair"),
    
    POOR("poor"),
    
    BAD("bad");

    private String value;

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

    @JsonValue
    public String getValue() {
      return value;
    }

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

    @JsonCreator
    public static AudioQualityEnum fromValue(String value) {
      for (AudioQualityEnum b : AudioQualityEnum.values()) {
        if (b.value.equals(value)) {
          return b;
        }
      }
      throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }
  }
openapi-generator version

5.1.0

OpenAPI declaration file content or URL

Here is a minimal reproducible example.

https://gist.github.com/aelfric/9e039e971552be774b60a930818a118d

Generation Details

I was using the Maven plugin with this configuration:

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <!-- RELEASE_VERSION -->
            <version>5.1.0</version>
            <!-- /RELEASE_VERSION -->
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/resources/minimal.json</inputSpec>
                        <generatorName>java</generatorName>
                        <library>native</library>
                        <configOptions>
                            <sourceFolder>src/gen/java/main</sourceFolder>
                        </configOptions>
                        <skipValidateSpec>true</skipValidateSpec>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Steps to reproduce

Run

mvn compile
Related issues/PRs
Suggest a fix

I'm not sure what the best solution is - maybe enums of this type should be named _BLANK.

Assignee
Assign to
Time tracking