Skip to content
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

Class names duplicated when generating models in single file #413

Open
matteo-bloomandwild opened this issue Jan 10, 2023 · 1 comment
Open

Comments

@matteo-bloomandwild
Copy link

Hello,
I used to use this plugin by letting it generate all models in one kotlin file, which worked great until I had to serialise a json that contains multiple fields named data, which results in multiple inner classes of the same name.

@JsonClass(generateAdapter = true)
data class Test(
  @Json(name = "data")
  val `data`: List<Data?>? = null,
  ...
) {
  @JsonClass(generateAdapter = true)
  data class Data(
    ...
  ) {
    ...
  
      @JsonClass(generateAdapter = true)
      data class SomeInnerClass(
        @Json(name = "data")
        val `data`: Data? = null
      ) {
        @JsonClass(generateAdapter = true)
        data class Data(
          ...
        )
      }
      ...

While the generated file passes the IDE's compile-checks, because the kotlin compiler is smart enough to apply the correct scope of each subclass, the kapt processor explodes.

Caused by: java.lang.IllegalStateException: No type element found for: <my.class.package>.Data.
	at com.squareup.moshi.kotlinpoet.metadata.classinspectors.ElementsClassInspector.declarationContainerFor(ElementsClassInspector.kt:115)
	at com.squareup.moshi.kotlinpoet.metadata.specs.ClassInspectorKt.classFor(ClassInspector.kt:112)
	at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec(KotlinPoetMetadataSpecs.kt:470)
	at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec(KotlinPoetMetadataSpecs.kt:476)
	at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec(KotlinPoetMetadataSpecs.kt:476)
	at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec(KotlinPoetMetadataSpecs.kt:476)
	at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec(KotlinPoetMetadataSpecs.kt:182)
	at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec$default(KotlinPoetMetadataSpecs.kt:178)
	at com.squareup.moshi.kotlin.codegen.apt.MoshiCachedClassInspector.toTypeSpec(MoshiCachedClassInspector.kt:47)
	at com.squareup.moshi.kotlin.codegen.apt.MetadataKt.targetType(metadata.kt:205)
	at com.squareup.moshi.kotlin.codegen.apt.JsonClassCodegenProcessor.adapterGenerator(JsonClassCodegenProcessor.kt:141)
	at com.squareup.moshi.kotlin.codegen.apt.JsonClassCodegenProcessor.process(JsonClassCodegenProcessor.kt:107)
	at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt:90)
	at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:197)
	at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:985)
	... 44 more

When generating from the same json but allowing multiple files, the duplicated classes are renamed appending an increasing number of X, which works great with kapt but forces me to have hundreds of files, which is not ideal.

Is there any workaround for this so far, or would it be possible in the future to force the plugin to append the Xs to duplicated class names even when using a single file?

@wuseal
Copy link
Owner

wuseal commented Feb 11, 2023

Maybe can force or wait for the apt to support this case as this code is legally, is it?😆

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