mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Merge pull request #511 from square/eric.non-null
Fix error message for assigning to non-null properties.
This commit is contained in:
@@ -181,13 +181,27 @@ internal class AdapterGenerator(
|
||||
propertyList.forEachIndexed { index, property ->
|
||||
if (property.differentiateAbsentFromNull) {
|
||||
result.beginControlFlow("%L -> ", index)
|
||||
result.addStatement("%N = %N.fromJson(%N)",
|
||||
property.localName, nameAllocator.get(property.delegateKey), readerParam)
|
||||
if (property.delegateKey.nullable) {
|
||||
result.addStatement("%N = %N.fromJson(%N)",
|
||||
property.localName, nameAllocator.get(property.delegateKey), readerParam)
|
||||
} else {
|
||||
result.addStatement("%N = %N.fromJson(%N)" +
|
||||
" ?: throw %T(\"Non-null value '%N' was null at \${%N.path}\")",
|
||||
property.localName, nameAllocator.get(property.delegateKey), readerParam,
|
||||
JsonDataException::class, property.localName, readerParam)
|
||||
}
|
||||
result.addStatement("%N = true", property.localIsPresentName)
|
||||
result.endControlFlow()
|
||||
} else {
|
||||
result.addStatement("%L -> %N = %N.fromJson(%N)",
|
||||
index, property.localName, nameAllocator.get(property.delegateKey), readerParam)
|
||||
if (property.delegateKey.nullable) {
|
||||
result.addStatement("%L -> %N = %N.fromJson(%N)",
|
||||
index, property.localName, nameAllocator.get(property.delegateKey), readerParam)
|
||||
} else {
|
||||
result.addStatement("%L -> %N = %N.fromJson(%N)" +
|
||||
" ?: throw %T(\"Non-null value '%N' was null at \${%N.path}\")",
|
||||
index, property.localName, nameAllocator.get(property.delegateKey), readerParam,
|
||||
JsonDataException::class, property.localName, readerParam)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -383,6 +383,20 @@ class GeneratedAdaptersTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun nonNullConstructorParameterCalledWithNullFromAdapterFailsWithJsonDataException() {
|
||||
val moshi = Moshi.Builder().add(object {
|
||||
@FromJson fun fromJson(string: String): String? = null
|
||||
}).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 \$.a")
|
||||
}
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class HasNonNullConstructorParameter(val a: String)
|
||||
|
||||
@@ -539,6 +553,20 @@ class GeneratedAdaptersTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun nonNullPropertySetToNullFromAdapterFailsWithJsonDataException() {
|
||||
val moshi = Moshi.Builder().add(object {
|
||||
@FromJson fun fromJson(string: String): String? = null
|
||||
}).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 \$.a")
|
||||
}
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class HasNonNullProperty {
|
||||
var a: String = ""
|
||||
|
Reference in New Issue
Block a user