swagger-api / swagger-api/swagger-codegen
Incorrect parameter type in generated fromValue method for enum of integer type: should be int, not String
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Incorrect paramater type for generated @JsonCreator function fromValue: should be int, not String.
Given the following enum definition:
"Purpose": {
"type": "integer",
"enum": [0, 1, 2, 3, 4]
}
... the following Java class is generated:
public enum Purpose {
NUMBER_0(0),
NUMBER_1(1),
NUMBER_2(2),
NUMBER_3(3),
NUMBER_4(4);
private Integer value;
Purpose(Integer value) {
this.value = value;
}
@JsonValue
public Integer getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static Purpose fromValue(String text) {
for (Purpose b : Purpose.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
The relevant section is the @JsonCreator fromValue(String text) method. In earlier versions of io.swagger:swagger-codegen-cli:2.4.19 for instance, the generated parameter seems to be Integer, as expected.
The reason this is a major concern for us is that as of jackson-databind 2.11, the jackson object mapper correctly treats int values as ints, not strings.
Swagger-codegen version
3.0.25
Swagger declaration file content or url
"Purpose": {
"type": "integer",
"enum": [0, 1, 2, 3, 4]
}
Command line used for generation
Using swaggerCodegen 'io.swagger.codegen.v3:swagger-codegen-cli:3.0.25' with gradle plugin
'org.hidetake.swagger.generator' version '2.18.2'.
Steps to reproduce
Related issues/PRs
A related issue is filed in jackson-databind: https://github.com/FasterXML/jackson-databind/issues/2737
Suggest a fix/enhancement
Roll back implementation to what was used in version 2.4.19.
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 by comparing Java enum generation in swagger-codegen 3.0.25 with the earlier 2.4.19 behavior, focusing on the generated @JsonCreator fromValue method. Reproduce the Purpose integer enum from the issue and identify the generation template or entry point that selects its parameter type. Done means generated integer enums use an integer-compatible parameter and a regression case covers the output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100