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.02k stars 6.6k forks source link

[BUG] [KOTLIN-CLIENT] enums with empty entries to generate invalid kotlin-client code #18660

Open burntcookie90 opened 6 months ago

burntcookie90 commented 6 months ago

Bug Report Checklist

Description

A spec with an empty enum entry no longer generates a valid enum in kotlin code.

openapi-generator version

7.4.0, also 7.5.0

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: API
  version: 1.0.0
paths:
  /api/v1/foo/:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Foo'
components:
  schemas:
    Foo:
      type: object
      description: foo
      properties:
        field_name:
          nullable: true
          enum:
            - ''
            - stringvalue1
          type: string
Generation Details

will generate this kotlin code:

/**
 *
 * Please note:
 * This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * Do not edit this file manually.
 *
 */

@file:Suppress(
    "ArrayInDataClass",
    "EnumEntryName",
    "RemoveRedundantQualifierName",
    "UnusedImport"
)

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
 * foo
 *
 * @param fieldName 
 */
@JsonClass(generateAdapter = true)

data class ApiFoo (

    @Json(name = "field_name")
    val fieldName: ApiFoo.FieldName? = null

) {

    /**
     * 
     *
     * Values: ,stringvalue1
     */
    @JsonClass(generateAdapter = false)
    enum class FieldName(val value: kotlin.String) {
        @Json(name = "") (""),
        @Json(name = "stringvalue1") stringvalue1("stringvalue1");
    }
}

which has the invalid first entry. Previously this (in 7.3.0) this would generate the following:

/**
 *
 * Please note:
 * This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * Do not edit this file manually.
 *
 */

@file:Suppress(
    "ArrayInDataClass",
    "EnumEntryName",
    "RemoveRedundantQualifierName",
    "UnusedImport"
)

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
 * foo
 *
 * @param fieldName 
 */
@JsonClass(generateAdapter = true)

data class ApiFoo (

    @Json(name = "field_name")
    val fieldName: ApiFoo.FieldName? = null

) {

    /**
     * 
     *
     * Values: eMPTY,stringvalue1
     */
    @JsonClass(generateAdapter = false)
    enum class FieldName(val value: kotlin.String) {
        @Json(name = "") eMPTY(""),
        @Json(name = "stringvalue1") stringvalue1("stringvalue1");
    }
}

with the eMPTY entry.

here is my openApiGenerate task

openApiGenerate {
  generatorName = "kotlin"
  inputSpec = specFile
  outputDir = generationDir
  templateDir = "$templatesDir"
  apiPackage = "com.giftster.network.api"
  packageName = "com.giftster.network"
  cleanupOutput = true
  modelNamePrefix = "Api"
  library = "jvm-retrofit2"
  generateApiTests = false
  generateModelTests = false
  configOptions.set([
    artifactId              : "client",
    dateLibrary             : "java8",
    groupId                 : "com.foo,
    moshiCodeGen            : "true",
    omitGradlePluginVersions: "true",
    omitGradleWrapper       : "true",
    useCoroutines           : "true"
  ])
}
Steps to reproduce

Run the generation on the yaml.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/18062

Suggest a fix

Should revert the enum generation changes or provide guidance on fixing the breaking change.

burntcookie90 commented 6 months ago

setting enumPropertyNaming to camelCase reverts the behavior introudced in #18062. However, I'm leaving this issue open because original should not generate invalid code.