Skip to content

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

@valpackett

Description

@valpackett
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

7747cc9

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions