[BUG][Kotlin] Generated clients cannot serialize into `List<T>`
See original GitHub issueBug 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:
- Created 3 years ago
- Reactions:2
- Comments:19 (10 by maintainers)
Top GitHub Comments
fyi, the moshi fix is waiting for an intermediate approval here https://github.com/streammachineio/openapi-generator/pull/2
@missionlanejeremymcbride new PR is ready for review by the OpenApi team: https://github.com/OpenAPITools/openapi-generator/pull/9918