OpenAPITools / OpenAPITools/openapi-generator

[BUG] Description:

Open
#16,224 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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.45
OpenAPI 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.