Fix error message for assigning to non-null properties.

instead of falling down to "Required property 'a' missing at $"
This commit is contained in:
Eric Cochran
2018-04-24 13:04:10 -07:00
parent 1c68437f3c
commit 51d23b5b33
3 changed files with 74 additions and 4 deletions

View File

@@ -181,13 +181,27 @@ internal class AdapterGenerator(
propertyList.forEachIndexed { index, property -> propertyList.forEachIndexed { index, property ->
if (property.differentiateAbsentFromNull) { if (property.differentiateAbsentFromNull) {
result.beginControlFlow("%L -> ", index) result.beginControlFlow("%L -> ", index)
result.addStatement("%N = %N.fromJson(%N)", if (property.delegateKey.nullable) {
property.localName, nameAllocator.get(property.delegateKey), readerParam) 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.addStatement("%N = true", property.localIsPresentName)
result.endControlFlow() result.endControlFlow()
} else { } else {
result.addStatement("%L -> %N = %N.fromJson(%N)", if (property.delegateKey.nullable) {
index, property.localName, nameAllocator.get(property.delegateKey), readerParam) 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)
}
} }
} }

View File

@@ -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) @JsonClass(generateAdapter = true)
class HasNonNullConstructorParameter(val a: String) 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) @JsonClass(generateAdapter = true)
class HasNonNullProperty { class HasNonNullProperty {
var a: String = "" var a: String = ""

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) class HasNonNullConstructorParameter(val a: String)
@Test fun nonNullPropertySetToNullFailsWithJsonDataException() { @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 { class HasNonNullProperty {
var a: String = "" var a: String = ""
} }