Fail on sealed classes in KotlinJsonAdapterFactory and code gen (#914)

Resolves #906
This commit is contained in:
Zac Sweers
2019-09-12 20:38:46 -04:00
committed by GitHub
parent a67b4d6a72
commit dd0e3807d3
4 changed files with 41 additions and 0 deletions

View File

@@ -1113,6 +1113,21 @@ class KotlinJsonAdapterTest {
@JvmSuppressWildcards(suppress = false)
data class MapOfStringToClassCodegen(val map: Map<String, ConstructorParameters> = mapOf())
@Test fun sealedClassesAreRejected() {
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
try {
moshi.adapter<SealedClass>()
fail()
} catch (e: IllegalArgumentException) {
assertThat(e).hasMessageContaining("Cannot reflectively serialize sealed class")
}
}
sealed class SealedClass
private fun <T> mapWildcardsParameterizedTest(type: Class<T>, json: String, value: T) {
// Ensure the map was created with the expected wildcards of a Kotlin map.
val fieldType = type.getDeclaredField("map").genericType