OpenAPITools / OpenAPITools/openapi-generator

[REQ] [Kotlin] Consistent API methods return types

Open
#10,896 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Greets!

Is your feature request related to a problem? Please describe.

In our services, we have some endpoints that are returning an optional class, and some that are not.
Currently, the default behaviour of the generator is to map all endpoint methods to non-nullable classes. This is problematic because it might potentially cause an NPE on the caller's side.

We found an option called nullableReturnType (that was implemented in #4422) but that makes all API method responses nullable. This makes it a bit inconvenient from the caller's point of view because they have to always check if the returned object is null, even if the original method cannot return null by definition.

Describe the solution you'd like

Can we have another option in the config file? When set, the generated client's return types will be consistent with the actual return types:

  • if the service returns a non-nullable object from a method foo, the generated client will return a non-nullable object for the generated foo
  • if the service returns a nullable object from a method bar, the generated client will return a nullable object for the generated bar

cc @dacloutier

Thank you!

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 by tracing the existing nullableReturnType configuration and the implementation referenced in #4422. Compare how generated Kotlin client methods represent nullable and non-nullable service responses, then define configuration behavior that preserves each endpoint's actual return type without making every response nullable.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
api, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.