Ktorfit icon indicating copy to clipboard operation
Ktorfit copied to clipboard

Import for <ERROR TYPE: MyResponse<List<AppObjectResponse>, Unit>> not found

Open SomethingNice2016 opened this issue 1 year ago • 6 comments

Ktorfit version

2.1.0

What happened and how can we reproduce this issue?

Hello. At the moment I have a problem. I have a cross-platform application on 2 platforms. I wrote my own wrapper for respons in generics, but at the compilation stage I get an error. The wrapper class is in another module. Please help me solve this problem image

What did you expect to happen?

Happy code generation

Is there anything else we need to know about?

I am using this implementation in an android project. Everything works correctly in it

SomethingNice2016 avatar Oct 12 '24 01:10 SomethingNice2016

Thank you for the report! I tried to reproduce it, but i was not able yet

Foso avatar Oct 12 '24 14:10 Foso

Ktorfit does not generate implementations for all interfaces that are located in another module, even if they do not have generics

SomethingNice2016 avatar Oct 14 '24 09:10 SomethingNice2016

image image

SomethingNice2016 avatar Oct 14 '24 09:10 SomethingNice2016

Can you please try to reproduce it with the Multiplatform project https://github.com/Foso/Ktorfit/tree/master/example/MultiplatformExample . I want to find a fix for that bug, but when i use the classes everything is working fine. https://github.com/Foso/Ktorfit/issues/634 seems to be the same issue

Foso avatar Oct 15 '24 16:10 Foso

Hej Jens,

I just started playing around with Ktorfit today, but I'm running into the same issue here. My project's app is a Compose Multiplatform project (currently targeting Android and JVM), but the DTO models I want to use are located in a separate common module, which is also used by the server.

The project structure is like this

app
  androidApp
  desktopApp
  shared
common
server

The following is part of my server and app settings.gradle.kts, so I can reach the DTO models

include(":common")
project(":common").projectDir = file("../common")

And I then also define a dependency on the project, of course

implementation(project(":common"))

Now take my UserDto, say com.example.common.model.UserDto from the common module, which I want to use in the shared source set within the app project. That works fine, as long as I don't wrap the DTO in some container type when defining my interface.

For example, the following works fine:

@GET("users/current")
suspend fun currentUser(): UserDto

But the moment I change the return type of my function, I get the same errors as shared above. I've tried Flow<UserDto>, Call<UserDto>, and MyOwnVeryFancyWrapperClass<UserDto>, but they all fail in exactly the same way. When I remove the wrapper, it immediately compiles and works fine again! So it probably has to do with resolving the generic type parameter, I guess?

Let me know if you could use any other information, or if I can be of assistance. I've been dabbling with KSP and KotlinPoet for the last few months. I'll see if I can find the time to maybe set up a test environment and do some root cause analysis :)

Kotlin 2.0.21 and Ktorfit 2.2.0 btw, only using ktorfit-lib-light, and the Gradle plugin of course

image

Thijsiez avatar Jan 06 '25 00:01 Thijsiez

The issue seems to be fixed by adding the main source dir of the external common module to commonMain. This is what I added to my app/shared/build.gradle.kts script:

kotlin {
    ...
    sourceSets {
        ...
        commonMain.configure {
            val dirs = kotlin.srcDirs.map { it.path }.toTypedArray()
            kotlin.srcDirs(*dirs, "../../common/src/main")
        }
        commonMain.dependencies {
            ...
            compileOnly(project(":common"))
            implementation(libs.bundles.ktorfit)
        }
        ...
    }
}

This isn't a pretty fix, but at least KSP can now resolve the classes we're trying to reference. Hope this helps someone :)

Thijsiez avatar Jan 07 '25 01:01 Thijsiez