-
Notifications
You must be signed in to change notification settings - Fork 24
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
Throw an exception when trying to add element to nonexistent index? #8
Comments
Hi @smoskalenko, how about performing a |
Do you mean that before main patch request we should run test operation and check that array field contains specified index? Also I've played a little bit with test operation and found the following issue: $inputData = json_encode(
[
'arrayField' => [
['name' => 'foo'],
['name' => 'bar']
]
]
);
$jsonPatch = json_encode(
[
['op' => 'test', 'path' => '/arrayField/0', 'value' => ['name' => 'foo']]
]
);
// Apply JSON patches
$patch = new Patch($inputData, $jsonPatch);
$patchedDocument = $patch->apply(); I get the FailedTestException("Failed on Test PatchOperation at index: 0") It seems that new integration test is needed. The basic issue is that in unit tests you instantiate the operation objects with the following structure (value is json encoded string, why?): $operation = new \stdClass;
$operation->path = '/chars/array';
$operation->value = '["abc", "def"]';
$testOperation = new Test($operation); But in fact when we call patch() operations objects instantiate with stdObject where value also is stdObject in case of array (because of json_decode() convert assoc. array to std object, when calling without $assoc flag).
P.S. In our application in most cases we have a big JSON documents, and patch request will consist of a several operations, so it's very comfortably just send PATCH request to REST API for-example and get the result - 200 OK , or 400 Bad Request |
Hi @raphaelstolt
Thanks for your library, I think it's best library for patching JSONs on github ;) But we want to have another feature in our project - we want to catch the cases when someone trying to add element to array to nonexistent index, as example case with too large index in patch operation:
What are you thinking about throwing exception in such cases in Rs/Json/Patch/Operations/Add::perform() method, so we can catch this and process as we want. Can I create pull request for this feature?
Best regards,
Slava
The text was updated successfully, but these errors were encountered: