OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] Java generator doesn't work with discriminatorProperty enum
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 (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
When using a discriminatorMapping that references an enum field,
"discriminator":{
"propertyName":"method"
}
"method":{
"type":"string",
"enum":[
"OPTION",
"HELLO",
"WORLD"
]
},
It results in the generation of oneOf classes that have an invalid constructor trying to cast a string to an enum. Eg:
public OneOfTheOneOfClasses() {
this.method = this.getClass().getSimpleName();
}
Error: error: incompatible types: String cannot be converted to MethodEnum
this.method = this.getClass().getSimpleName();
openapi-generator version
6.6.0
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
Take an existing working example of discriminatorMapping openapi spec and take the discriminator property and convert to an enum. The java-client won't build with the error above every time.
Related issues/PRs
Suggest a fix
the valueOf function could to be used when the discriminatorProperty is an enum. Eg:
public CreateACHPayment() {
this.method = MethodEnum.valueOf(this.getClass().getSimpleName());
}
HOWEVER I don't think this will work because the class name isn't a valid Enum value.. so this is weird code to begin with, and I'm not sure why it gets generated at all or what use case it fills.
Would recommend finding current java generations with discriminatorProperty to see how it works in the happy case, then evaluating how it should be made to work with discriminator properties that are enums.
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's discriminatorMapping handling and the generated oneOf constructor shown in the report. Reproduce the failure by converting the discriminator property in an existing working spec to an enum, then compare the generated Java with the happy-case discriminator output. Done means the generated java-client builds successfully and has regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100