mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Strip type annotations from TypeRenderer and asTypeBlock() (#1039)
Resolves #1033
This commit is contained in:
@@ -41,6 +41,9 @@ abstract class TypeRenderer {
|
|||||||
abstract fun renderTypeVariable(typeVariable: TypeVariableName): CodeBlock
|
abstract fun renderTypeVariable(typeVariable: TypeVariableName): CodeBlock
|
||||||
|
|
||||||
fun render(typeName: TypeName, forceBox: Boolean = false): CodeBlock {
|
fun render(typeName: TypeName, forceBox: Boolean = false): CodeBlock {
|
||||||
|
if (typeName.annotations.isNotEmpty()) {
|
||||||
|
return render(typeName.copy(annotations = emptyList()), forceBox)
|
||||||
|
}
|
||||||
if (typeName.isNullable) {
|
if (typeName.isNullable) {
|
||||||
return renderObjectType(typeName.copy(nullable = false))
|
return renderObjectType(typeName.copy(nullable = false))
|
||||||
}
|
}
|
||||||
|
@@ -62,6 +62,9 @@ internal fun TypeName.defaultPrimitiveValue(): CodeBlock =
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal fun TypeName.asTypeBlock(): CodeBlock {
|
internal fun TypeName.asTypeBlock(): CodeBlock {
|
||||||
|
if (annotations.isNotEmpty()) {
|
||||||
|
return copy(annotations = emptyList()).asTypeBlock()
|
||||||
|
}
|
||||||
when (this) {
|
when (this) {
|
||||||
is ParameterizedTypeName -> {
|
is ParameterizedTypeName -> {
|
||||||
return if (rawType == ARRAY) {
|
return if (rawType == ARRAY) {
|
||||||
@@ -143,4 +146,4 @@ internal fun TypeName.stripTypeVarVariance(): TypeName {
|
|||||||
return mapTypes<TypeVariableName> {
|
return mapTypes<TypeVariableName> {
|
||||||
TypeVariableName(name = name, bounds = bounds.map { it.mapTypes(TypeVariableName::stripTypeVarVariance) }, variance = null)
|
TypeVariableName(name = name, bounds = bounds.map { it.mapTypes(TypeVariableName::stripTypeVarVariance) }, variance = null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -35,6 +35,7 @@ import org.junit.Assert.fail
|
|||||||
import org.junit.Ignore
|
import org.junit.Ignore
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
import kotlin.annotation.AnnotationTarget.TYPE
|
||||||
import kotlin.properties.Delegates
|
import kotlin.properties.Delegates
|
||||||
import kotlin.reflect.full.memberProperties
|
import kotlin.reflect.full.memberProperties
|
||||||
|
|
||||||
@@ -1185,6 +1186,20 @@ class GeneratedAdaptersTest {
|
|||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
data class DeprecatedProperty(@Deprecated("Deprecated for reasons") val foo: String)
|
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
|
// Regression test for https://github.com/square/moshi/issues/1022
|
||||||
|
Reference in New Issue
Block a user