OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] Broken code generation when using `format: enum`

Open
#24,950 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator? N/A I guess?
  • 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

https://github.com/OpenAPITools/openapi-generator/pull/24812 breaks code generation in my project (confirmed via git bisect). Noted tangentially in another issue https://github.com/OpenAPITools/openapi-generator/issues/24942 (and was reproducible in that context) but filing here explicitly with a smaller test case as requested by @Mattias-Sehlstedt + @jpfinne.

openapi-generator version

7.26.0-SNAPSHOT as built from 993bfc9aafdf434c4682fcd863fb19530a3549b8; a regression since 7.25.0

OpenAPI declaration file content or url
Reproduction sources

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>test</groupId>
  <artifactId>enum-format-repro</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>org.openapitools</groupId>
      <artifactId>jackson-databind-nullable</artifactId>
      <version>0.2.6</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.17.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-jsr310</artifactId>
      <version>2.17.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.findbugs</groupId>
      <artifactId>jsr305</artifactId>
      <version>3.0.2</version>
    </dependency>
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
      <version>1.3.2</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <!-- Switch between 7.25.0 (passes) and 7.26.0-SNAPSHOT (fails) -->
        <version>7.25.0</version>
        <!--
        <version>7.26.0-SNAPSHOT</version>
        -->
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/openapi/test.yaml</inputSpec>
              <generatorName>java</generatorName>
              <library>native</library>
              <generateApiTests>false</generateApiTests>
              <generateModelTests>false</generateModelTests>
              <generateApiDocumentation>false</generateApiDocumentation>
              <generateModelDocumentation>false</generateModelDocumentation>
              <apiPackage>test.api</apiPackage>
              <modelPackage>test.model</modelPackage>
              <configOptions>
                <sourceFolder>src/main/java</sourceFolder>
              </configOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
        <configuration>
          <source>11</source>
          <target>11</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

/tmp/enum-repro/src/main/openapi/test.yaml:

openapi: 3.0.3
info:
  title: Enum format test
  version: 1.0.0
paths: {}
components:
  schemas:
    Ruleset:
      type: object
      properties:
        bypassMode:
          type: string
          format: enum
          enum:
            - RULESET_BYPASS_MODE_UNSPECIFIED
            - RULESET_BYPASS_MODE_ALLOWED
            - RULESET_BYPASS_MODE_DISABLED
Generation Details

as above

Steps to reproduce

mvn clean compile with 7.25.0: no error

mvn clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -am -pl modules/openapi-generator-maven-plugin on trunk sources of the generator, then building test project with 7.26.0:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /tmp/enum-repro/target/generated-sources/openapi/src/main/java/test/model/Ruleset.java:[28,18] cannot find symbol
  symbol:   class Enum
  location: package test.model
[ERROR] /tmp/enum-repro/target/generated-sources/openapi/src/main/java/test/model/Ruleset.java:[45,41] method valueOf in class java.lang.Enum<E> cannot be applied to given types;
  required: java.lang.Class<T>,java.lang.String
  found:    java.lang.String
  reason: cannot infer type-variable(s) T
    (actual and formal argument lists differ in length)
[ERROR] /tmp/enum-repro/target/generated-sources/openapi/src/main/java/test/model/Ruleset.java:[47,37] method valueOf in class java.lang.Enum<E> cannot be applied to given types;
  required: java.lang.Class<T>,java.lang.String
  found:    java.lang.String
  reason: cannot infer type-variable(s) T
    (actual and formal argument lists differ in length)
[ERROR] /tmp/enum-repro/target/generated-sources/openapi/src/main/java/test/model/Ruleset.java:[49,38] method valueOf in class java.lang.Enum<E> cannot be applied to given types;
  required: java.lang.Class<T>,java.lang.String
  found:    java.lang.String
  reason: cannot infer type-variable(s) T
    (actual and formal argument lists differ in length)
[INFO] 4 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

The diff to the generated source file:

27a28
> import test.model.Enum;
44c45
<     RULESET_BYPASS_MODE_UNSPECIFIED(String.valueOf("RULESET_BYPASS_MODE_UNSPECIFIED")),
---
>     RULESET_BYPASS_MODE_UNSPECIFIED(Enum.valueOf("RULESET_BYPASS_MODE_UNSPECIFIED")),
46c47
<     RULESET_BYPASS_MODE_ALLOWED(String.valueOf("RULESET_BYPASS_MODE_ALLOWED")),
---
>     RULESET_BYPASS_MODE_ALLOWED(Enum.valueOf("RULESET_BYPASS_MODE_ALLOWED")),
48c49
<     RULESET_BYPASS_MODE_DISABLED(String.valueOf("RULESET_BYPASS_MODE_DISABLED"));
---
>     RULESET_BYPASS_MODE_DISABLED(Enum.valueOf("RULESET_BYPASS_MODE_DISABLED"));
50c51
<     private String value;
---
>     private Enum value;
52c53
<     BypassModeEnum(String value) {
---
>     BypassModeEnum(Enum value) {
57c58
<     public String getValue() {
---
>     public Enum getValue() {
67c68
<     public static BypassModeEnum fromValue(String value) {
---
>     public static BypassModeEnum fromValue(Enum value) {
Related issues/PRs

not that I could see

Suggest a fix

FWIW Claude claims

The intent of typeMapping.put("enum", "Enum") was to support the new getCommonSchemaType discriminator-resolution path. The fix should not pollute the global typeMapping; instead, handle it inline inside getCommonSchemaType / getCommonTypeMapping without registering "enum" as a general-purpose format mapping — or, at minimum, add importMapping.put("Enum", "java.lang.Enum") and fix the Mustache template so the generated code uses the correct BypassModeEnum.valueOf(String) call rather than Enum.valueOf(String) (the two-argument java.lang.Enum.valueOf(Class<T>, String) is unrelated to what the template emits).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the Maven setup in pom.xml and the schema in src/main/openapi/test.yaml, comparing Java generation on 7.25.0 with the current version. Inspect the regression introduced by PR 24812 and the generated Ruleset.java; done means the generated project compiles and the enum-format case has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.