OpenAPITools / OpenAPITools/openapi-generator

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

Open
#13,718 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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?>()) {

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at line 251 of src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache and read how response bodies are handled for Unit results. Compare the current bodyContent check with the proposed type check, then verify that non-empty responses for Unit no longer invoke JSON deserialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.