Skip to content

Commit

Permalink
Fix #1729. Properly handle type resolving for type parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyhuo committed Sep 14, 2023
1 parent 24cf12c commit 84f0e9e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ private fun resolveTypeArgs(
): TypeName {
val unwrappedType = propertyType.unwrapTypeAlias()

if (unwrappedType !is TypeVariableName) {
if (unwrappedType is ParameterizedTypeName) {
return unwrappedType.copy(typeArguments = unwrappedType.typeArguments.map {
resolveTypeArgs(targetClass, it, resolvedTypes, allowedTypeVars, entryStartIndex)
})
} else if (unwrappedType !is TypeVariableName) {
return unwrappedType
} else if (entryStartIndex == -1) {
return unwrappedType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ private fun declaredProperties(
val result = mutableMapOf<String, TargetProperty>()
for (property in classDecl.getDeclaredProperties()) {
val initialType = property.type.resolve()
val resolvedType = if (initialType.declaration is KSTypeParameter) {
val resolvedType = if (initialType.declaration is KSTypeParameter ||
initialType.arguments.any { it.type?.resolve()?.declaration is KSTypeParameter }
) {
property.asMemberOf(originalType.asType())
} else {
initialType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,28 @@ class JsonClassCodegenProcessorTest {
assertThat(result.messages).contains("JsonQualifier @UpperCase must have RUNTIME retention")
}

@Test
fun genericSuperClassPropertyTypeSubstitution() {
val result = compile(
kotlin(
"source.kt",
"""
package test
import com.squareup.moshi.JsonClass
open class GenericSuperClass<T> {
var t: T? = null
var list: List<T>? = null
}
@JsonClass(generateAdapter = true)
class SubClassOfGeneric : GenericSuperClass<String>()
""",
),
)
assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
}

@Test
fun `TypeAliases with the same backing type should share the same adapter`() {
val result = compile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,28 @@ class JsonClassSymbolProcessorTest {
assertThat(result.messages).contains("Error preparing ElementEnvelope")
}

@Test
fun genericSuperClassPropertyTypeSubstitution() {
val result = compile(
kotlin(
"source.kt",
"""
package test
import com.squareup.moshi.JsonClass
open class GenericSuperClass<T> {
var t: T? = null
var list: List<T>? = null
}
@JsonClass(generateAdapter = true)
class SubClassOfGeneric : GenericSuperClass<String>()
""",
),
)
assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
}

@Test
fun `TypeAliases with the same backing type should share the same adapter`() {
val result = compile(
Expand Down

0 comments on commit 84f0e9e

Please sign in to comment.