Skip to content

[BUG] [kotlin] jvm-okhttp4 ApiClient.kt: response.body used without null-safety — fails to compile with Kotlin 2.x #23514

@oymo

Description

@oymo

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

The generated ApiClient.kt uses response.body (OkHttp's Response.body,
which returns ResponseBody?) without a null-safety check. This fails to compile
with Kotlin 2.x, which enforces stricter null-safety on platform types.

openapi-generator version
  • Generator: kotlin, library: jvm-okhttp4
  • Generator version: 7.21.0 (also affects earlier versions)
  • Kotlin version: 2.1.10 (any Kotlin 2.x triggers this)
  • OkHttp version: 4.12.0
OpenAPI declaration file content or url

The specific spec doesn't matter — this bug is in the shared ApiClient.kt infrastructure template, not in spec-specific generated code. Any valid spec triggers it. Here's the minimal one used in the reproducer:

json
{
  "openapi": "3.0.3",
  "info": { "title": "Minimal API", "version": "1.0.0" },
  "paths": {
    "/items/{id}": {
      "get": {
        "operationId": "getItem",
        "parameters": [
          { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Item" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Item": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "name": { "type": "string" }
        }
      }
    }
  }
}
Generation Details

CLI (after brew install openapi-generator on a Mac):

openapi-generator generate \
  -g kotlin \
  -i api-spec.json \
  -o generated \
  --additional-properties=library=jvm-okhttp4
Steps to reproduce
  • Perform the generation step (which should complete with no errors)
  • Compile the generated code with Kotlin 2.x.
./gradlew compileKotlin

Please refer to the full reproducer project: https://github.com/oymo/openapi-kotlin2-nullable-repro

git clone https://github.com/oymo/openapi-kotlin2-nullable-repro.git
cd openapi-kotlin2-nullable-repro
./gradlew compileKotlin   # fails with null-safety error on Kotlin 2.x
Related issues/PRs

I could not find any similar reported issues, neither open nor closed.

Suggest a fix

I believe the issue can be fixed by a simple edit of

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache

Change the assignment from:

val body = response.body

to:

val body = response.body ?: return null

This is safe because the enclosing responseBody() function returns T?.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions