Report json name if different from property name in kotlin (#917)

* Report json name in code gen if different from property name

Resolves #800

* Unify required property name error message

* Report json name in kotlinjsonadapter, match code gen

* Upper case JSON

Co-Authored-By: Jake Wharton <jakew@google.com>

* Upper case JSON

Co-Authored-By: Jake Wharton <jakew@google.com>

* Don't keep constants

* Inline json name - property name comparison to util methods

* Remove unnecessary constructor keyword

* Consolidate non-null/missing property tests to parameterized suite

* Add custom json name tests for nonNull property checks

* Rename test to make maven happy

Maven won't run the test unless it ends with `Test` or `TestCase`
This commit is contained in:
Zac Sweers
2019-09-30 23:04:21 -04:00
committed by GitHub
parent 336ca952b0
commit 7804d74318
6 changed files with 310 additions and 254 deletions

View File

@@ -83,8 +83,11 @@ internal class KotlinJsonAdapter<T>(
values[index] = binding.adapter.fromJson(reader)
if (values[index] == null && !binding.property.returnType.isMarkedNullable) {
throw JsonDataException(
"Non-null value '${binding.property.name}' was null at ${reader.path}")
throw Util.unexpectedNull(
binding.property.name,
binding.jsonName,
reader
)
}
}
reader.endObject()
@@ -93,8 +96,11 @@ internal class KotlinJsonAdapter<T>(
for (i in 0 until constructorSize) {
if (values[i] === ABSENT_VALUE && !constructor.parameters[i].isOptional) {
if (!constructor.parameters[i].type.isMarkedNullable) {
throw JsonDataException(
"Required value '${constructor.parameters[i].name}' missing at ${reader.path}")
throw Util.missingProperty(
constructor.parameters[i].name,
bindings[i]?.jsonName,
reader
)
}
values[i] = null // Replace absent with null.
}
@@ -130,6 +136,7 @@ internal class KotlinJsonAdapter<T>(
data class Binding<K, P>(
val name: String,
val jsonName: String?,
val adapter: JsonAdapter<P>,
val property: KProperty1<K, P>,
val parameter: KParameter?) {
@@ -245,8 +252,13 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory {
resolvedPropertyType, Util.jsonAnnotations(allAnnotations.toTypedArray()), property.name)
@Suppress("UNCHECKED_CAST")
bindingsByName[property.name] = KotlinJsonAdapter.Binding(name, adapter,
property as KProperty1<Any, Any?>, parameter)
bindingsByName[property.name] = KotlinJsonAdapter.Binding(
name,
jsonAnnotation?.name ?: name,
adapter,
property as KProperty1<Any, Any?>,
parameter
)
}
val bindings = ArrayList<KotlinJsonAdapter.Binding<Any, Any?>?>()