Colton Idle
05/27/2024, 8:19 PM@Serializable
data class MyResponseBody(
val id: Long,
val email: String,
val username: String?
)
I got an error of Field 'username' is required for type with serial name
Why is that. Doesn't the ?
indicate that it's not required? If not, then how can I catch this at compile time? Seems like marking something with @Serializable should enforce that any ?
field should have a default, no?jw
05/27/2024, 8:23 PMjw
05/27/2024, 8:24 PMjw
05/27/2024, 8:25 PMjw
05/27/2024, 8:26 PM{"presentNotNull":"present","presentNull":null}
and the three properties that would bind to it: presentNotNull, presentNull, and absentjw
05/27/2024, 8:27 PMjw
05/27/2024, 8:27 PMjw
05/27/2024, 8:28 PMsindrenm
05/27/2024, 8:39 PM@Serializable
data class MyResponseBody(
val id: Long,
val email: String,
val username: String? = null,
)
Alternatively, at least if you're using kotlinx.serialization.json, you can configure Json
to not require explicit nulls by setting explicitNulls = false
.
https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-builder/explicit-nulls.htmlColton Idle
05/27/2024, 11:11 PM