Skip to content

[BUG][Kotlin] kotlin generator causes warning: 'Unnecessary safe call on a non-null receiver of type ...' #8600

Description

@maartenapple
Description

The changes in pull request #7341 are forcing paramName as an nullable. This causes the compiler to return the following warning for non-nullable objects:
Unnecessary safe call on a non-null receiver of type String

This breaks our ability to build our app using -Werror as a compile option.

openapi-generator version

Version 5.0.0

OpenAPI declaration file content or url

PlayerTokenParam is a required header parameter, so non-nullable:

  /emailChange:
    post:
      description: "Change Email."
      operationId: "ChangeEmail"
      parameters:
      - $ref: '#/components/parameters/PlayerTokenParam'
      ...
playerTokenParam:
      in: header
      name: playerToken
      description: session token for the current player
      required: true
      schema:
        type: string

This generates (where playerToken? causes the warning):

 fun changeEmail(playerToken: kotlin.String, changeEmailRequest: ChangeEmailRequest) : Unit {
        val localVariableBody: kotlin.Any? = changeEmailRequest
        val localVariableQuery: MultiValueMap = mutableMapOf()
        val localVariableHeaders: MutableMap<String, String> = mutableMapOf()
        playerToken?.apply { localVariableHeaders["playerToken"] = this.toString() }
Generation Details

Gradle script task:

task generateApi(type: GenerateTask){
    generatorName = "kotlin"
    inputSpec = <input-spec-file>
    outputDir = <output-dir>
    packageName = <package-name>
    validateSpec = true
    configOptions = [
        sourceFolder: <source-folder>,
        dateLibrary: "threetenbp",
        parcelizeModels: "true",
        enumPropertyNaming: "UPPERCASE"
    ]
}
Related issues/PRs

#7341

Suggest a fix

I think modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache should not asume the object is a nullable object: {{{paramName}}}?.apply {. I'm not sure how to fix it though!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions