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
When a property is set to null in the json and the model property is nullable, parsing the property to null should always work.
No coercing or leniency is needed since it directly maps accurately to the model.
However, setting isLenient OR coerceInputValues to true causes an unexpected MissingFieldException even though the field is clearly and accurately defined in the json.
To Reproduce
This test will unexpectedly fail with a thrown MissingFieldException:
importkotlinx.serialization.Serializableimportkotlinx.serialization.decodeFromStringimportkotlinx.serialization.json.Jsonimportorg.junit.TestclassSerializationTest {
@Test
fun`model with nullable enum and explicit json null value does not throw MissingFieldException`() {
// Arrange: Json with a valid null valueval jsonWithExplicitNullValue =""" { "color": null }""".trimIndent()
// Arrange: Serializer that is as forgiving as possibleval jsonSerializerLenient =Json {
isLenient =true// NOTE: Test will pass if this is false
coerceInputValues =true// NOTE: OR test will pass if `isLenient` is true and this is false
ignoreUnknownKeys =true
prettyPrint =true
allowStructuredMapKeys =true
}
// Act: Convert json to modelval model = jsonSerializerLenient.decodeFromString<Model>(jsonWithExplicitNullValue)
// Assert: Model converted and value is null like in the jsonassert(model.color ==null)
}
@Serializable
data classModel(
valcolor:Color?
)
enumclassColor {
Red,
Blue,
Green
}
}
Describe the bug
When a property is set to
null
in the json and the model property is nullable, parsing the property to null should always work.No coercing or leniency is needed since it directly maps accurately to the model.
However, setting
isLenient
ORcoerceInputValues
totrue
causes an unexpectedMissingFieldException
even though the field is clearly and accurately defined in the json.To Reproduce
This test will unexpectedly fail with a thrown
MissingFieldException
:Expected behavior
Environment
The text was updated successfully, but these errors were encountered: