OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] "jaxrs-spec" generator creates invalid nested enums with type number

Open
#18,511 0 comments 1 reaction 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 (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

If the OpenAPI definition contains a an enum of type number directly in the properties, an invalid Java enum is generated and the code does not compile. If the exact same enum is references however, the code is correct.

openapi-generator version

jaxrs-spec:7.5.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: test
  version: 0.0.1

paths:
  /test:
    get:
      responses:
        200:
          description: test
          content:
            application/json:
              schema:
                type: object
                properties:
                  inline:
                    type: number
                    enum: [ 0, 1, 2 ]
                  referenced:
                    $ref: '#/components/schemas/ReferencedEnum'

components:
  schemas:
    ReferencedEnum:
      type: number
      enum: [ 0, 1, 2 ]
Generation Details

I created a standard Maven project and ran it with mvn clean compile

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>Test</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>7.5.0</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/test.yaml</inputSpec>
              <generatorName>jaxrs-spec</generatorName>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
Steps to reproduce

Running the above, ReferencedEnum is valid:

public enum ReferencedEnum {
  NUMBER_0(new BigDecimal("0")),
  NUMBER_1(new BigDecimal("1")),
  NUMBER_2(new BigDecimal("2"));
  private BigDecimal value;
// ...
}

However the nested InlineEnum is references a method that does not exist:

public enum InlineEnum {
    NUMBER_0(BigDecimal.valueOf(new BigDecimal("0"))),
    NUMBER_1(BigDecimal.valueOf(new BigDecimal("1"))),
    NUMBER_2(BigDecimal.valueOf(new BigDecimal("2")));
    private BigDecimal value;
// ...
}

The method BigDecimal.valurOf(BigDecimal) does not exist, which gives a compilation error.

Related issues/PRs

none

Suggest a fix

Generate the same enum code no matter whether it is a separate Schema or not.

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

Start with the jaxrs-spec generator and the provided OpenAPI declaration, then run the Maven reproduction using pom.xml and test.yaml. Compare the generated InlineEnum and ReferencedEnum; the work is done when both numeric enums use valid, equivalent Java code and mvn clean compile succeeds.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.