KotlinJsonAdapter shouldn't convert enums.

Closes: https://github.com/square/moshi/issues/284
This commit is contained in:
jwilson
2017-04-21 09:41:41 -05:00
parent d95dd07c56
commit cd1542363d
2 changed files with 19 additions and 7 deletions

View File

@@ -156,10 +156,9 @@ object KotlinJsonAdapterFactory : JsonAdapter.Factory {
if (!annotations.isEmpty()) return null
val rawType = Types.getRawType(type)
val platformType = ClassJsonAdapter.isPlatformType(rawType)
if (platformType) return null
if (rawType.isEnum) return null
if (!rawType.isAnnotationPresent(KOTLIN_METADATA)) return null
if (ClassJsonAdapter.isPlatformType(rawType)) return null
val constructor = rawType.kotlin.primaryConstructor ?: return null
val parametersByName = constructor.parameters.associateBy { it.name }

View File

@@ -178,7 +178,7 @@ class KotlinJsonAdapterTest {
class AbsentNull(var a: Int?, var b: Int?)
@Test fun repeatedValue() {
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
val jsonAdapter = moshi.adapter(RepeatedValue::class.java)
try {
@@ -395,7 +395,7 @@ class KotlinJsonAdapterTest {
}
@Test fun privateConstructor() {
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
val jsonAdapter = moshi.adapter(PrivateConstructor::class.java)
val encoded = PrivateConstructor.newInstance(3, 5)
@@ -446,7 +446,7 @@ class KotlinJsonAdapterTest {
}
@Test fun unsettableProperty() {
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
try {
moshi.adapter(UnsettableProperty::class.java)
fail()
@@ -462,7 +462,7 @@ class KotlinJsonAdapterTest {
}
@Test fun nonPropertyConstructorParameter() {
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
try {
moshi.adapter(NonPropertyConstructorParameter::class.java)
fail()
@@ -475,6 +475,19 @@ class KotlinJsonAdapterTest {
class NonPropertyConstructorParameter(a: Int, val b: Int)
@Test fun kotlinEnumsAreNotCovered() {
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
val adapter = moshi.adapter(UsingEnum::class.java)
assertThat(adapter.fromJson("""{"e": "A"}""")).isEqualTo(UsingEnum(KotlinEnum.A))
}
data class UsingEnum(val e: KotlinEnum)
enum class KotlinEnum {
A, B
}
// TODO(jwilson): resolve generic types?
@Retention(RUNTIME)