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:
Zac Sweers
2019-11-02 15:15:29 -04:00
committed by GitHub
parent c8c7121e76
commit fe34a577e7
2 changed files with 49 additions and 1 deletions

View File

@@ -435,6 +435,52 @@ class DualKotlinTest(useReflection: Boolean) {
val wildcardOut: GenericClass<out TypeAlias>,
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