Skip to content

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

Description

@jglick

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

#24812 breaks code generation in my project (confirmed via git bisect). Noted tangentially in another issue #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 993bfc9; 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).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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