swagger-api / swagger-api/swagger-codegen

[Kotlin] Enum deserialization not working

Open
#7,811 4 comments 0 reactions 0 assignees View on GitHub

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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.