mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Check against non-nullable known primitives when rendering class… (#993)
* Check against non-nullable known primitives when rendering class blocks `Int?` will not equal `Int` in KotlinPoet, so this was always falling through to the default `::class.java` code. Resolves #991 * Nix comment * Flesh out test to include all primitives
This commit is contained in:
@@ -71,7 +71,9 @@ internal fun TypeName.asTypeBlock(): CodeBlock {
|
|||||||
return bound.asTypeBlock()
|
return bound.asTypeBlock()
|
||||||
}
|
}
|
||||||
is ClassName -> {
|
is ClassName -> {
|
||||||
return when (this) {
|
// Check against the non-nullable version for equality, but we'll keep the nullability in
|
||||||
|
// consideration when creating the CodeBlock if needed.
|
||||||
|
return when (copy(nullable = false)) {
|
||||||
BOOLEAN, CHAR, BYTE, SHORT, INT, FLOAT, LONG, DOUBLE -> {
|
BOOLEAN, CHAR, BYTE, SHORT, INT, FLOAT, LONG, DOUBLE -> {
|
||||||
if (isNullable) {
|
if (isNullable) {
|
||||||
// Remove nullable but keep the java object type
|
// Remove nullable but keep the java object type
|
||||||
|
@@ -435,6 +435,52 @@ class DualKotlinTest(useReflection: Boolean) {
|
|||||||
val wildcardOut: GenericClass<out TypeAlias>,
|
val wildcardOut: GenericClass<out TypeAlias>,
|
||||||
val complex: GenericClass<GenericTypeAlias>
|
val complex: GenericClass<GenericTypeAlias>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Regression test for https://github.com/square/moshi/issues/991
|
||||||
|
@Test fun nullablePrimitiveProperties() {
|
||||||
|
val adapter = moshi.adapter<NullablePrimitives>()
|
||||||
|
|
||||||
|
@Language("JSON")
|
||||||
|
val testJson = """{"objectType":"value","boolean":true,"byte":3,"char":"a","short":3,"int":3,"long":3,"float":3.2,"double":3.2}"""
|
||||||
|
|
||||||
|
val instance = NullablePrimitives(
|
||||||
|
objectType = "value",
|
||||||
|
boolean = true,
|
||||||
|
byte = 3,
|
||||||
|
char = 'a',
|
||||||
|
short = 3,
|
||||||
|
int = 3,
|
||||||
|
long = 3,
|
||||||
|
float = 3.2f,
|
||||||
|
double = 3.2
|
||||||
|
)
|
||||||
|
assertThat(adapter.toJson(instance))
|
||||||
|
.isEqualTo(testJson)
|
||||||
|
|
||||||
|
val result = adapter.fromJson(testJson)!!
|
||||||
|
assertThat(result).isEqualTo(instance)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class NullablePrimitives(
|
||||||
|
val objectType: String = "",
|
||||||
|
val boolean: Boolean,
|
||||||
|
val nullableBoolean: Boolean? = null,
|
||||||
|
val byte: Byte,
|
||||||
|
val nullableByte: Byte? = null,
|
||||||
|
val char: Char,
|
||||||
|
val nullableChar: Char? = null,
|
||||||
|
val short: Short,
|
||||||
|
val nullableShort: Short? = null,
|
||||||
|
val int: Int,
|
||||||
|
val nullableInt: Int? = null,
|
||||||
|
val long: Long,
|
||||||
|
val nullableLong: Long? = null,
|
||||||
|
val float: Float,
|
||||||
|
val nullableFloat: Float? = null,
|
||||||
|
val double: Double,
|
||||||
|
val nullableDouble: Double? = null
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
typealias TypeAlias = Int
|
typealias TypeAlias = Int
|
||||||
|
Reference in New Issue
Block a user