OpenAPITools / OpenAPITools/openapi-generator

[BUG] [kotlin-server] generated code for objects where one property is a list of enum is incorrect

Open
#19,105 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
  • [v ] Have you provided a full/minimal spec to reproduce the issue?
  • [v] Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • [v] Have you searched for related issues/PRs?
  • [v] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating the code using the kotlin-server generator, if there is a model that is an object where one of the property is an array of enums, the generated kotlin code is incorrect. Here is an example:

...
components:
  schemas:
    things:
      type: object
      description: list of things
      properties:
        values:
        type: array
        items:
          description: possible thing
          type: string
          enum:
            - foo
            - bar
            - baz

The model class generated for this enum is the following (removed comments for brievity):

...
data class Foobar(
    val propertyValues: Foobar.PropertyValues? = null
) : Serializable 
{
    companion object {
        private const val serialVersionUID: Long = 123
    }
    enum class PropertyValues(val value: kotlin.collections.List<kotlin.String>){
        foo("foo"),
        bar("bar"),
        baz("baz");
    }
}
...

This code does not compile because the constructor for the enum class expects List<String> instead of a String and all the enum variants are constructed with <variant>(<String>)

openapi-generator version

Using version 7.7.0 of the openapi-generator gradle plugin.

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: Reproduce bug in kotlin-server generator
  version: "1.0"
paths:
  /api/v1/enums:
    get:
      description: Returns the value of the enum
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/things"
components:
  schemas:
    things:
      type: object
      description: list of things
      properties:
        values:
        type: array
        items:
          description: possible thing
          type: string
          enum:
            - foo
            - bar
            - baz
Generation Details

Using the open-api-generator gradle plugin with the following options (snippets of build.gradle.kts):

openApiGenerate {
    inputSpec.set(genSpec)
    generatorName.set("kotlin-server")
    library.set("ktor")
    additionalProperties.set(
        mapOf(
            "artifactId" to "org.acme",
            "artifactVersion" to "1.0",
            "groupId" to "org.acme",
            "packageName" to "org.acme.server",
            "serializableModel" to "true"
        )
    )
    outputDir.set(genDir)
}

(using gradle 8.7)

Steps to reproduce
  • setup simple gradle project with the open-api-generator 7.7.0 gradle plugin
  • add the snippets above (genSpec points to the yaml file above)
  • run the "openApiGenerate" gradle task
  • open src/main/kotlin/org/acme/server/models/Foobar.kt
  • observer the problem.
Related issues/PRs

None that I could find.

Suggest a fix

Update the mustache template to fix the code generation. Note that the kotlin generator does not suffer from this problem and generates the constructor correctly. Maybe port this portion of the mustashe template to the kotlin-server generator template?

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 reproducing the issue with the provided OpenAPI declaration and kotlin-server Gradle configuration, then inspect the generated Foobar.kt output. Compare the kotlin-server mustache template with the kotlin generator template, as suggested in the issue. Done means the generated enum for the list property compiles and its variants use the expected scalar values.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.