Merge pull request #511 from square/eric.non-null

Fix error message for assigning to non-null properties.
This commit is contained in:
Jesse Wilson
2018-05-04 15:06:35 -04:00
committed by GitHub
3 changed files with 74 additions and 4 deletions

View File

@@ -152,6 +152,20 @@ class KotlinJsonAdapterTest {
}
}
@Test fun nonNullConstructorParameterCalledWithNullFromAdapterFailsWithJsonDataException() {
val moshi = Moshi.Builder().add(object {
@FromJson fun fromJson(string: String): String? = null
}).add(KotlinJsonAdapterFactory()).build()
val jsonAdapter = moshi.adapter(HasNonNullConstructorParameter::class.java)
try {
jsonAdapter.fromJson("{\"a\":\"hello\"}")
fail()
} catch (expected: JsonDataException) {
assertThat(expected).hasMessage("Non-null value 'a' was null at \$")
}
}
class HasNonNullConstructorParameter(val a: String)
@Test fun nonNullPropertySetToNullFailsWithJsonDataException() {
@@ -166,6 +180,20 @@ class KotlinJsonAdapterTest {
}
}
@Test fun nonNullPropertySetToNullFromAdapterFailsWithJsonDataException() {
val moshi = Moshi.Builder().add(object {
@FromJson fun fromJson(string: String): String? = null
}).add(KotlinJsonAdapterFactory()).build()
val jsonAdapter = moshi.adapter(HasNonNullProperty::class.java)
try {
jsonAdapter.fromJson("{\"a\":\"hello\"}")
fail()
} catch (expected: JsonDataException) {
assertThat(expected).hasMessage("Non-null value 'a' was null at \$")
}
}
class HasNonNullProperty {
var a: String = ""
}