OpenAPITools / OpenAPITools/openapi-generator

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

Open
#18,236 3 comments 7 reactions 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
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • 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.

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 in modules/openapi-generator/src/main/resources/kotlin-client/libraries/multiplatform/api.mustache around the path-parameter generation shown in the issue. Reproduce with the supplied OpenAPI declaration and UPPERCASE enumPropertyNaming; done means the generated request uses the enum's declared value, producing some_enum rather than SOME_ENUM.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
tooling
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.