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?