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.42k stars 6.48k forks source link

[BUG][kotlin] enums in form-urlencoded bodies generate non-nullable access compile error #18982

Closed valpackett closed 3 months ago

valpackett commented 3 months ago
Description

With a spec that has an enum inside of a property of an application/x-www-form-urlencoded body, the generated Kotlin code does not compile:

> Task :compileKotlin FAILED
e: file:///run/user/1000/s/out/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt:112:51 Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type DefaultApi.VariantWatPost?

FAILURE: Build failed with an exception.

because of the unchecked .value access to a nullable ? parameter

    fun watPostRequestConfig(variant: VariantWatPost?) : RequestConfig<Map<String, PartConfig<*>>> {
        val localVariableBody = mapOf(
            "variant" to PartConfig(body = variant.value, headers = mutableMapOf()),)
        val localVariableQuery: MultiValueMap = mutableMapOf()
        val localVariableHeaders: MutableMap<String, String> = mutableMapOf("Content-Type" to "application/x-www-form-urlencoded")
openapi-generator version

7747cc93e0a6f01715b8f267f91c5aa83fe23a0b

OpenAPI declaration file content or url
openapi: '3.0.0'
info:
  version: 1.0.0
  title: test
paths:
  /wat:
    post:
      responses:
        '200':
          description: OK
          headers: {}
          content:
            text/html:
              schema:
                type: string
      requestBody:
        required: true
        content:
          "application/x-www-form-urlencoded":
            schema:
              type: object
              properties:
                variant:
                  type: string
                  enum:
                    - one
                    - two
Generation Details
java -jar /home/val/.m2/repository/org/openapitools/openapi-generator-cli/7.7.0-SNAPSHOT/openapi-generator-cli-7.7.0-SNAPSHOT.jar generate -i spec.yaml -o out -g kotlin
Steps to reproduce

sh gradlew check that example

Related issues/PRs
Suggest a fix

Should be a simple template fix to generate the ?. safe access, will look into doing it myself