Skip to content

Prevent imported types containing generic types #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dimber-cais opened this issue Feb 14, 2023 · 1 comment
Open

Prevent imported types containing generic types #22

dimber-cais opened this issue Feb 14, 2023 · 1 comment

Comments

@dimber-cais
Copy link

dimber-cais commented Feb 14, 2023

If a class mapping is performed with generics, for example

configure<JSONSchemaCodegen> {
    packageName.set("com.example")
    classMappings {
        byExtension("kotlin.collections.Map<String, Any?>", "x-polymorphic", "true")
    }
}

Then the resulting import in the generated code looks like:

package com.example

import kotlin.collections.Map<String, Any?>

data class Example(
   ...
)

I think it might be an easy fix to change the following function in Target from:

    fun addImport(classId: ClassId) {
        if (!samePackage(classId))
            imports.addOnce(classId.qualifiedClassName)
        localImports.addOnce(classId)
    }

to:

    fun addImport(classId: ClassId) {
        if (!samePackage(classId))
            imports.addOnce(classId.qualifiedClassName.substringBefore('<'))
        localImports.addOnce(classId)
    }

Alternatively, this could be changed to be stripped off when processing the className within the various CustomClass sublcass constructors.

@pwall567
Copy link
Owner

I think allowing generics would be more difficult than you suggest, but the main reason I haven't put any effort into it is that there is a simple workaround.

If you create a typealias as follows:

typealias MapStringAny = Map<String, Any>

then you can use MapStringAny in your classMappings setting. It may not be ideal, but it gets around the problem with relatively little effort.

-Peter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants