Skip to content

Aliased import ignored for nullable variants of that type #2020

@mcarleio

Description

@mcarleio

Describe the bug
An aliased import added via addAliasedImport is not used when e.g. function arguments or return types are a nullable version of that:

To Reproduce

package org.example

import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.asTypeName

class PersonId

fun main() {
    println(
        FileSpec.builder("org.example", "SomeFile")
            .addAliasedImport(PersonId::class, "PID")
            .addFunction(
                FunSpec.builder("pid")
                    .addParameter("pid", PersonId::class.asTypeName().copy(nullable = true))
                    .returns(PersonId::class.asTypeName().copy(nullable = true))
                    .addCode("return null")
                    .build()
            )
            .build()
    )
}

Expected behavior
Use the import alias for nullable types:

package org.example

import org.example.PersonId as PID

public fun pid(pid: PID?): PID? = null

Actual behavior
Does not use the import alias for nullable types:

package org.example

import org.example.PersonId as PID

public fun pid(pid: org.example.PersonId?): org.example.PersonId? = null

Additional context
Used version 2.0.0 above.
Worked with version 1.16.0.
Broken since 1.17.0.

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