OpenAPITools / OpenAPITools/openapi-generator
[BUG] Description:
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
There seems a bug in code generation from swagger document in case of Enumeration values.
For plugin swagger-codegen-maven-plugin, I have added the below property:
<additionalProperties>
<additionalProperty>errorOnUnknownEnum=true</additionalProperty>
</additionalProperties>
I am using openapi: 3.0.1 version. The enum is defined in Swagger as:
MySearchResult
type: string
enum:
- ABC
- DEF
- GHI
- JKL
maxLength: 10
The code generated for an enum is:
@JsonCreator
public static TypeEnum fromValue(String input) {
for (TypeEnum b : TypeEnum.values()) {
if (b.value.equals(input)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + **text** + "' for 'MySearchResult' enum.");
}
The bug is for the generated code for enum, when there is an unmatched value for enum, an exception is thrown as above code, the "text" is unknown as "Cannot resolve symbol 'text'". It should be "input" instead of "text".
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue? yes
- Have you validated the input using an OpenAPI validator (example)? yes
- Have you tested with the latest master to confirm the issue still exists? yes
- Have you searched for related issues/PRs? yes (No solution)
- What's the actual output vs expected output? An exception is thrown as shown in above code, the "text" is unknown as "Cannot resolve symbol 'text'". It should be "input" instead of "text".
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
openapi-generator version
3.0.45OpenAPI declaration file content or url
Generation Details
I use mvn clean install using IntelliJ IDE.
The output generated java file has the below code:
@JsonCreator
public static TypeEnum fromValue(String input) {
for (TypeEnum b : TypeEnum.values()) {
if (b.value.equals(input)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + text + "' for 'MySearchResult' enum.");
}
Steps to reproduce
Related issues/PRs
https://github.com/swagger-api/swagger-codegen-generators/pull/1094
Suggest a fix
https://github.com/swagger-api/swagger-codegen-generators/pull/1094
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 enum template or generator path that produces the fromValue(String input) method for errorOnUnknownEnum, using the provided OpenAPI 3.0.1 enum as the reproduction case. Compare the generated exception expression with the related swagger-codegen-generators PR 1094. Done means generated code references input rather than the unresolved text identifier and the regression is covered by the relevant generator test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100