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
21.28k stars 6.44k forks source link

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

Open meuter opened 2 months ago

meuter commented 2 months ago

Bug Report Checklist

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
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?

soheilrahsaz commented 1 month ago

Also using java generator and library=native or library=apache-httpclient has this problem. in toUrlQueryString it gets the list of enums, but then instead of using the right enum type, it uses string:

// add `allowedChatTypes` to the URL query string
    if (getAllowedChatTypes() != null) {
      int i = 0;
      for (String _item : getAllowedChatTypes()) {
      ......

String _item is wrong because


  public Set<AllowedChatTypesEnum> getAllowedChatTypes() {
    return allowedChatTypes;
  }

and AllowedChatTypesEnum is a enum.

Using version 7.7.0.