mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Fix nullable properties of TypeVariable types
We were forgetting to apply the property's nullability to the resolved type. Fixes #521
This commit is contained in:
@@ -33,7 +33,7 @@ internal data class DelegateKey(
|
|||||||
private val type: TypeName,
|
private val type: TypeName,
|
||||||
private val jsonQualifiers: Set<AnnotationMirror>
|
private val jsonQualifiers: Set<AnnotationMirror>
|
||||||
) {
|
) {
|
||||||
val nullable get() = type.nullable || type is TypeVariableName
|
val nullable get() = type.nullable
|
||||||
|
|
||||||
/** Returns an adapter to use when encoding and decoding this property. */
|
/** Returns an adapter to use when encoding and decoding this property. */
|
||||||
fun generateProperty(
|
fun generateProperty(
|
||||||
|
@@ -80,6 +80,7 @@ internal fun Type.asTypeName(
|
|||||||
val realType = when {
|
val realType = when {
|
||||||
hasTypeParameter() -> return getTypeParameter(typeParameter)
|
hasTypeParameter() -> return getTypeParameter(typeParameter)
|
||||||
.asTypeName(nameResolver, getTypeParameter, resolveAliases)
|
.asTypeName(nameResolver, getTypeParameter, resolveAliases)
|
||||||
|
.let { if (nullable) it.asNullable() else it }
|
||||||
hasTypeParameterName() -> typeParameterName
|
hasTypeParameterName() -> typeParameterName
|
||||||
hasAbbreviatedType() && !resolveAliases -> abbreviatedType.typeAliasName
|
hasAbbreviatedType() && !resolveAliases -> abbreviatedType.typeAliasName
|
||||||
else -> className
|
else -> className
|
||||||
|
@@ -222,14 +222,16 @@ class GeneratedAdaptersTest {
|
|||||||
listOf("foo", null, "bar"),
|
listOf("foo", null, "bar"),
|
||||||
setOf("foo", null, "bar"),
|
setOf("foo", null, "bar"),
|
||||||
mapOf("foo" to "bar", "baz" to null),
|
mapOf("foo" to "bar", "baz" to null),
|
||||||
null
|
null,
|
||||||
|
1
|
||||||
)
|
)
|
||||||
|
|
||||||
val noNullsTypeParams = NullableTypeParams<Int>(
|
val noNullsTypeParams = NullableTypeParams<Int>(
|
||||||
nullableTypeParams.nullableList,
|
nullableTypeParams.nullableList,
|
||||||
nullableTypeParams.nullableSet,
|
nullableTypeParams.nullableSet,
|
||||||
nullableTypeParams.nullableMap.filterValues { it != null },
|
nullableTypeParams.nullableMap.filterValues { it != null },
|
||||||
null
|
null,
|
||||||
|
1
|
||||||
)
|
)
|
||||||
|
|
||||||
val json = adapter.toJson(nullableTypeParams)
|
val json = adapter.toJson(nullableTypeParams)
|
||||||
@@ -782,7 +784,8 @@ data class NullableTypeParams<T>(
|
|||||||
val nullableList: List<String?>,
|
val nullableList: List<String?>,
|
||||||
val nullableSet: Set<String?>,
|
val nullableSet: Set<String?>,
|
||||||
val nullableMap: Map<String, String?>,
|
val nullableMap: Map<String, String?>,
|
||||||
val nullableT: T?
|
val nullableT: T?,
|
||||||
|
val nonNullT: T
|
||||||
)
|
)
|
||||||
|
|
||||||
typealias TypeAliasName = String
|
typealias TypeAliasName = String
|
||||||
|
Reference in New Issue
Block a user