OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java] Enum default value generated as string instead of enum value

Open
#15,144 2 comments 2 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

We have a field based on an enum and specifying a default value.
The generated Java code correctly contains the enum and it's used for the field, but the initialization of the field is incorrectly done with a string, that of course does not compile.

private JoinCriteria criteria = "AT_LEAST";

The expected code is

private JoinCriteria criteria = JoinCriteria.AT_LEAST;
openapi-generator version

openapi-generator-maven-plugin v6.5.0

OpenAPI declaration file content or url

Field definition

criteria:
  description: The Join satisfaction criteria
  default: AT_LEAST
  type: string
  allOf:
    - $ref: '#/components/schemas/JoinCriteria'

where JoinCriteria is defined as enum

JoinCriteria:
  enum:
    - ALL
    - AT_LEAST
  type: string
Generation Details

Generated with maven plugin

<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.5.0</version>
Steps to reproduce

Define a field based on an enum and with a default value and generated the Java code.

This is a small openapi definition to reproduce the issue




---
openapi: 3.0.3

info:
  title: Test API
  description: Test API
  version: "1.0"

paths:
  /twsd/api/add-info:
    put:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ApiInfo'
        required: true
      responses:
        "400":
          description: The provided parameters are not valid.

components:
  schemas:
    ApiInfo:
      type: object
      properties:
        dependencies:
          type: array
          items:
            $ref: '#/components/schemas/ObjectUsingEnum'
    JoinCriteria:
      enum:
        - ALL
        - AT_LEAST
      type: string
    ObjectUsingEnum:
      type: object
      properties:
        criteria:
          description: The Join satisfaction criteria
          default: AT_LEAST
          type: string
          allOf:
            - $ref: '#/components/schemas/JoinCriteria'
Related issues/PRs

The issue https://github.com/OpenAPITools/openapi-generator/issues/14689 looks similar, but even if it's fixed in v6.5.0 our problem is still present with that version

Suggest a fix

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

No repository file or test is named. Start by reproducing the issue with the provided minimal OpenAPI 3.0.3 definition and Java generator configuration; done means the generated Java code compiles and initializes the enum field with JoinCriteria.AT_LEAST rather than a string.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.