Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,27 @@ class JsonPathTest : JsonTestBase() {
expectPath(expectedPath) { json.decodeFromString(Sealed.serializer(), malformed) }
}

@Serializable
data class SimpleNested(val n: SimpleNested? = null, val t: DataObject? = null)

@Serializable
data object DataObject

@Test
fun testMalformedDataObjectInDeeplyNestedStructure() {
var outer = SimpleNested(t = DataObject)
repeat(20) {
outer = SimpleNested(n = outer)
}
val str = Json.encodeToString(SimpleNested.serializer(), outer)
// throw-away data
Json.decodeFromString(SimpleNested.serializer(), str)

val malformed = str.replace("{}", "42")
val expectedPath = "$" + ".n".repeat(20) + ".t\n"
expectPath(expectedPath) { Json.decodeFromString(SimpleNested.serializer(), malformed) }
}

private inline fun expectPath(path: String, block: () -> Unit) {
val message = runCatching { block() }
.exceptionOrNull()!!.message!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ internal class JsonPath {
private fun resize() {
val newSize = currentDepth * 2
currentObjectPath = currentObjectPath.copyOf(newSize)
indicies = indicies.copyOf(newSize)
val newIndices = IntArray(newSize) { -1 }
indicies.copyInto(newIndices)
indicies = newIndices
}

override fun toString(): String = getPath()
Expand Down