Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong path reported by json parser for unknown key #2869

Open
larshagencognite opened this issue Nov 27, 2024 · 1 comment
Open

Wrong path reported by json parser for unknown key #2869

larshagencognite opened this issue Nov 27, 2024 · 1 comment
Assignees
Labels

Comments

@larshagencognite
Copy link

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
@sandwwraith sandwwraith self-assigned this Nov 27, 2024
@sandwwraith
Copy link
Member

Thanks for the report, I'll take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants