diff --git a/kotlin-codegen/compiler/src/main/java/com/squareup/moshi/DelegateKey.kt b/kotlin-codegen/compiler/src/main/java/com/squareup/moshi/DelegateKey.kt index 2302471..b9261bd 100644 --- a/kotlin-codegen/compiler/src/main/java/com/squareup/moshi/DelegateKey.kt +++ b/kotlin-codegen/compiler/src/main/java/com/squareup/moshi/DelegateKey.kt @@ -40,7 +40,7 @@ internal data class DelegateKey( private val type: TypeName, private val jsonQualifiers: Set ) { - val nullable get() = type.nullable || type is TypeVariableName + val nullable get() = type.nullable /** Returns an adapter to use when encoding and decoding this property. */ fun generateProperty( diff --git a/kotlin-codegen/compiler/src/main/java/com/squareup/moshi/metadata.kt b/kotlin-codegen/compiler/src/main/java/com/squareup/moshi/metadata.kt index 03c2b1e..1109cfb 100644 --- a/kotlin-codegen/compiler/src/main/java/com/squareup/moshi/metadata.kt +++ b/kotlin-codegen/compiler/src/main/java/com/squareup/moshi/metadata.kt @@ -80,6 +80,7 @@ internal fun Type.asTypeName( val realType = when { hasTypeParameter() -> return getTypeParameter(typeParameter) .asTypeName(nameResolver, getTypeParameter, resolveAliases) + .let { if (nullable) it.asNullable() else it } hasTypeParameterName() -> typeParameterName hasAbbreviatedType() && !resolveAliases -> abbreviatedType.typeAliasName else -> className diff --git a/kotlin-codegen/integration-test/src/test/kotlin/com/squareup/moshi/GeneratedAdaptersTest.kt b/kotlin-codegen/integration-test/src/test/kotlin/com/squareup/moshi/GeneratedAdaptersTest.kt index 6cc69da..fd65397 100644 --- a/kotlin-codegen/integration-test/src/test/kotlin/com/squareup/moshi/GeneratedAdaptersTest.kt +++ b/kotlin-codegen/integration-test/src/test/kotlin/com/squareup/moshi/GeneratedAdaptersTest.kt @@ -222,14 +222,16 @@ class GeneratedAdaptersTest { listOf("foo", null, "bar"), setOf("foo", null, "bar"), mapOf("foo" to "bar", "baz" to null), - null + null, + 1 ) val noNullsTypeParams = NullableTypeParams( nullableTypeParams.nullableList, nullableTypeParams.nullableSet, nullableTypeParams.nullableMap.filterValues { it != null }, - null + null, + 1 ) val json = adapter.toJson(nullableTypeParams) @@ -781,7 +783,8 @@ data class NullableTypeParams( val nullableList: List, val nullableSet: Set, val nullableMap: Map, - val nullableT: T? + val nullableT: T?, + val nonNullT: T ) typealias TypeAliasName = String