Fix @field:Json parameters annotations always taking precedence (#946)

* Add regression test for field json annotations

* Make json name null if absent rather than default to name

This fixes incorrect behavior where the parameter json name would always win out over the property value
This commit is contained in:
Zac Sweers
2019-10-05 21:26:43 -04:00
committed by GitHub
parent 9dc331f20c
commit dbed99d71b
5 changed files with 24 additions and 6 deletions

View File

@@ -1126,14 +1126,32 @@ class GeneratedAdaptersTest {
}
@JsonClass(generateAdapter = true)
class InternalPropertyWithoutBackingField {
class InternalPropertyWithoutBackingField {
@Transient
private var foo: Int = 5
internal var bar
get() = foo
set(f) { foo = f}
set(f) {
foo = f
}
}
@JsonClass(generateAdapter = true)
data class ClassWithFieldJson(
@field:Json(name = "_links") val links: String
) {
@field:Json(name = "_ids") var ids: String? = null
}
// Regression test to ensure annotations with field site targets still use the right name
@Test fun classWithFieldJsonTargets() {
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter<ClassWithFieldJson>()
//language=JSON
val instance = adapter.fromJson("""{"_links": "link", "_ids": "id" }""")!!
assertThat(instance).isEqualTo(ClassWithFieldJson("link").apply { ids = "id" })
}
}