mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
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:
@@ -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" })
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user