mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 16:39:22 +08:00
Crash earlier for property type conflicts. (#377)
This commit is contained in:
committed by
Jesse Wilson
parent
a00860ee1d
commit
834a401122
@@ -206,6 +206,11 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (parameter != null && parameter.type != property.returnType) {
|
||||||
|
throw IllegalArgumentException("'${property.name}' has a constructor parameter of type " +
|
||||||
|
"${parameter.type} but a property of type ${property.returnType}.")
|
||||||
|
}
|
||||||
|
|
||||||
if (property !is KMutableProperty1 && parameter == null) continue
|
if (property !is KMutableProperty1 && parameter == null) continue
|
||||||
|
|
||||||
property.isAccessible = true
|
property.isAccessible = true
|
||||||
|
@@ -346,6 +346,21 @@ class KotlinJsonAdapterTest {
|
|||||||
var b: Int = -1
|
var b: Int = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test fun constructorParametersAndPropertiesWithSameNamesMustHaveSameTypes() {
|
||||||
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||||
|
try {
|
||||||
|
moshi.adapter(ConstructorParameterWithSameNameAsPropertyButDifferentType::class.java)
|
||||||
|
fail()
|
||||||
|
} catch (expected: IllegalArgumentException) {
|
||||||
|
assertThat(expected).hasMessage("'a' has a constructor parameter of type " +
|
||||||
|
"kotlin.Int but a property of type kotlin.String.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ConstructorParameterWithSameNameAsPropertyButDifferentType(a: Int) {
|
||||||
|
var a = "boo"
|
||||||
|
}
|
||||||
|
|
||||||
@Test fun supertypeConstructorParameters() {
|
@Test fun supertypeConstructorParameters() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||||
val jsonAdapter = moshi.adapter(SubtypeConstructorParameters::class.java)
|
val jsonAdapter = moshi.adapter(SubtypeConstructorParameters::class.java)
|
||||||
|
Reference in New Issue
Block a user