OpenAPITools / OpenAPITools/openapi-generator

[BUG][KOTLIN][TYPESCRIPT] Non nullable properties generate as nullable/undefined

Open
#13,531 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

When we generate a client in either Kotlin or Typescript all properties from models become nullable (in Kotlin) or undefined (in Typescript). This means that some models may have nullable/undefined types when they are in fact always defined/not null.

openapi-generator version

CLI version 2.5.2.

OpenAPI declaration file content or url

For the sake of simplicity I've created the following spec file with only one model to be generated:

{
  "openapi": "3.0.1",
  "info": {
    "title": "OpenAPI definition",
    "version": "v0"
  },
  "servers": [
    {
      "url": "http://localhost:8080",
      "description": "Generated server url"
    }
  ],
  "paths": {},
  "components": {
    "schemas": {
      "AccountDto": {
        "type": "object",
        "properties": {
          "email": {
            "type": "string",
          }
        }
      }
    }
  }
}
Generation Details

I use the following CLI command to generate the code (for Kotlin):

npx @openapitools/openapi-generator-cli generate -I spec.json -c config.json  -g kotlin -o output/

With the following config file (for Kotlin):

{
  "packageName": "com.domain.project",
  "artifactId": "project",
  "groupId": "com.domain.project",
  "enumPropertyNaming": "UPPERCASE",
  "serializationLibrary": "jackson"
}
Steps to reproduce
  1. Run the CLI command as described above with the provided config file.
  2. Open the AccountDto model.
  3. Check the generated property email.

You can see that the class will be generated like this:

data class AccountDto (
    @field:JsonProperty("email")
    val email: kotlin.String? = null
)

Instead of this:

data class AccountDto (
    @field:JsonProperty("email")
    val email: kotlin.String
)
Suggest a fix

Provide a way to make properties not nullable. I've tried to add these properties to the email property without success:

"email": {
  "type": "string",
  "nullable": false,
  "default": "email@email.com"
}

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 running the provided CLI command with the sample spec and config, then compare the generated AccountDto output for Kotlin and TypeScript. Review how each generator interprets the schema's property and required information, and identify the existing generator tests covering nullable or undefined model properties. Done means the expected nullability behavior is reproduced and verified in tests for both targets.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.