You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Json configuration option coerceInputValues = true is very helpful to future-proof network code, but it has some unfortunate side effects. Some of them are already raised in different issues, but this one is not (AFAIK):
When model has enum field with no default, and unexpected enum value is encountered, confusing MissingFieldException is thrown instead of correct exception with helpful error message. This caught me off-guard a couple times, causing time wasted looking for typos in field name, etc.
To Reproduce
@Test
fun`GIVEN json with unexpected enum value WHEN deserialize THEN throw meaningful exception`() {
val jsonWithUnexpectedValue =""" { "color": "SOMETHING_UNEXPECTED" }""".trimIndent()
val jsonSerializer =Json {
coerceInputValues =true
}
val throwable = runCatching {
jsonSerializer.decodeFromString<Model>(jsonWithUnexpectedValue)
}.exceptionOrNull()
assertThat(throwable, instanceOf(SerializationException::class.java))
assertThat(throwable!!.message, containsString("SOMETHING_UNEXPECTED"))
}
@Serializable
data classModel(valcolor:Color)
enumclassColor { RED, GREEN }
Expected behavior kotlinx.serialization.SerializationException: gmk57.Color does not contain element with name 'SOMETHING_UNEXPECTED' at path $.color
That's what is thrown with coerceInputValues = false
Observed behavior kotlinx.serialization.MissingFieldException: Field 'color' is required for type with serial name 'gmk57.Model', but it was missing at path: $
Environment
Kotlin version: 1.9.10
Library version: 1.6.2
I guess the root cause is the same as in #1947, but the issue we face is a bit different.
The text was updated successfully, but these errors were encountered:
Describe the bug
Json configuration option
coerceInputValues = true
is very helpful to future-proof network code, but it has some unfortunate side effects. Some of them are already raised in different issues, but this one is not (AFAIK):When model has enum field with no default, and unexpected enum value is encountered, confusing
MissingFieldException
is thrown instead of correct exception with helpful error message. This caught me off-guard a couple times, causing time wasted looking for typos in field name, etc.To Reproduce
Expected behavior
kotlinx.serialization.SerializationException: gmk57.Color does not contain element with name 'SOMETHING_UNEXPECTED' at path $.color
That's what is thrown with
coerceInputValues = false
Observed behavior
kotlinx.serialization.MissingFieldException: Field 'color' is required for type with serial name 'gmk57.Model', but it was missing at path: $
Environment
I guess the root cause is the same as in #1947, but the issue we face is a bit different.
The text was updated successfully, but these errors were encountered: