json_pointer.contains() exceptions when path not found #1942
Labels
kind: question
release item: 🐛 bug fix
solution: proposed fix
a fix for the issue has been proposed and waits for confirmation
Milestone
When using contains with a
json_pointer
, the expected functionality is that thecontains()
function will returnfalse
if the path is not found in thejson
object.For a
json
object with "/path/[array]" this functionality operates differently, depending on the path parameter in thejson_pointer
.In the case of "/path/-" the operation completes as expected, as the "-" index is beyond the end of the array (fails range check), and does not raise any error or exception (returns
false
).In the case of "/path/[number]" the operation completes as expected, (returns
true
) if the array index is within the size() range, and (returnsfalse
) if the index is beyond the end of any array, and does not raise any error or exception.If the object being checked in the case of "/path/[number]" does not have an array, it does not raise any error or exception (returns
false
).However, in the case of "/path/string" the operation always raises an exception, as the current functionality assumes an array index, and raises an exception if it is not.
I believe that the function is presently operating counter to the intent of the
contains()
functionality, which is intended to verify if a given path is valid, not if the existing object can support that path.An object should not determine how the json_pointer path is verified.
A
json_pointer
should be verified if it can be accessed within an object.i.e. is
object[json_pointer]
valid to use? Not that the path is invalid for the object being tested.At present the object has required that the path be an index within the array (object defined), and not that the path verifies that the
json_pointer
may be used to access the object.This is present in v3.7.3.
In my option the "/path/string" access into an object with an array, should not raise an exception due to the path being checked not being a numeric index into that array.
Proposed Patch:
In
bool json_pointer::contains(const BasicJsonType* ptr) const
The line:
if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
should be changed to:
if (JSON_HEDLEY_UNLIKELY((reference_token < "0") || (reference_token > "9")))
to reject any string as being an invalid index into the array.
This would therefore always return
false
for any non-numeric index, as any attempt to use a string to index into an array should only confirm that the json_pointer may not be used.i.e. the json_pointer is not contained in the object.
The text was updated successfully, but these errors were encountered: