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
- fix #8560: Kotlin: add support for generic parametrised types for moshi — committed to adamsz-lume/openapi-generator by adamsz-lume 3 years ago
- fix #8560: Kotlin: add support for generic parametrised types for moshi — committed to adamsz-lume/openapi-generator by adamsz-lume 3 years ago
- fix #8560: Kotlin: add support for generic parametrised types for moshi — committed to adamsz-lume/openapi-generator by adamsz-lume 3 years ago
- fix #8560: Kotlin: add support for generic parametrised types for moshi — committed to adamsz-lume/openapi-generator by adamsz-lume 3 years ago
- fix #8560: Kotlin: add support for generic parametrised types for moshi — committed to adamsz-lume/openapi-generator by adamsz-lume 3 years ago
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
Yep, for example:
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 registeredseems 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.