Skip to content

Unexpected token for data object in a deep json structure gives ArrayIndexOutOfBoundsException #2994

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

Open
larshagencognite opened this issue Apr 22, 2025 · 0 comments · May be fixed by #2995

Comments

@larshagencognite
Copy link

Describe the bug

When the json parser encounters an unexpected token while parsing a data object deep in a json structure (>= 8 levels), it throws ArrayIndexOutOfBoundsException.

This seems to be related the size of the initial size of the indices array in JsonPath. At the start, all values are initialized to -1. When the array is later increased in size for a deep structure, the resize operation uses copyOf(newsize), which fills in the array with zeroes.

To Reproduce

package com.foo.bar

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 TestDeepJsonFailure {
    @Test
    fun `test deep failure`() {
        val exception = assertThrows<SerializationException> {
            Json.decodeFromString<Foo>("""{"bar": {"bar": {"bar": {"bar": {"bar": {"bar": {"baz": false}}}}}}}""")
        }

        assertEquals(
            """
                Unexpected JSON token at offset 56: Expected start of the object '{', but had 'f' instead at path: ${'$'}.bar.bar.bar.bar.bar.bar.baz
                JSON input: {"bar": {"bar": {"bar": {"bar": {"bar": {"bar": {"baz": false}}}}}}}
            """.trimIndent(),
            exception.message
        )
    }

    @Test
    fun `test deeper failure`() {
        val exception = assertThrows<ArrayIndexOutOfBoundsException> {
            Json.decodeFromString<Foo>("""{"bar": {"bar": {"bar": {"bar": {"bar": {"bar": {"bar": {"baz": false}}}}}}}}""")
        }

        assertEquals(
            "Index 0 out of bounds for length 0",
            exception.message
        )
    }
}

@Serializable
data class Foo(val bar: Foo?, val baz: Baz?)

@Serializable
data object Baz

Expected behavior

The parser should fail with a similar error message as for less deep json structure.

Environment

  • Kotlin version: 2.1.10
  • Library version: 1.8.0
  • Kotlin platforms: JVM
  • Gradle version: N/A
  • IDE version N/A
@larshagencognite larshagencognite linked a pull request Apr 22, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant