OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.82k stars 6.33k forks source link

[BUG][Kotlin/okhttp-gson] wrong handling of empty response #13718

Open zoltanwork opened 1 year ago

zoltanwork commented 1 year ago
Description

We use a legacy REST server that returns a non-empty response even if it the response type is Unit. In this case the following exception is thrown: java.lang.IllegalArgumentException: Platform class kotlin.Unit requires explicit JsonAdapter to be registered at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:76) at com.squareup.moshi.Moshi.adapter(Moshi.java:146) at com.squareup.moshi.Moshi.adapter(Moshi.java:106) at com.squareup.moshi.Moshi.adapter(Moshi.java:75) at com.squareup.moshi._MoshiKotlinExtensionsKt.adapter(-MoshiKotlinExtensions.kt:40) ...

I checked another generator (Java/apache-httpclient) to see how it handles this case. There the Java class of the expected result is checked and the code doesn't call the JSON deserializer if no response is expected.

openapi-generator version

6.0.1

Suggest a fix/enhancement

Suggestion is to change the line 251 in the src\main\resources\kotlin-client\libraries\jvm-okhttp\infrastructure\ApiClient.kt.mustache from

        if (bodyContent.isEmpty()) {

to

        if (typeOf<T>() == typeOf<Unit?>()) {
wing328 commented 1 year ago

can you please file a PR with the suggested fix and we'll review accordingly? thanks

zoltanwork commented 1 year ago

Sorry , I am not familiar with github. I did what you suggested. Can you please check if it is OK? Thanks