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

@@ -203,7 +203,7 @@ internal class AdapterGenerator(
result.addStatement("%N = %N.fromJson(%N)",
property.localName, nameAllocator[property.delegateKey], readerParam)
} else {
val exception = unexpectedNull(property.localName, readerParam)
val exception = unexpectedNull(property, readerParam)
result.addStatement("%N = %N.fromJson(%N) ?: throw·%L",
property.localName, nameAllocator[property.delegateKey], readerParam, exception)
}
@@ -221,7 +221,7 @@ internal class AdapterGenerator(
result.addStatement("%L -> %N = %N.fromJson(%N)",
propertyIndex, property.localName, nameAllocator[property.delegateKey], readerParam)
} else {
val exception = unexpectedNull(property.localName, readerParam)
val exception = unexpectedNull(property, readerParam)
result.addStatement("%L -> %N = %N.fromJson(%N) ?: throw·%L",
propertyIndex, property.localName, nameAllocator[property.delegateKey], readerParam,
exception)
@@ -308,7 +308,8 @@ internal class AdapterGenerator(
}
if (!property.isTransient && property.isRequired) {
val missingPropertyBlock =
CodeBlock.of("%T.missingProperty(%S, %N)", Util::class, property.localName, readerParam)
CodeBlock.of("%T.missingProperty(%S, %S, %N)",
MOSHI_UTIL, property.localName, property.jsonName, readerParam)
result.addCode(" ?: throw·%L", missingPropertyBlock)
}
separator = ",\n"
@@ -341,8 +342,9 @@ internal class AdapterGenerator(
return result.build()
}
private fun unexpectedNull(identifier: String, reader: ParameterSpec): CodeBlock {
return CodeBlock.of("%T.unexpectedNull(%S, %N)", Util::class, identifier, reader)
private fun unexpectedNull(property: PropertyGenerator, reader: ParameterSpec): CodeBlock {
return CodeBlock.of("%T.unexpectedNull(%S, %S, %N)",
MOSHI_UTIL, property.localName, property.jsonName, reader)
}
private fun generateToJsonFun(): FunSpec {