Fix infinite loop with type var variance stripping + remove from API (#1246)

* Add test case

* Add TypeVariableResolver to handle backward referencing type vars

* Copy nullability in typevars
This commit is contained in:
Zac Sweers
2020-10-04 18:10:49 -04:00
committed by GitHub
parent 5ce092857a
commit f17e7c2584
4 changed files with 121 additions and 51 deletions

View File

@@ -68,7 +68,7 @@ class DualKotlinTest(useReflection: Boolean) {
// Prevent falling back to generated adapter lookup
val rawType = Types.getRawType(type)
val metadataClass = Class.forName("kotlin.Metadata") as Class<out Annotation>
check(!rawType.isAnnotationPresent(metadataClass)) {
check(rawType.isEnum || !rawType.isAnnotationPresent(metadataClass)) {
"Unhandled Kotlin type in reflective test! $rawType"
}
return moshi.nextAdapter<Any>(this, type, annotations)
@@ -585,6 +585,33 @@ class DualKotlinTest(useReflection: Boolean) {
@JsonClass(generateAdapter = true)
data class OutDeclaration<out T>(val input: T)
// Regression test for https://github.com/square/moshi/issues/1244
@Test fun backwardReferencingTypeVarsAndIntersectionTypes() {
val adapter = moshi.adapter<IntersectionTypes<IntersectionTypesEnum>>()
@Language("JSON")
val testJson =
"""{"value":"VALUE"}"""
val instance = IntersectionTypes(IntersectionTypesEnum.VALUE)
assertThat(adapter.serializeNulls().toJson(instance))
.isEqualTo(testJson)
val result = adapter.fromJson(testJson)!!
assertThat(result).isEqualTo(instance)
}
interface IntersectionTypeInterface<E : Enum<E>>
enum class IntersectionTypesEnum : IntersectionTypeInterface<IntersectionTypesEnum> {
VALUE
}
@JsonClass(generateAdapter = true)
data class IntersectionTypes<E>(
val value: E
) where E : Enum<E>, E : IntersectionTypeInterface<E>
}
typealias TypeAlias = Int