OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
22.01k stars 6.6k forks source link

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

Open rauldeheer opened 2 years ago

rauldeheer commented 2 years ago

Bug Report Checklist

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"
}
rauldeheer commented 2 years ago

We found out that setting the required property on the schema will make the generator generate nullability correctly.

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

My question now remains; why does the property nullable do nothing?