OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Java] Bug generating code when specs contains enum

Open
#11,571 1 comment 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 (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

The OpenAPI generator generates invalid Java code with swagger 2 spec file which contains enumeration

The generated code doesn't compile

This is an example project, which demonstrates the error: https://github.com/dsurot/openapi-generator-maven-plugin-bug

openapi-generator version

It used to work with version prior to 5.3.1

OpenAPI declaration file content or url
swagger: '2.0'
info:
  description: a short description
  version: '1.0.0'
  title: A swagger 2 API with enum
  contact:
    name: API Developper
basePath: '/api/v1'
tags:
  - name: api-end-point
    description: API End Point
schemes:
  - https
paths:
  /api:
    get:
      tags:
        - api-end-point
      operationId: apiUsingGET
      produces:
        - '*/*'
      responses:
        '200':
          description: OK
          headers:
            X-TraceId:
              type: string
          schema:
            type: array
            items:
              $ref: '#/definitions/ObjectWithEnum'
        '400':
          description: Bad Request
          headers:
            X-TraceId:
              type: string
securityDefinitions:
  basicAuth:
    type: basic
  apiKey:
    type: apiKey
    name: apikey
    in: header
security:
  - basicAuth: []
  - apiKey: []
definitions:
  ObjectWithEnum:
    description: Object with enum.
    properties:
      amount:
        example: '56.99'
        format: double
        type: number
      currency:
        example: EUR
        type: string
      status:
        allOf:
          - $ref: '#/definitions/StatusEnum'
          - example: WAITING_FOR_PAYMENT
            readOnly: true
    required:
      - amount
      - currency
    type: object
  StatusEnum:
    description: "Status Enum"
    enum:
      - WAITING_FOR_PAYMENT
      - PAYMENT_OK
      - PAYMENT_KO
      - PAYMENT_NOT_ATTEMPTED
    type: string

Generation Details

Maven configuration

<plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>5.3.1</version>
        <configuration>
          <generateModelTests>false</generateModelTests>
          <generateApiTests>false</generateApiTests>
          <configOptions>
            <dateLibrary>java8</dateLibrary>
            <serializableModel>true</serializableModel>
            <openApiNullable>false</openApiNullable>
          </configOptions>
        </configuration>
        <executions>
          <execution>
            <id>swagger2-with-enum</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/specs/swagger2-with-enum.yml
              </inputSpec>
              <generatorName>java</generatorName>
              <library>resttemplate</library>
              <apiPackage>com.accor.issue.api</apiPackage>
              <modelPackage>com.accor.issue.client.model</modelPackage>
              <templateDirectory>${project.basedir}/src/main/resources/templates</templateDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

Steps to reproduce

mvn clean compile on project https://github.com/dsurot/openapi-generator-maven-plugin-bug

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 by running mvn clean compile in the linked example project with the provided Swagger 2 specification, then inspect the Java generator output for the enum and its allOf usage. Trace the relevant Java generator or template entry point that produces the invalid model code. Done means the specification generates Java code that compiles with the reported Maven configuration.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.