Skip to content

[Kotlin] Enum deserialization not working #7811

@lopesmcc

Description

@lopesmcc
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions