openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open kyhule opened this issue 1 year ago • 1 comments

Bug Report Checklist

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

kyhule avatar Mar 26 '24 16:03 kyhule

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.

grEvenX avatar Apr 24 '24 10:04 grEvenX