diff --git a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/TypeRenderer.kt b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/TypeRenderer.kt index 5770df4..f460286 100644 --- a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/TypeRenderer.kt +++ b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/TypeRenderer.kt @@ -41,6 +41,9 @@ abstract class TypeRenderer { abstract fun renderTypeVariable(typeVariable: TypeVariableName): CodeBlock fun render(typeName: TypeName, forceBox: Boolean = false): CodeBlock { + if (typeName.annotations.isNotEmpty()) { + return render(typeName.copy(annotations = emptyList()), forceBox) + } if (typeName.isNullable) { return renderObjectType(typeName.copy(nullable = false)) } diff --git a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt index 1d61aa8..1b678d2 100644 --- a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt +++ b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt @@ -62,6 +62,9 @@ internal fun TypeName.defaultPrimitiveValue(): CodeBlock = } internal fun TypeName.asTypeBlock(): CodeBlock { + if (annotations.isNotEmpty()) { + return copy(annotations = emptyList()).asTypeBlock() + } when (this) { is ParameterizedTypeName -> { return if (rawType == ARRAY) { @@ -143,4 +146,4 @@ internal fun TypeName.stripTypeVarVariance(): TypeName { return mapTypes { TypeVariableName(name = name, bounds = bounds.map { it.mapTypes(TypeVariableName::stripTypeVarVariance) }, variance = null) } -} \ No newline at end of file +} diff --git a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codegen/GeneratedAdaptersTest.kt b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codegen/GeneratedAdaptersTest.kt index a7c1e60..d4ff515 100644 --- a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codegen/GeneratedAdaptersTest.kt +++ b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codegen/GeneratedAdaptersTest.kt @@ -35,6 +35,7 @@ import org.junit.Assert.fail import org.junit.Ignore import org.junit.Test import java.util.Locale +import kotlin.annotation.AnnotationTarget.TYPE import kotlin.properties.Delegates import kotlin.reflect.full.memberProperties @@ -1185,6 +1186,20 @@ class GeneratedAdaptersTest { @JsonClass(generateAdapter = true) data class DeprecatedProperty(@Deprecated("Deprecated for reasons") val foo: String) + + + @Target(TYPE) + annotation class TypeAnnotation + + /** + * Compilation-only test to ensure we don't render types with their annotations. + * Regression test for https://github.com/square/moshi/issues/1033 + */ + @JsonClass(generateAdapter = true) + data class TypeAnnotationClass( + val propertyWithAnnotatedType: @TypeAnnotation String = "", + val generic: List<@TypeAnnotation String> + ) } // Regression test for https://github.com/square/moshi/issues/1022