10
votes

I am in the process of converting from Moshi to kotlinx serialization with Ktor and when I try to make a request to get data I am getting this error

kotlinx.serialization.MissingFieldException: Field 'attachments' is required, but it was missing

which makes sense since this specific response does not contain this field

Response Json

{
    "data": {
        "id": "1299418846990921728",
        "text": "This is a test"
    }
}

BUT my Serialized class has the attachments field as nullable (it is in the response only when it needs to be) so it should just ignore it I thought like it did with Moshi

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

In my Ktor client setup I have it set to ignore unknown keys

private val _client: HttpClient = HttpClient(engine) {
    install(JsonFeature) {
        val json = Json {
            this.isLenient = true
            this.ignoreUnknownKeys = true
        }
        serializer = KotlinxSerializer(json)
    }
}

Why is it still saying that the field is required even though its nullable?

1

1 Answers

22
votes

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