Skip to content

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

Open
@fmossott

Description

@fmossott

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 #14689 looks similar, but even if it's fixed in v6.5.0 our problem is still present with that version

Suggest a fix

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