mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Correct error for duplicate JSON key for Kotlin. (#789)
Before, the KotlinJsonAdapter threw "java.lang.IndexOutOfBoundsException: Index: 0, Size: 0" on a duplicate key when the key matched a property that was not a constructor parameter.
This commit is contained in:
@@ -818,20 +818,37 @@ class GeneratedAdaptersTest {
|
||||
}
|
||||
|
||||
/** Generated adapters don't track enough state to detect duplicated values. */
|
||||
@Ignore @Test fun duplicatedValue() {
|
||||
@Ignore @Test fun duplicatedValueParameter() {
|
||||
val moshi = Moshi.Builder().build()
|
||||
val jsonAdapter = moshi.adapter(DuplicateValue::class.java)
|
||||
val jsonAdapter = moshi.adapter(DuplicateValueParameter::class.java)
|
||||
|
||||
try {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
fail()
|
||||
} catch(expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for a at $.a")
|
||||
assertThat(expected).hasMessage("Multiple values for 'a' at $.a")
|
||||
}
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class DuplicateValue(var a: Int = -1, var b: Int = -2)
|
||||
class DuplicateValueParameter(var a: Int = -1, var b: Int = -2)
|
||||
|
||||
/** Generated adapters don't track enough state to detect duplicated values. */
|
||||
@Ignore @Test fun duplicatedValueProperty() {
|
||||
val moshi = Moshi.Builder().build()
|
||||
val jsonAdapter = moshi.adapter(DuplicateValueProperty::class.java)
|
||||
|
||||
try {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
fail()
|
||||
} catch(expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'a' at $.a")
|
||||
}
|
||||
}
|
||||
|
||||
class DuplicateValueProperty {
|
||||
var a: Int = -1
|
||||
var b: Int = -2
|
||||
}
|
||||
|
||||
@Test fun extensionProperty() {
|
||||
val moshi = Moshi.Builder().build()
|
||||
|
@@ -205,9 +205,9 @@ class KotlinJsonAdapterTest {
|
||||
var a: String = ""
|
||||
}
|
||||
|
||||
@Test fun duplicatedValue() {
|
||||
@Test fun duplicatedValueParameter() {
|
||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
val jsonAdapter = moshi.adapter(DuplicateValue::class.java)
|
||||
val jsonAdapter = moshi.adapter(DuplicateValueParameter::class.java)
|
||||
|
||||
try {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
@@ -217,7 +217,24 @@ class KotlinJsonAdapterTest {
|
||||
}
|
||||
}
|
||||
|
||||
class DuplicateValue(var a: Int = -1, var b: Int = -2)
|
||||
class DuplicateValueParameter(var a: Int = -1, var b: Int = -2)
|
||||
|
||||
@Test fun duplicatedValueProperty() {
|
||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
val jsonAdapter = moshi.adapter(DuplicateValueProperty::class.java)
|
||||
|
||||
try {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
fail()
|
||||
} catch(expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'a' at $.a")
|
||||
}
|
||||
}
|
||||
|
||||
class DuplicateValueProperty {
|
||||
var a: Int = -1
|
||||
var b: Int = -2
|
||||
}
|
||||
|
||||
@Test fun explicitNull() {
|
||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
|
Reference in New Issue
Block a user