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 parsing json with unknown key, the path in the error message will only be correct if the unknown key is the first key in the object. If it is not the first key, the reported path will include the previous key at the same level as the unknown key.
To Reproduce
package ...
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
class UnknownKeyParsingTest {
@Serializable
data class MyClass(val field: String)
@Test
fun testUnknownKey() {
assertThrows<SerializationException> {
Json.decodeFromString<MyClass>("""{ "unknownField": 123 }""")
}.also {
assertEquals(
"""
Unexpected JSON token at offset 3: Encountered an unknown key 'unknownField' at path: $
Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys.
JSON input: { "unknownField": 123 }
""".trimIndent(),
it.message
)
}
}
@Test
fun testUnknownKeyAsSecondKey() {
assertThrows<SerializationException> {
Json.decodeFromString<MyClass>("""{ "field": "value", "unknownField": 123 }""")
}.also {
assertEquals(
"""
Unexpected JSON token at offset 21: Encountered an unknown key 'unknownField' at path: $.field
Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys.
JSON input: { "field": "value", "unknownField": 123 }
""".trimIndent(),
it.message
)
}
}
}
Expected behavior
The expected behaviour is that the reported path is for the object that contains the unknown key, not for a sibling key.
Environment
Kotlin version: 2.0.21
Library version: 1.7.3
Kotlin platforms: JVM
The text was updated successfully, but these errors were encountered:
Describe the bug
When parsing json with unknown key, the path in the error message will only be correct if the unknown key is the first key in the object. If it is not the first key, the reported path will include the previous key at the same level as the unknown key.
To Reproduce
Expected behavior
The expected behaviour is that the reported path is for the object that contains the unknown key, not for a sibling key.
Environment
The text was updated successfully, but these errors were encountered: