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

Fix #1729. Properly handle type resolving for type parameters. #1737

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,13 @@ 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