OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] Enum in request body generates as String parameter, not Enum type
Nobody has claimed this yet.
- 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?
- 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 12fa2c0032a12c29efd8cf359e1e62cfdc0f0d1a 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.yamlinput_spec.yamlis 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
- Issue #15144: tried the suggested "fix" to remove "type: string" from the components schema definition. Issue persists. For completeness, this is the updated definition of the schema
Letter:
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with DefaultCodegen#fromRequestBody and trace the ModelUtils#isStringSchema path into updateRequestBodyForString, then compare it with updateRequestBodyForObject and addBodyModelSchema. Reproduce using the supplied OpenAPI YAML and Java generator command; done means ResourceClassApi.java generates send(Letter body) rather than send(String body).
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100