Skip to content
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

Closed
smoskalenko opened this issue Aug 20, 2015 · 2 comments
Closed

Comments

@smoskalenko
Copy link
Contributor

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:

$inputArray = [
  'arrayField' => ['foo', 'bar', 'baz']
];

$jsonPatch = [
  ['op' => 'add', 'path' => '/arrayField/7', 'value' => 'new']
];

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

@raphaelstolt
Copy link
Owner

Hi @smoskalenko, how about performing a test operation prior to using one of the patch (i.e. add) methods?

@smoskalenko
Copy link
Contributor Author

Hi @raphaelstolt

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).

object(stdClass)#239 (3) {
  ["op"]=>
  string(4) "test"
  ["path"]=>
  string(13) "/arrayField/0"
  ["value"]=>
  object(stdClass)#238 (1) {
    ["name"]=>
    string(3) "foo"
  }
}

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

smoskalenko added a commit to smoskalenko/php-jsonpatch that referenced this issue Aug 21, 2015
smoskalenko added a commit to smoskalenko/php-jsonpatch that referenced this issue Aug 21, 2015
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

No branches or pull requests

2 participants