Skip to content

[BUG][JAVA] Enum in request body generates as String parameter, not Enum type #21407

Open
@WannabeSoftwareEngineer

Description

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

When an OpenAPI specification defines a enum schema with type: string and this schema is used in a requestBody, the Java generator produces an API interface method where the parameter is typed as java.lang.String instead of the generated Java enum type.

openapi-generator version

At the time of writing, the openapi-generator-cli was built on commit 12fa2c0 of the openapi-generator repository, which was merged into the master branch on June 10, 2025.

OpenAPI content
openapi: 3.1.0
info:
  title: ResourceClass
  description: OpenAPI spec for ResourceClass
  version: "1"
tags:
- name: ResourceClass
paths:
  /v1/resource-class/send:
    post:
      tags:
      - ResourceClass
      description: Add @Operation annotation to provide a description
      operationId: send
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Letter"
      responses:
        "200":
          description: OK - the request has succeeded.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Letter"
components:
  schemas:
    Letter:
      type: string
      enum:
      - A
      - B
jsonSchemaDialect: https://spec.openapis.org/oas/3.1/dialect/base
Steps to reproduce
  • clone https://github.com/OpenAPITools/openapi-generator
  • build the jar with ./mvnw clean install -DskipTests -Dmaven.javadoc.skip=true
  • run java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i input_spec.yaml
    • input_spec.yaml is the OpenAPI Spec from above
  • inspect the generated class src/main/java/org/openapitools/client/api/ResourceClassApi.java#send(String)

Actual

public Letter send(@javax.annotation.Nullable String body) throws ApiException {}

Expected

public Letter send(@javax.annotation.Nullable Letter body) throws ApiException {}
Related issues/PRs
components:
  schemas:
    Letter:
      enum:
      - A
      - B
Suggest a fix

The issue appears to be in DefaultCodegen#fromRequestBody, specifically when ModelUtils#isStringSchema(schema) is evaluated to true. This leads to updateRequestBodyForString being called, which then treats the parameter as a primitive string.

A solution could be to treat a named enum schema as a model.

This can be done by modifying DefaultCodegen#updateRequestBodyForString and supplying the schema's component name, e.g., Letter, to it as it similarly done in DefaultCodegen#updateRequestBodyForObject. Then this method could check for this name:

  • If a name is present, delegate to DefaultCodegen#addBodyModelSchema. This will correctly process the schema as a model and generate the proper enum type.
  • If no name is present, fall back to the existing updateRequestBodyForPrimitiveType logic to handle it as a primitive string.

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