OpenAPITools / OpenAPITools/openapi-generator
[BUG][java] Discriminator default for direct property enum $ref generates String.<value>
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)
Describe the bug
The Java client generator produces Java that does not compile when a discriminator property is a direct $ref to an inline enum property. The generated child constructor assigns the discriminator mapping value as though the property were String:
this.category = String.ARCHIVE;
String.ARCHIVE does not exist.
Minimal OpenAPI 3.1 input
openapi: 3.1.0
info:
title: Shared category discriminator
version: 1.0.0
paths: {}
components:
schemas:
CategorySource:
type: object
required:
- category
properties:
category:
type: string
enum:
- ARCHIVE
CategoryEvent:
type: object
required:
- category
properties:
category:
description: Event category
$ref: "#/components/schemas/CategorySource/properties/category"
discriminator:
propertyName: category
mapping:
ARCHIVE: "#/components/schemas/ArchiveCategoryEvent"
ArchiveCategoryEvent:
allOf:
- $ref: "#/components/schemas/CategoryEvent"
- type: object
properties:
value:
type: string
Reproduction
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar \
generate \
-g java \
-i enum-discriminator.yaml \
-o generated \
--skip-validate-spec \
--additional-properties modelNamePrefix=Stock,hideGenerationTimestamp=true
cd generated
mvn -q -DskipTests compile
Actual result
Latest master (b391b2b98c5926ee9842ef3bc863b57c4d824bbe, 7.26.0-SNAPSHOT) generates:
public StockArchiveCategoryEvent() {
this.category = String.ARCHIVE;
}
Compilation fails:
error: cannot find symbol
symbol: variable ARCHIVE
location: class String
The same failure occurs with OpenAPI Generator 7.24.0.
Expected result
The direct property reference should retain the enum type at the discriminator use site. The generated constructor should assign the enum member, rather than String.ARCHIVE, and the generated client should compile.
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 the Java generator entry point by running the CLI generation command against the supplied OpenAPI 3.1 input, then inspect the generated child constructor and its discriminator assignment. Done means the generated Java retains the direct property’s enum type and compiles successfully with Maven.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 72/100