Bug Report Checklist
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.
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:
to:
val body = response.body ?: return null
This is safe because the enclosing responseBody() function returns T?.
Bug Report Checklist
Description
The generated
ApiClient.ktusesresponse.body(OkHttp'sResponse.body,which returns
ResponseBody?) without a null-safety check. This fails to compilewith Kotlin 2.x, which enforces stricter null-safety on platform types.
openapi-generator version
kotlin, library:jvm-okhttp4OpenAPI declaration file content or url
The specific spec doesn't matter — this bug is in the shared
ApiClient.ktinfrastructure template, not in spec-specific generated code. Any valid spec triggers it. Here's the minimal one used in the reproducer:Generation Details
CLI (after
brew install openapi-generatoron a Mac):Steps to reproduce
Please refer to the full reproducer project: https://github.com/oymo/openapi-kotlin2-nullable-repro
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:
to:
This is safe because the enclosing
responseBody()function returnsT?.