question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue
Description

The changes in pull request https://github.com/OpenAPITools/openapi-generator/pull/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

https://github.com/OpenAPITools/openapi-generator/pull/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!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
4brunucommented, Feb 8, 2021

Thanks for the heads up, I assumed wrongly that this was solved.

0reactions
maartenapplecommented, May 6, 2021

@4brunu I can confirm that https://github.com/OpenAPITools/openapi-generator/pull/9415 fixes the issue!! 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin Warning: Unnecessary safe call on a non-null receiver ...
Declares an instance of type nullable Marker . Hence, in your case, the null safety ? check is redundant because mLastMarker cannot be...
Read more >
Incorrect warning 'Unnecessary safe call on a non-null ...
Incorrect warning 'Unnecessary safe call on a non-null receiver of type Result<String>'. Idea wrongly shows the warning for the following code:.
Read more >
Kotlin Warning: Unnecessary safe call on a non-null receiver ...
Declares an instance of type nullable Marker . Hence, in your case, the null safety ? check is redundant because mLastMarker cannot be...
Read more >
Kotlin — Null safe calls and why you should be cautious when ...
In this way, it will be clear to end-users that something went wrong with the app and not just show them a blank...
Read more >
Safe Call and Elvis Operator usage in Kotlin - ProAndroidDev
This article is for those who have a basic understanding of ' Nullability '. The only reason I am writing this article is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found