swagger-api / swagger-api/swagger-codegen
[JavaSpring] Incorrect enum generation for integer-based data types
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
JavaSpring code generator is serializing number enumerations as strings
Swagger-codegen version
3.0.36, not a regression
Swagger declaration file content or url
openapi: 3.0.0
info:
version: 1.0
title: simple
termsOfService: urn:tos
contact: {}
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0
tags:
- name: Simple Service
description: >-
Simple service
paths:
/simple:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Simple'
components:
schemas:
Simple:
type: object
properties:
my_enum:
type: number
enum: [1,2,3]
Command line used for generation
> git status
On branch 3.0.0
Your branch is up to date with 'origin/3.0.0'.
> mvn package
[...]
[INFO] Reactor Summary for swagger-codegen-project 3.0.36:
[INFO]
[INFO] swagger-codegen-project ............................ SUCCESS [ 0.538 s]
[INFO] swagger-codegen (core library) ..................... SUCCESS [ 8.889 s]
[INFO] swagger-codegen (executable) ....................... SUCCESS [ 3.691 s]
[INFO] swagger-codegen (maven-plugin) ..................... SUCCESS [ 2.240 s]
[INFO] swagger-generator .................................. SUCCESS [ 3.463 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.052 s
[INFO] Finished at: 2022-11-15T15:58:09-05:00
[INFO] ------------------------------------------------------------------------
> java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
-i ./simple.yaml \
-l spring \
-o ~/tmp/swagger-codegen
Steps to reproduce
Serialize a "Simple" resource using the code generated by the above command. Note that the my_enum is serialized as a string instead of a number. This is because the generated code contains a @JsonValue annotation that causes jackson to serialize as a string.
public enum MyEnumEnum {
NUMBER_1(new BigDecimal(1)),
NUMBER_2(new BigDecimal(2)),
NUMBER_3(new BigDecimal(3));
private BigDecimal value;
MyEnumEnum(BigDecimal value) {
this.value = value;
}
@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static MyEnumEnum fromValue(String text) {
for (MyEnumEnum b : MyEnumEnum.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
Related issues/PRs
I verified that this exact same issue was fixed for the Java language template in https://github.com/swagger-api/swagger-codegen/pull/5713
Suggest a fix/enhancement
Apply the fix for the Java language template to the JavaSpring language template.
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
Compare the Java language template fix in PR #5713 with the JavaSpring language template, then generate the supplied OpenAPI example with the Spring generator. Inspect the generated MyEnumEnum and its serialization; done means the integer-based enum values serialize as numbers rather than strings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100