IndexOutOfBoundsException thrown instead of JsonPatchException
Closed this issue · 1 comments
choffmeister commented
Hi,
first of all thanks for the great library. I just stumbled upon an edge case bug, that causes a wrong exception to be throws:
val data = JsObject(
"array" -> JsArray(
JsString("bar1"),
JsString("bar2"),
JsObject(
"sub1" -> JsString("bar3")
)
)
)
// works (as index is present)
val diff1 = JsonPatch(Replace(new JsonPointer().parse("/array/2/sub1"), JsString("bar4")))
println(Try(diff1(data)))
// fails with index out of bound exception (first index that is not present)
val diff2 = JsonPatch(Replace(new JsonPointer().parse("/array/3/sub1"), JsString("bar4")))
println(Try(diff2(data)))
// fails correctly with JsonPatchException (higher index that is not present)
val diff3 = JsonPatch(Replace(new JsonPointer().parse("/array/4/sub1"), JsString("bar4")))
println(Try(diff3(data)))
gives the following output:
Success({"array":["bar1","bar2",{"sub1":"bar4"}]})
Failure(java.lang.IndexOutOfBoundsException: 3)
Failure(gnieh.diffson.PatchException: element 4 does not exist at path /array)
Guess it is a simple one-off bug, but did not dig into the source code.
choffmeister commented
Seems to be the following lines (JsonPatch.scala:144-145
):
if (idx > elems.size)
throw new PatchException(f"element $idx does not exist at path $parent")
Should rather be idx >= elems.size
.