Skip to content

[BUG][kotlin-server] required nullable properties generated incorrectly #20052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 of 6 tasks
dennisameling opened this issue Nov 7, 2024 · 0 comments · Fixed by #20054
Closed
5 of 6 tasks

[BUG][kotlin-server] required nullable properties generated incorrectly #20052

dennisameling opened this issue Nov 7, 2024 · 0 comments · Fixed by #20054

Comments

@dennisameling
Copy link
Contributor

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 having a required and nullable property in my OpenAPI spec, it becomes non-nullable in the Kotlin code.

Taking the YAML example later down in this bug report, it results in this Kotlin code with the kotlin-server generator:

package com.example.models

/**
 * 
 * @param notNullableRequired 
 * @param nullableRequired 
 * @param nullableNotRequired 
 * @param notNullableNotRequired 
 */
data class Pet(
    val notNullableRequired: kotlin.String,
    val nullableRequired: kotlin.String,
    val nullableNotRequired: kotlin.String? = null,
    val notNullableNotRequired: kotlin.String? = null
) 

The nullableRequired example is incorrect, because it's supposed to be nullable. It should be kotlin.String? instead.

openapi-generator version

7.8.0

OpenAPI declaration file content or url
# OpenAPI schemas support 4 possible combinations for properties:
# 1. nullable + required
# 2. nullable + not required
# 3. not nullable + required
# 4. not nullable + not required
# This sample contains all of them to demonstrate how they are rendered in the generated code.
openapi: 3.0.3
info:
  title: Nullable Required
  version: 1.0.0
servers:
  - url: /api/v3
paths:
  /pet:
    post:
      operationId: addPet
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
      responses:
        "200":
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
components:
  schemas:
    Pet:
      required:
        - notNullable_required
        - nullable_required
      type: object
      properties:
        nullable_notRequired:
          type: string
          example: doggie
          nullable: true
        notNullable_notRequired:
          type: string
          example: doggie
        notNullable_required:
          type: string
          example: doggie
        nullable_required:
          type: string
          example: doggie
          nullable: true
Generation Details

Using the openapi-generator Gradle plugin:

openApiGenerate {
    group = "build"
    generatorName = "kotlin-server"
    inputSpec = "src/main/resources/openapi/example.yaml"
    outputDir = "${layout.buildDirectory.get()}/generated/example"
    packageName = "com.example"
    library = "javalin6"
}
Steps to reproduce
  • Take the YAML above and run the OpenAPI Generator (Gradle or CLI) for the kotlin-server generator
Related issues/PRs

Probably related issue: #14765

Suggest a fix

I'll provide a PR right after submitting this with a fix. It's a bug in the Mustache template.

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

Successfully merging a pull request may close this issue.

1 participant