Change DelegateKey to use AnnotationSpec instead of AnnotationMirror

AnnotationSpec implements equals() in the way we need, but
AnnotationMirror doesn't. As a consequence this fixes a problem
where we were generating redundant adapters.

Closes: https://github.com/square/moshi/issues/563
This commit is contained in:
Jesse Wilson
2018-09-09 13:08:19 -04:00
parent 4f3c418202
commit 7382145318
5 changed files with 70 additions and 41 deletions

View File

@@ -32,6 +32,7 @@ import org.junit.Assert.fail
import org.junit.Ignore
import org.junit.Test
import java.util.Locale
import kotlin.reflect.full.memberProperties
class GeneratedAdaptersTest {
@@ -857,6 +858,28 @@ class GeneratedAdaptersTest {
throw AssertionError()
}
/** https://github.com/square/moshi/issues/563 */
@Test fun qualifiedAdaptersAreShared() {
val moshi = Moshi.Builder()
.add(UppercaseJsonAdapter())
.build()
val jsonAdapter = moshi.adapter(MultiplePropertiesShareAdapter::class.java)
val encoded = MultiplePropertiesShareAdapter("Android", "Banana")
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"a":"ANDROID","b":"BANANA"}""")
val delegateAdapters = jsonAdapter::class.memberProperties.filter {
it.returnType.classifier == JsonAdapter::class
}
assertThat(delegateAdapters).hasSize(1)
}
@JsonClass(generateAdapter = true)
class MultiplePropertiesShareAdapter(
@Uppercase(true) var a: String,
@Uppercase(true) var b: String
)
@Test fun toJsonOnly() {
val moshi = Moshi.Builder()
.add(CustomToJsonOnlyAdapter())