swagger-api / swagger-api/swagger-codegen
[Kotlin] Enum deserialization not working
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When deserializing a response, if enums are used, and the enum value doesn't match with the enum name, an exception is thrown by moshi stating that the enum doesn't match expected values.
E.g.:
status: {
type: "string",
description: "Order Status",
enum: [
"Placed",
"Approved",
"Delivered"
]
}
When deserializing, it fails with: expected one of [placed, approved, delivered] but was Placed at path $.status
This happens because moshi's EnumJsonAdapter doesn't use the enum's getValue and instead either uses a Json annotation or name().
Swagger-codegen version
2.3.1
Command line used for generation
swagger-codegen generate -l kotlin -i swagger.yaml -o tmp
Steps to reproduce
Change on Petstore swagger spec any enum from lowercase to capitalized.
Suggest a fix/enhancement
A solution would be to add the Json annotation to each enum value. Another one to override the name method to return the value instead. One more is to provide a EnumJsonAdapter to moshi that can correctly decode the generated 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 Kotlin output produced by swagger-codegen generate -l kotlin -i swagger.yaml -o tmp, using the Petstore specification with an enum changed to capitalized values. Compare the generated enum handling with Moshi's EnumJsonAdapter behavior. Done means the generated client deserializes those enum values without the expected-values exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100