[kotlin-client][jvm-ktor] Support nullable response types#23870
Open
TheJJ wants to merge 1 commit into
Open
Conversation
The jvm-ktor library did not handle OpenAPI schemas that allow null
as a valid response body (e.g. anyOf: [T, {type: null}] or
allOf: [T] with nullable: true).
Two template bugs prevented this from working:
1. api.mustache used {{{returnType}}} directly, ignoring the
isNullable flag on the response property. Now it appends ? when
returnProperty.isNullable is true.
2. HttpResponse.kt.mustache hardcoded <T : Any> on every generic
type parameter. This made HttpResponse<SomeType?> invalid at
the Kotlin type-system level. Removing the : Any constraint lets
the generated code use nullable types naturally.
TheJJ
added a commit
to TheJJ/stustapay
that referenced
this pull request
May 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
jvm-ktorlibrary did not handle OpenAPI schemas that allow null as a valid response body (e.g. anyOf:[T, {type: null}]orallOf: [T]withnullable: true).Two template bugs prevented this from working:
api.mustacheused{{{returnType}}}directly, ignoring theisNullableflag on the response property. Now it appends?whenreturnProperty.isNullableistrue.HttpResponse.kt.mustachehardcoded<T : Any>on every generic type parameter. This madeHttpResponse<SomeType?>invalid at the Kotlin type-system level. Removing the: Anyconstraint lets the generated code use nullable types naturally.PR checklist
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)Summary by cubic
Enables nullable response bodies in the Kotlin
jvm-ktorgenerator so clients can return HttpResponse<T?> when an OpenAPI schema allows null (e.g., anyOf [T, null] or nullable: true). Generated code now compiles and handles nulls correctly.api.mustachenow appends?to the return type whenreturnProperty.isNullableis true.: Anybounds from generics inHttpResponse.kt.mustache(includingHttpResponse,BodyProvider,TypedBodyProvider,MappedBodyProvider,wrap, andtypedBody/map) to allow nullable types.Written for commit df3caf7. Summary will update on new commits. Review in cubic