Kotlinx Serialization MissingFieldException

I figured it out, apparently even though you mark something as nullable its still considered required.

For it to truly be optional you need to give it a default value so for example the data class would look like this with the nullables

@Serializable
data class ResponseData(
    val id: Long
    val attachments: Attachments? = null,
    val author_id: String? = null,
    val text: String
}

once you set the value the fields becomes optional and wont throw that exception

Leave a Comment