openapi-generator: [BUG][Kotlin] Generated clients cannot serialize into `List`

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

An endpoint definition in the Open Api Spec that returns an array of objects, is not correctly serialized by all available serializationLibrary options (Moshi, Gson, Jackson) for the Kotlin generator. This results in deserialization issues, such as:

Exception in thread "main" java.lang.ClassCastException: class com.squareup.moshi.LinkedHashTreeMap
cannot be cast to class com.some.package.Example (com.squareup.moshi.LinkedHashTreeMap and
com.some.package.Example are in unnamed module of loader 'app')
openapi-generator version

Version 5.0.0

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  version: 0.1.0
  title: Example List<T> bug

paths:
  "/examples":
    get:
      summary: "Get all examples"
      responses:
        200:
          description: "The examples"
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Example"

components:
  schemas:
    Example:
      type: object
      properties:
        name:
          type: string
        amount:
          type: number
Generation Details

For generating, the only thing that is important, is using the Kotlin generator. Other options do not have any effect on the bug, as it’s unrelated. Regardless of the serializationLibrary, the issue persists over all of them.

Steps to reproduce

Use the generated client to perform a call that returns a list of objects. The call succeeds, however, if you’d want to do (e.g.) a map operation, that will fail, as Kotlin tries to cast the object there.

Related issues/PRs

None

Suggest a fix

I’ll create a Pull Request soon, with the fixes for all serializationLibrary options. For Jackson, I’ve already created a fix (which was by using a TypeReference<T>, instead of T::class.java.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 19 (10 by maintainers)

Commits related to this issue

Most upvoted comments

fyi, the moshi fix is waiting for an intermediate approval here https://github.com/streammachineio/openapi-generator/pull/2

Yep, for example:

//change the request type to string - you will need to change the ApiClient.responseBody with `if (T::class == String::class)` method to instantly return the raw object as string if you requested a return type of string.
val localVarResponse = request<String>(
            localVariableConfig
        )

        return when (localVarResponse.responseType) {
            ResponseType.Success -> {
                //Add this part
                val rawString = (localVarResponse as Success<*>).data as String
                val listType: Type = Types.newParameterizedType(List::class.java, MyObject::class.java)
                val adapter: JsonAdapter<List<MyObject>> = Serializer.moshi.adapter(listType)
                adapter.fromJson(rawString) as List<MyObject>
            }
            ...
        }

I’ve been keep an eye on this issue and saw recently the PR was merged, after upgrading the openapi generator version i’m running into this issue Platform class kotlin.Unit requires explicit JsonAdapter to be registered seems to be some issue with the moshi adapter, does anyone happen to know a quick fix?

Ah, it wasn’t failing first. I don’t know exactly when I have time to check it, probably somewhere this week.