mcarleio / konvert

This kotlin compiler plugin is using KSP API and generates kotlin code to map one class to another
https://mcarleio.github.io/konvert/
Apache License 2.0
93 stars 8 forks source link

Unresolved reference when using KonvertFrom on companion object for nested dtos #75

Closed Karkoon closed 5 months ago

Karkoon commented 5 months ago

Hi. I have this structure:

class GalleryDto(
  val id: UUID,
  val name: String,
  val description: String,
  val images: Map<UUID, ImageDto>
) {
  @KonvertFrom(Gallery::class)
  companion object
}

class ImageDto(
  val id: UUID,
  val name: String,
  val url: URL,
) {
  @KonvertFrom(Image::class)
  companion object
}

And the generate code is this:

@GeneratedKonverter(priority = 4_000)
public fun GalleryDto.Companion.fromGallery(gallery: Gallery): GalleryDto = GalleryDto(
  id = gallery.id,
  name = gallery.name,
  description = gallery.description,
  images = gallery.images.mapValues { (_, it) ->
      dev.karkoon.notes.gallery.ImageDto.dev.karkoon.notes.gallery.fromImage(image = it) }
)

The line: dev.karkoon.notes.gallery.ImageDto.dev.karkoon.notes.gallery.fromImage(image = it) generates an error.

The second "dev" in this line produces an unresolved reference. If I remove the whole second "dev.karkoon.notes.gallery" it works.

I'm using the 3.2.0 version. Thank you for creating this library.

mcarleio commented 5 months ago

Thanks for raising this issue, I could reproduce and implemented a fix. It was a bug in the MapToX converter, where a String was concatenated with a kotlinpoet CodeBlock instead of using the %L placeholder. Without the placeholder, the CodeBlock will be converted toString and is unaware of packages, imports, etc.