question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][Kotlin] Generated clients cannot serialize into `List<T>`

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
adamsz-lumecommented, May 11, 2021

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

2reactions
trietschcommented, Jul 8, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Moshi 1.9.1 Cannot serialize Kotlin type - Stack Overflow
Upgrading to 1.9.1 now leads to a crash when attempting to serialize: java.lang.IllegalArgumentException: Cannot serialize Kotlin type com.xxx.
Read more >
Android Data Serialization Tutorial with the Kotlin Serialization ...
The Kotlin Serialization library generates serializers for classes annotated with @Serializable . A serializer is a class that handles an ...
Read more >
kotlinx.serialization 1.0 released | The Kotlin Blog
serialization comes with a simple but powerful pure-Kotlin API, which makes it effortless to parse JSON into type-safe Kotlin objects, and vice ...
Read more >
Fixing serialization of Kotlin objects once and for all - Medium
Some time ago, I read an article describing a problem experienced with Kotlin objects and serialization using built-in Java methods.
Read more >
Apache Kafka Reference Guide - Quarkus
Then, your application can generate messages and publish them to the prices-out ... For Kafka producer client serialization failures are not recoverable, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found