Skip to content

[kotlin-client][jvm-ktor] Support nullable response types#23870

Open
TheJJ wants to merge 1 commit into
OpenAPITools:masterfrom
TheJJ:ktor-nullable-responses
Open

[kotlin-client][jvm-ktor] Support nullable response types#23870
TheJJ wants to merge 1 commit into
OpenAPITools:masterfrom
TheJJ:ktor-nullable-responses

Conversation

@TheJJ
Copy link
Copy Markdown

@TheJJ TheJJ commented May 25, 2026

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.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Enables nullable response bodies in the Kotlin jvm-ktor generator 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.

  • Bug Fixes
    • api.mustache now appends ? to the return type when returnProperty.isNullable is true.
    • Removed : Any bounds from generics in HttpResponse.kt.mustache (including HttpResponse, BodyProvider, TypedBodyProvider, MappedBodyProvider, wrap, and typedBody/map) to allow nullable types.

Written for commit df3caf7. Summary will update on new commits. Review in cubic

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.
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant