The following model class definition fails with Moshi 1.9.0 and above:
@JsonClass(generateAdapter = true)
data class Model(@Json(name = "id") val id: String) {
inline fun <reified T> inlineMethod(named: String): T? = null
}
The fields on the class & the body of its inline function are irrelevant - instead, it seems like the presence of the reified keyword will cause Moshi's codegen to fail with the following message:
e: [kapt] An exception occurred: java.util.NoSuchElementException: Key 0 is missing in the map.
at kotlin.collections.MapsKt__MapWithDefaultKt.getOrImplicitDefaultNullable(MapWithDefault.kt:24)
at kotlin.collections.MapsKt__MapsKt.getValue(Maps.kt:294)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toFunSpec(KotlinPoetMetadataSpecs.kt:543)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.access$toFunSpec(KotlinPoetMetadataSpecs.kt:1)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs$toTypeSpec$26.invoke(KotlinPoetMetadataSpecs.kt:429)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs$toTypeSpec$26.invoke(KotlinPoetMetadataSpecs.kt)
at kotlin.sequences.TransformingSequence$iterator$1.next(Sequences.kt:172)
at com.squareup.kotlinpoet.TypeSpec$Builder.addFunctions(TypeSpec.kt:786)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec(KotlinPoetMetadataSpecs.kt:403)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec(KotlinPoetMetadataSpecs.kt:173)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec$default(KotlinPoetMetadataSpecs.kt:171)
at com.squareup.moshi.kotlin.codegen.MetadataKt.targetType(metadata.kt:153)
at com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor.adapterGenerator(JsonClassCodegenProcessor.kt:133)
at com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor.process(JsonClassCodegenProcessor.kt:107)
at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt)
(More kapt stuff below this line...)
On 1.8.0, code generation and compilation are successful.
As a workaround, the reified inline fun can be defined outside of the model class, as an extension function:
@JsonClass(generateAdapter = true)
data class Model(@Json(name = "id") val id: String)
// This works
inline fun <reified T> Model.inlineMethod(named: String): T? = null
Can you try the master snapshot and 1.9.1? We fixed an issue around type
variables last week.
On Wed, Nov 6, 2019 at 4:03 AM Marcel Schnelle notifications@github.com
wrote:
The following model class definition fails with Moshi 1.9.0 and above:
@JsonClass(generateAdapter = true)data class Model(@Json(name = "id") val id: String) {
inline funinlineMethod(named: String): T? = null
}The fields on the class & the body of its inline function are irrelevant -
instead, it seems like the presence of the reified keyword will cause
Moshi's codegen to fail with the following message:e: [kapt] An exception occurred: java.util.NoSuchElementException: Key 0 is missing in the map.
at kotlin.collections.MapsKt__MapWithDefaultKt.getOrImplicitDefaultNullable(MapWithDefault.kt:24)
at kotlin.collections.MapsKt__MapsKt.getValue(Maps.kt:294)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toFunSpec(KotlinPoetMetadataSpecs.kt:543)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.access$toFunSpec(KotlinPoetMetadataSpecs.kt:1)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs$toTypeSpec$26.invoke(KotlinPoetMetadataSpecs.kt:429)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs$toTypeSpec$26.invoke(KotlinPoetMetadataSpecs.kt)
at kotlin.sequences.TransformingSequence$iterator$1.next(Sequences.kt:172)
at com.squareup.kotlinpoet.TypeSpec$Builder.addFunctions(TypeSpec.kt:786)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec(KotlinPoetMetadataSpecs.kt:403)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec(KotlinPoetMetadataSpecs.kt:173)
at com.squareup.moshi.kotlinpoet.metadata.specs.KotlinPoetMetadataSpecs.toTypeSpec$default(KotlinPoetMetadataSpecs.kt:171)
at com.squareup.moshi.kotlin.codegen.MetadataKt.targetType(metadata.kt:153)
at com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor.adapterGenerator(JsonClassCodegenProcessor.kt:133)
at com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor.process(JsonClassCodegenProcessor.kt:107)
at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt)
(More kapt stuff below this line...)On 1.8.0, code generation and compilation are successful.
As a workaround, the reified inline fun can be defined outside of the
model class, as an extension function:@JsonClass(generateAdapter = true)data class Model(@Json(name = "id") val id: String)
// This worksinline funModel.inlineMethod(named: String): T? = null —
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/1008?email_source=notifications&email_token=AAKMJPXSTBM3DKJVYXEQZX3QSKXA7A5CNFSM4JJT5EK2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HXGS22A,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAKMJPSCATPIK33GHAVXEHDQSKXA7ANCNFSM4JJT5EKQ
.
Neither 1.9.1 nor the latest 1.10.0-SNAPSHOT seem to change anything about the failure, unfortunately. The stack trace is unchanged compared to the error using 1.9.0 as well.
I'm also seeing something similar with the out variance annotation:
@JsonClass(generateAdapter = true)
data class NodeGlobalApiResponse<out T>
This was working in 1.8.0, but in 1.9.1 I'm getting a compile-time error:
Type parameter T is declared as 'out' but occurs in 'invariant' position in type JsonAdapter<NodeGlobalApiResponse<T>>
@mannodermaus since this is a KotlinPoet issue, do you mind filing there? We'll need to start there
@DanielClayton yours looks like a separate issue. Can you please post a separate issue with the full stacktrace? Also whether or not you tested the current master snapshot
@ZacSweers Thanks! I raised it on KotlinPoet as https://github.com/square/kotlinpoet/issues/829.
@DanielClayton I've extracted your issue to a separate issue in #1009 and opened a PR for it in #1010.
I can confirm that the latest 1.10.0-SNAPSHOT resolves this issue. As soon as 1.9.2 hits jcenter, I will upgrade to that version. Thank you for the quick turnaround!
Most helpful comment
@mannodermaus since this is a KotlinPoet issue, do you mind filing there? We'll need to start there
@DanielClayton yours looks like a separate issue. Can you please post a separate issue with the full stacktrace? Also whether or not you tested the current master snapshot