mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Fix a crash when processing a parameter annotated with an annotation that placed in annotation package (#1287)
* Update KotlinPoet to 1.7.2 * Fix a Java NPE crash when processing a parameter annotated with an annotation that placed in annotation package This problem happens with KotlinPoet 1.7.x. From KotlinPoet 1.7.x, `TypeName.toString()` is escaped with backquotes if the package name contains keywords. So NPE will be thrown if an annotation is placed in `annotation` package because `elements.getTypeElement(it.typeName.toString())` returns `null`. * Reformat imports * Use rawType().canonicalName instead of toString() * Fix test case * Require getTypeElement * Exclude com.google.guava from shadowJar * Move a test case of processing a qualifier placed in `annotation` package * Use checkNull instead
This commit is contained in:
@@ -44,7 +44,7 @@ object Dependencies {
|
||||
}
|
||||
|
||||
object KotlinPoet {
|
||||
private const val version = "1.6.0"
|
||||
private const val version = "1.7.2"
|
||||
const val kotlinPoet = "com.squareup:kotlinpoet:$version"
|
||||
const val metadata = "com.squareup:kotlinpoet-metadata-specs:$version"
|
||||
const val metadataSpecs = "com.squareup:kotlinpoet-metadata-specs:$version"
|
||||
|
@@ -60,9 +60,11 @@ dependencies {
|
||||
exclude(group = "org.jetbrains.kotlin")
|
||||
exclude(group = "com.squareup", module = "kotlinpoet")
|
||||
}
|
||||
api(Dependencies.KotlinPoet.elementsClassInspector)
|
||||
shade(Dependencies.KotlinPoet.elementsClassInspector) {
|
||||
exclude(group = "org.jetbrains.kotlin")
|
||||
exclude(group = "com.squareup", module = "kotlinpoet")
|
||||
exclude(group = "com.google.guava")
|
||||
}
|
||||
api(Dependencies.asm)
|
||||
|
||||
|
@@ -488,7 +488,10 @@ internal fun TargetProperty.generator(
|
||||
private fun List<AnnotationSpec>?.qualifiers(elements: Elements): Set<AnnotationSpec> {
|
||||
if (this == null) return setOf()
|
||||
return filterTo(mutableSetOf()) {
|
||||
elements.getTypeElement(it.typeName.toString()).getAnnotation(JSON_QUALIFIER) != null
|
||||
val typeElement = checkNotNull(elements.getTypeElement(it.typeName.rawType().canonicalName)) {
|
||||
"Could not get the type element of $it"
|
||||
}
|
||||
typeElement.getAnnotation(JSON_QUALIFIER) != null
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -28,6 +28,8 @@ import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.ToJson
|
||||
import com.squareup.moshi.adapter
|
||||
import com.squareup.moshi.internal.NullSafeJsonAdapter
|
||||
import com.squareup.moshi.kotlin.codegen.annotation.UppercaseInAnnotationPackage
|
||||
import com.squareup.moshi.kotlin.codegen.annotation.UppercaseInAnnotationPackageJsonAdapter
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.fail
|
||||
@@ -510,6 +512,22 @@ class GeneratedAdaptersTest {
|
||||
@JsonClass(generateAdapter = true)
|
||||
class ConstructorParameterWithQualifier(@Uppercase(inFrench = true) var a: String, var b: String)
|
||||
|
||||
@Test fun constructorParameterWithQualifierInAnnotationPackage() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(UppercaseInAnnotationPackageJsonAdapter())
|
||||
.build()
|
||||
val jsonAdapter = moshi.adapter<ConstructorParameterWithQualifierInAnnotationPackage>()
|
||||
|
||||
val encoded = ConstructorParameterWithQualifierInAnnotationPackage("Android")
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"a":"ANDROID"}""")
|
||||
|
||||
val decoded = jsonAdapter.fromJson("""{"a":"Android"}""")!!
|
||||
assertThat(decoded.a).isEqualTo("android")
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class ConstructorParameterWithQualifierInAnnotationPackage(@UppercaseInAnnotationPackage var a: String)
|
||||
|
||||
@Test fun propertyWithQualifier() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(UppercaseJsonAdapter())
|
||||
|
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Square, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.squareup.moshi.kotlin.codegen.annotation
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.JsonQualifier
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.util.Locale
|
||||
|
||||
@JsonQualifier
|
||||
annotation class UppercaseInAnnotationPackage
|
||||
|
||||
class UppercaseInAnnotationPackageJsonAdapter {
|
||||
@ToJson fun toJson(@UppercaseInAnnotationPackage s: String): String {
|
||||
return s.toUpperCase(Locale.US)
|
||||
}
|
||||
@FromJson @UppercaseInAnnotationPackage fun fromJson(s: String): String {
|
||||
return s.toLowerCase(Locale.US)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user