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.32k stars 6.45k forks source link

[BUG] [Kotlin] [Multiplatform] Enum parameters should use value instead of relying on toString #18236

Open kyhule opened 5 months ago

kyhule commented 5 months ago

Bug Report Checklist

Description

Enum parameters rely on toString for their values opposed to using the value that is encapsulated in the enum object. This is especially problematic when setting the enumPropertyNaming config to something other than the format of the enum values. Given the spec listed below and using UPPERCASE enum property naming, the resulting generated enum class and put function are:

    /**
     * enum for parameter pathEnum
     */
    @Serializable
    enum class PathEnumSomePathPathEnumPut(val value: kotlin.String) {

        @SerialName(value = "some_enum")
        SOME_ENUM("some_enum"),

        @SerialName(value = "different_enum")
        DIFFERENT_ENUM("different_enum"),

        @SerialName(value = "another_enum")
        ANOTHER_ENUM("another_enum")

    }

    /**
     * 
     * 
     * @param pathEnum Path enum
     * @return void
     */
    open suspend fun somePathPathEnumPut(pathEnum: PathEnumSomePathPathEnumPut): HttpResponse<Unit> {

        val localVariableAuthNames = listOf<String>()

        val localVariableBody = 
            io.ktor.client.utils.EmptyContent

        val localVariableQuery = mutableMapOf<String, List<String>>()
        val localVariableHeaders = mutableMapOf<String, String>()

        val localVariableConfig = RequestConfig<kotlin.Any?>(
            RequestMethod.PUT,
            "/some/path/{path_enum}".replace("{" + "path_enum" + "}", "$pathEnum"),
            query = localVariableQuery,
            headers = localVariableHeaders,
            requiresAuthentication = false,
        )

        return request(
            localVariableConfig,
            localVariableBody,
            localVariableAuthNames
        ).wrap()
    }

For example, for a call like somePathPathEnumPut(PathEnumSomePathPathEnumPut.SOME_ENUM):

Expected: https://example.com/some/path/some_enum

Actual: https://example.com/some/path/SOME_ENUM

openapi-generator version

Using gradle plugin versions 7.2.0, 7.3.0, and 7.4.0

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Test API
  version: '1'
paths:
  /some/path/{path_enum}:
    put:
      parameters:
        - name: path_enum
          in: path
          description: 'Path enum'
          schema:
            type: string
            enum:
              - 'some_enum'
              - 'different_enum'
              - 'another_enum'
          required: true
      responses:

        204:
          description: 'Processed.'
Generation Details
Steps to reproduce

Just generate the code using the settings given above.

Related issues/PRs

Could not find one though

Suggest a fix

I am working around this issue by changing the generated code from the template here to use paramName.value instead.

grEvenX commented 4 months ago

There are at least one more issue with enums on the kotlin generator that have hit us hard lately 😓 I have a fix for both, not sure if it's the proper fix or not since I suspect enums could in general be handled in the wrong way throughout the codebase atm, but I'll push up a PR to discuss the approach there.