From 9dbf2880cc07138753699636d00c4d043ebd0a2f Mon Sep 17 00:00:00 2001 From: Eric Hawicz Date: Wed, 26 Jul 2023 22:01:04 -0400 Subject: [PATCH] Adjust the behavior of the args passed to json_patch_apply to make it easier to do in place modifications, and add a struct json_patch_error to report more details on failures. --- json_patch.c | 162 ++++++++++++++++++++++++--------- json_patch.h | 54 +++++++++-- tests/test_json_patch.c | 17 +++- tests/test_json_patch.expected | 35 +++++++ 4 files changed, 210 insertions(+), 58 deletions(-) diff --git a/json_patch.c b/json_patch.c index b48eed8..dbc54ec 100644 --- a/json_patch.c +++ b/json_patch.c @@ -1,9 +1,9 @@ /* * Copyright (c) 2021 Alexandru Ardelean. + * Copyright (c) 2023 Eric Hawicz * * This is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See COPYING for details. - * */ #include "config.h" @@ -16,6 +16,33 @@ #include "json_object_private.h" #include "json_pointer_private.h" +#include +#ifndef SIZE_T_MAX +#if SIZEOF_SIZE_T == SIZEOF_INT +#define SIZE_T_MAX UINT_MAX +#elif SIZEOF_SIZE_T == SIZEOF_LONG +#define SIZE_T_MAX ULONG_MAX +#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG +#define SIZE_T_MAX ULLONG_MAX +#else +#error Unable to determine size of size_t +#endif +#endif + +#define _set_err(_errval, _errmsg) do { \ + patch_error->errno_code = (_errval); \ + patch_error->errmsg = (_errmsg); \ + errno = 0; /* To avoid confusion */ \ +} while (0) + +#define _set_err_from_ptrget(_errval, _fieldname) do { \ + patch_error->errno_code = (_errval); \ + patch_error->errmsg = (_errval) == ENOENT ? \ + "Did not find element referenced by " _fieldname " field" : \ + "Invalid " _fieldname " field"; \ + errno = 0; /* To avoid confusion */ \ +} while(0) + /** * JavaScript Object Notation (JSON) Patch * RFC 6902 - https://tools.ietf.org/html/rfc6902 @@ -23,23 +50,23 @@ static int json_patch_apply_test(struct json_object **res, struct json_object *patch_elem, - const char *path) + const char *path, struct json_patch_error *patch_error) { struct json_object *value1, *value2; if (!json_object_object_get_ex(patch_elem, "value", &value1)) { - errno = EINVAL; + _set_err(EINVAL, "Patch object does not contain a 'value' field"); return -1; } - /* errno should be set by json_pointer_get() */ if (json_pointer_get(*res, path, &value2)) + { + _set_err_from_ptrget(errno, "path"); return -1; + } if (!json_object_equal(value1, value2)) { - json_object_put(*res); - *res = NULL; - errno = ENOENT; + _set_err(ENOENT, "Value of element referenced by 'path' field did not match 'value' field"); return -1; } @@ -58,61 +85,79 @@ static int __json_patch_apply_remove(struct json_pointer_get_result *jpres) } } -static int json_patch_apply_remove(struct json_object **res, const char *path) +static int json_patch_apply_remove(struct json_object **res, const char *path, struct json_patch_error *patch_error) { struct json_pointer_get_result jpres; + int rc; if (json_pointer_get_internal(*res, path, &jpres)) + { + _set_err_from_ptrget(errno, "path"); return -1; + } - return __json_patch_apply_remove(&jpres); + rc = __json_patch_apply_remove(&jpres); + if (rc < 0) + _set_err(EINVAL, "Unable to remove path referenced by 'path' field"); + return rc; } +// callback for json_pointer_set_with_array_cb() static int json_object_array_insert_idx_cb(struct json_object *parent, size_t idx, struct json_object *value, void *priv) { + int rc; int *add = priv; if (idx > json_object_array_length(parent)) { + // Note: will propagate back out through json_pointer_set_with_array_cb() errno = EINVAL; return -1; } if (*add) - return json_object_array_insert_idx(parent, idx, value); + rc = json_object_array_insert_idx(parent, idx, value); else - return json_object_array_put_idx(parent, idx, value); + rc = json_object_array_put_idx(parent, idx, value); + if (rc < 0) + errno = EINVAL; + return rc; } static int json_patch_apply_add_replace(struct json_object **res, struct json_object *patch_elem, - const char *path, int add) + const char *path, int add, struct json_patch_error *patch_error) { struct json_object *value; int rc; if (!json_object_object_get_ex(patch_elem, "value", &value)) { - errno = EINVAL; + _set_err(EINVAL, "Patch object does not contain a 'value' field"); return -1; } /* if this is a replace op, then we need to make sure it exists before replacing */ if (!add && json_pointer_get(*res, path, NULL)) { - errno = ENOENT; + _set_err_from_ptrget(errno, "path"); return -1; } rc = json_pointer_set_with_array_cb(res, path, json_object_get(value), json_object_array_insert_idx_cb, &add); if (rc) + { + _set_err(errno, "Failed to set value at path referenced by 'path' field"); json_object_put(value); + } return rc; } +// callback for json_pointer_set_with_array_cb() static int json_object_array_move_cb(struct json_object *parent, size_t idx, struct json_object *value, void *priv) { + int rc; struct json_pointer_get_result *from = priv; size_t len = json_object_array_length(parent); @@ -127,16 +172,20 @@ static int json_object_array_move_cb(struct json_object *parent, size_t idx, if (idx > len) { + // Note: will propagate back out through json_pointer_set_with_array_cb() errno = EINVAL; return -1; } - return json_object_array_insert_idx(parent, idx, value); + rc = json_object_array_insert_idx(parent, idx, value); + if (rc < 0) + errno = EINVAL; + return rc; } static int json_patch_apply_move_copy(struct json_object **res, struct json_object *patch_elem, - const char *path, int move) + const char *path, int move, struct json_patch_error *patch_error) { json_pointer_array_set_cb array_set_cb; struct json_pointer_get_result from; @@ -146,7 +195,7 @@ static int json_patch_apply_move_copy(struct json_object **res, int rc; if (!json_object_object_get_ex(patch_elem, "from", &jfrom)) { - errno = EINVAL; + _set_err(EINVAL, "Patch does not contain a 'from' field"); return -1; } @@ -163,13 +212,16 @@ static int json_patch_apply_move_copy(struct json_object **res, */ if (from_s_len == strlen(path)) return 0; - errno = EINVAL; + _set_err(EINVAL, "Invalid attempt to move parent under a child"); return -1; } rc = json_pointer_get_internal(*res, from_s, &from); if (rc) + { + _set_err_from_ptrget(errno, "from"); return rc; + } json_object_get(from.obj); @@ -186,65 +238,87 @@ static int json_patch_apply_move_copy(struct json_object **res, rc = json_pointer_set_with_array_cb(res, path, from.obj, array_set_cb, &from); if (rc) + { + _set_err(errno, "Failed to set value at path referenced by 'path' field"); json_object_put(from.obj); + } return rc; } -int json_patch_apply(struct json_object *base, struct json_object *patch, - struct json_object **res) +int json_patch_apply(struct json_object *copy_from, struct json_object *patch, + struct json_object **base, struct json_patch_error *patch_error) { size_t ii; int rc = 0; + struct json_patch_error placeholder; - if (!base || !json_object_is_type(patch, json_type_array)) { - errno = EINVAL; + if (!patch_error) + patch_error = &placeholder; + + patch_error->patch_failure_idx = SIZE_T_MAX; + patch_error->errno_code = 0; + + if (base == NULL|| + (*base == NULL && copy_from == NULL) || + (*base != NULL && copy_from != NULL)) + { + _set_err(EFAULT, "Exactly one of *base or copy_from must be non-NULL"); return -1; } - - /* errno should be set inside json_object_deep_copy() */ - if (json_object_deep_copy(base, res, NULL) < 0) + + if (!json_object_is_type(patch, json_type_array)) { + _set_err(EFAULT, "Patch object is not of type json_type_array"); return -1; + } + + if (copy_from != NULL) + { + if (json_object_deep_copy(copy_from, base, NULL) < 0) + { + _set_err(ENOMEM, "Unable to copy copy_from using json_object_deep_copy()"); + return -1; + } + } /* Go through all operations ; apply them on res */ for (ii = 0; ii < json_object_array_length(patch); ii++) { struct json_object *jop, *jpath; struct json_object *patch_elem = json_object_array_get_idx(patch, ii); const char *op, *path; + + patch_error->patch_failure_idx = ii; + if (!json_object_object_get_ex(patch_elem, "op", &jop)) { - errno = EINVAL; - rc = -1; - break; + _set_err(EINVAL, "Patch object does not contain 'op' field"); + return -1; } op = json_object_get_string(jop); - json_object_object_get_ex(patch_elem, "path", &jpath); - path = json_object_get_string(jpath); + if (!json_object_object_get_ex(patch_elem, "path", &jpath)) { + _set_err(EINVAL, "Patch object does not contain 'path' field"); + return -1; + } + path = json_object_get_string(jpath); // Note: empty string is ok! if (!strcmp(op, "test")) - rc = json_patch_apply_test(res, patch_elem, path); + rc = json_patch_apply_test(base, patch_elem, path, patch_error); else if (!strcmp(op, "remove")) - rc = json_patch_apply_remove(res, path); + rc = json_patch_apply_remove(base, path, patch_error); else if (!strcmp(op, "add")) - rc = json_patch_apply_add_replace(res, patch_elem, path, 1); + rc = json_patch_apply_add_replace(base, patch_elem, path, 1, patch_error); else if (!strcmp(op, "replace")) - rc = json_patch_apply_add_replace(res, patch_elem, path, 0); + rc = json_patch_apply_add_replace(base, patch_elem, path, 0, patch_error); else if (!strcmp(op, "move")) - rc = json_patch_apply_move_copy(res, patch_elem, path, 1); + rc = json_patch_apply_move_copy(base, patch_elem, path, 1, patch_error); else if (!strcmp(op, "copy")) - rc = json_patch_apply_move_copy(res, patch_elem, path, 0); + rc = json_patch_apply_move_copy(base, patch_elem, path, 0, patch_error); else { - errno = EINVAL; - rc = -1; - break; + _set_err(EINVAL, "Patch object has invalid 'op' field"); + return -1; } if (rc < 0) break; } - if (rc < 0) { - json_object_put(*res); - *res = NULL; - } - return rc; } diff --git a/json_patch.h b/json_patch.h index 80dc8b9..79de5f9 100644 --- a/json_patch.h +++ b/json_patch.h @@ -19,25 +19,59 @@ extern "C" { #endif +/** + * Details of an error that occurred during json_patch_apply() + */ +struct json_patch_error { + /** + * An errno value indicating what kind of error occurred. + * Possible values include: + * - ENOENT - A path referenced in the operation does not exist. + * - EINVAL - An invalid operation or with invalid path was attempted + * - ENOMEM - Unable to allocate memory + * - EFAULT - Invalid arguments were passed to json_patch_apply() + * (i.e. a C API error, vs. a data error like EINVAL) + */ + int errno_code; + + /** + * The index into the patch array of the operation that failed, + * or SIZE_T_MAX for overall errors. + */ + size_t patch_failure_idx; + + /** + * A human readable error message. + * Allocated from static storage, does not need to be freed. + */ + const char *errmsg; +}; + /** * Apply the JSON patch to the base object. - * The patch object must be formatted as per RFC 6902. + * The patch object must be formatted as per RFC 6902, i.e. + * a json_type_array containing patch operations. * If the patch is not correctly formatted, an error will * be returned. * - * The original `base` object will first be copied, and then - * the patch will be applied. - * If anything fails during patching, the `res` object will be - * NULL and the function will return a negative result. + * The json_object at *base will be modified in place. + * Exactly one of *base or copy_from must be non-NULL. + * If *base is NULL, a new copy of copy_from will allocated and populated + * using json_object_deep_copy(). In this case json_object_put() _must_ be + * used to free *base even if the overall patching operation fails. + * + * If anything fails during patching a negative value will be returned, + * and patch_error (if non-NULL) will be populated with error details. * - * @param base the JSON object which to patch + * @param base a pointer to the JSON object which to patch * @param patch the JSON object that describes the patch to be applied - * @param the resulting patched JSON object + * @param copy_from a JSON object to copy to *base + * @param patch_error optional, details about errors * - * @return negative if an error (or not found), or 0 if succeeded + * @return negative if an error (or not found), or 0 if patch completely applied */ -JSON_EXPORT int json_patch_apply(struct json_object *base, struct json_object *patch, - struct json_object **res); +JSON_EXPORT int json_patch_apply(struct json_object *copy_from, struct json_object *patch, + struct json_object **base, struct json_patch_error *patch_error); #ifdef __cplusplus } diff --git a/tests/test_json_patch.c b/tests/test_json_patch.c index 8dd593f..d53c980 100644 --- a/tests/test_json_patch.c +++ b/tests/test_json_patch.c @@ -1,6 +1,7 @@ #ifdef NDEBUG #undef NDEBUG #endif +#include "strerror_override.h" #include #include #include @@ -40,19 +41,27 @@ void test_json_patch_op(struct json_object *jo) return; } fflush(stdout); + struct json_patch_error jperr; if (error) { - assert(-1 == json_patch_apply(doc, patch, &res)); - assert(res == NULL); + assert(-1 == json_patch_apply(doc, patch, &res, &jperr)); + assert(jperr.errno_code != 0); + printf("OK\n"); + printf(" => json_patch_apply failed as expected: %s at patch idx %zu: %s\n", + strerror(jperr.errno_code), jperr.patch_failure_idx, jperr.errmsg); + json_object_put(res); } else { - ret = json_patch_apply(doc, patch, &res); + ret = json_patch_apply(doc, patch, &res, &jperr); if (ret) { fprintf(stderr, "json_patch_apply() returned '%d'\n", ret); fprintf(stderr, "Expected: %s\n", json_object_get_string(expected)); fprintf(stderr, "Got: %s\n", json_object_get_string(res)); + fprintf(stderr, "json_patch_apply failed: %s at patch idx %zu: %s\n", + strerror(jperr.errno_code), jperr.patch_failure_idx, jperr.errmsg); fflush(stderr); assert(0); } assert(res != NULL); + assert(jperr.errno_code == 0); ret = json_object_equal(expected, res); if (ret == 0) { fprintf(stderr, "json_object_equal() returned '%d'\n", ret); @@ -63,9 +72,9 @@ void test_json_patch_op(struct json_object *jo) } json_object_put(res); res = NULL; + printf("OK\n"); } - printf("OK\n"); } void test_json_patch_using_file(const char *testdir, const char *filename) diff --git a/tests/test_json_patch.expected b/tests/test_json_patch.expected index 64e59b8..9d2887a 100644 --- a/tests/test_json_patch.expected +++ b/tests/test_json_patch.expected @@ -1,4 +1,5 @@ Testing '4.1. add with missing object', doc '{ "q": { "bar": 2 } }' patch '[ { "op": "add", "path": "\/a\/b", "value": 1 } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Failed to set value at path referenced by 'path' field Testing 'A.1. Adding an Object Member', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/baz", "value": "qux" } ]' : OK Testing 'A.2. Adding an Array Element', doc '{ "foo": [ "bar", "baz" ] }' patch '[ { "op": "add", "path": "\/foo\/1", "value": "qux" } ]' : OK Testing 'A.3. Removing an Object Member', doc '{ "baz": "qux", "foo": "bar" }' patch '[ { "op": "remove", "path": "\/baz" } ]' : OK @@ -8,12 +9,16 @@ Testing 'A.6. Moving a Value', doc '{ "foo": { "bar": "baz", "waldo": "fred" }, Testing 'A.7. Moving an Array Element', doc '{ "foo": [ "all", "grass", "cows", "eat" ] }' patch '[ { "op": "move", "from": "\/foo\/1", "path": "\/foo\/3" } ]' : OK Testing 'A.8. Testing a Value: Success', doc '{ "baz": "qux", "foo": [ "a", 2, "c" ] }' patch '[ { "op": "test", "path": "\/baz", "value": "qux" }, { "op": "test", "path": "\/foo\/1", "value": 2 } ]' : OK Testing 'A.9. Testing a Value: Error', doc '{ "baz": "qux" }' patch '[ { "op": "test", "path": "\/baz", "value": "bar" } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field Testing 'A.10. Adding a nested Member Object', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/child", "value": { "grandchild": { } } } ]' : OK Testing 'A.11. Ignoring Unrecognized Elements', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/baz", "value": "qux", "xyz": 123 } ]' : OK Testing 'A.12. Adding to a Non-existent Target', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/baz\/bat", "value": "qux" } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Failed to set value at path referenced by 'path' field Testing 'A.13 Invalid JSON Patch Document', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "\/baz", "value": "qux" } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field Testing 'A.14. ~ Escape Ordering', doc '{ "\/": 9, "~1": 10 }' patch '[ { "op": "test", "path": "\/~01", "value": 10 } ]' : OK Testing 'A.15. Comparing Strings and Numbers', doc '{ "\/": 9, "~1": 10 }' patch '[ { "op": "test", "path": "\/~01", "value": "10" } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field Testing 'A.16. Adding an Array Value', doc '{ "foo": [ "bar" ] }' patch '[ { "op": "add", "path": "\/foo\/-", "value": [ "abc", "def" ] } ]' : OK Testing 'empty list, empty docs', doc '{ }' patch '[ ]' : OK Testing 'empty patch list', doc '{ "foo": 1 }' patch '[ ]' : OK @@ -34,7 +39,9 @@ Testing 'Add, /foo/ deep target (trailing slash)', doc '{ "foo": { } }' patch '[ Testing 'Add composite value at top level', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": [ 1, 2 ] } ]' : OK Testing 'Add into composite value', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "add", "path": "\/baz\/0\/foo", "value": "world" } ]' : OK Testing 'Out of bounds (upper)', doc '{ "bar": [ 1, 2 ] }' patch '[ { "op": "add", "path": "\/bar\/8", "value": "5" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field Testing 'Out of bounds (lower)', doc '{ "bar": [ 1, 2 ] }' patch '[ { "op": "add", "path": "\/bar\/-1", "value": "5" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field Testing '(null)', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": true } ]' : OK Testing '(null)', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": false } ]' : OK Testing '(null)', doc '{ "foo": 1 }' patch '[ { "op": "add", "path": "\/bar", "value": null } ]' : OK @@ -44,9 +51,12 @@ Testing '(null)', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/1", Testing '(null)', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/0", "value": "bar" } ]' : OK Testing 'push item to array via last index + 1', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/2", "value": "bar" } ]' : OK Testing 'add item to array at index > length should fail', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/3", "value": "bar" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field Testing 'test against implementation-specific numeric parsing', doc '{ "1e0": "foo" }' patch '[ { "op": "test", "path": "\/1e0", "value": "foo" } ]' : OK Testing 'test with bad number should fail', doc '[ "foo", "bar" ]' patch '[ { "op": "test", "path": "\/1e0", "value": "bar" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field Testing 'Object operation on array target', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/bar", "value": 42 } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field Testing 'value in array add not flattened', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/1", "value": [ "bar", "baz" ] } ]' : OK Testing '(null)', doc '{ "foo": 1, "bar": [ 1, 2, 3, 4 ] }' patch '[ { "op": "remove", "path": "\/bar" } ]' : OK Testing '(null)', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "remove", "path": "\/baz\/0\/qux" } ]' : OK @@ -60,6 +70,7 @@ Testing '(null)', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/0", "value Testing 'value in array replace not flattened', doc '[ "foo", "sil" ]' patch '[ { "op": "replace", "path": "\/1", "value": [ "bar", "baz" ] } ]' : OK Testing 'replace whole document', doc '{ "foo": "bar" }' patch '[ { "op": "replace", "path": "", "value": { "baz": "qux" } } ]' : OK Testing 'test replace with missing parent key should fail', doc '{ "bar": "baz" }' patch '[ { "op": "replace", "path": "\/foo\/bar", "value": false } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field Testing 'spurious patch properties', doc '{ "foo": 1 }' patch '[ { "op": "test", "path": "\/foo", "value": 1, "spurious": 1 } ]' : OK Testing 'null value should be valid obj property', doc '{ "foo": null }' patch '[ { "op": "test", "path": "\/foo", "value": null } ]' : OK Testing 'null value should be valid obj property to be replaced with something truthy', doc '{ "foo": null }' patch '[ { "op": "replace", "path": "\/foo", "value": "truthy" } ]' : OK @@ -71,6 +82,7 @@ Testing 'test should pass despite rearrangement', doc '{ "foo": { "foo": 1, "bar Testing 'test should pass despite (nested) rearrangement', doc '{ "foo": [ { "foo": 1, "bar": 2 } ] }' patch '[ { "op": "test", "path": "\/foo", "value": [ { "bar": 2, "foo": 1 } ] } ]' : OK Testing 'test should pass - no error', doc '{ "foo": { "bar": [ 1, 2, 5, 4 ] } }' patch '[ { "op": "test", "path": "\/foo", "value": { "bar": [ 1, 2, 5, 4 ] } } ]' : OK Testing 'test op should fail', doc '{ "foo": { "bar": [ 1, 2, 5, 4 ] } }' patch '[ { "op": "test", "path": "\/foo", "value": [ 1, 2 ] } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Value of element referenced by 'path' field did not match 'value' field Testing 'Whole document', doc '{ "foo": 1 }' patch '[ { "op": "test", "path": "", "value": { "foo": 1 } } ]' : SKIPPING - no expected or error conditions in test Testing 'Empty-string element', doc '{ "": 1 }' patch '[ { "op": "test", "path": "\/", "value": 1 } ]' : OK Testing '(null)', doc '{ "foo": [ "bar", "baz" ], "": 0, "a\/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, "i\\j": 5, "k\"l": 6, " ": 7, "m~n": 8 }' patch '[ { "op": "test", "path": "\/foo", "value": [ "bar", "baz" ] }, { "op": "test", "path": "\/foo\/0", "value": "bar" }, { "op": "test", "path": "\/", "value": 0 }, { "op": "test", "path": "\/a~1b", "value": 1 }, { "op": "test", "path": "\/c%d", "value": 2 }, { "op": "test", "path": "\/e^f", "value": 3 }, { "op": "test", "path": "\/g|h", "value": 4 }, { "op": "test", "path": "\/i\\j", "value": 5 }, { "op": "test", "path": "\/k\"l", "value": 6 }, { "op": "test", "path": "\/ ", "value": 7 }, { "op": "test", "path": "\/m~0n", "value": 8 } ]' : OK @@ -82,29 +94,52 @@ Testing 'replacing the root of the document is possible with add', doc '{ "foo": Testing 'Adding to "/-" adds to the end of the array', doc '[ 1, 2 ]' patch '[ { "op": "add", "path": "\/-", "value": { "foo": [ "bar", "baz" ] } } ]' : OK Testing 'Adding to "/-" adds to the end of the array, even n levels down', doc '[ 1, 2, [ 3, [ 4, 5 ] ] ]' patch '[ { "op": "add", "path": "\/2\/1\/-", "value": { "foo": [ "bar", "baz" ] } } ]' : OK Testing 'test remove with bad number should fail', doc '{ "foo": 1, "baz": [ { "qux": "hello" } ] }' patch '[ { "op": "remove", "path": "\/baz\/1e0\/qux" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field Testing 'test remove on array', doc '[ 1, 2, 3, 4 ]' patch '[ { "op": "remove", "path": "\/0" } ]' : OK Testing 'test repeated removes', doc '[ 1, 2, 3, 4 ]' patch '[ { "op": "remove", "path": "\/1" }, { "op": "remove", "path": "\/2" } ]' : OK Testing 'test remove with bad index should fail', doc '[ 1, 2, 3, 4 ]' patch '[ { "op": "remove", "path": "\/1e0" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field Testing 'test replace with bad number should fail', doc '[ "" ]' patch '[ { "op": "replace", "path": "\/1e0", "value": false } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field Testing 'test copy with bad number should fail', doc '{ "baz": [ 1, 2, 3 ], "bar": 1 }' patch '[ { "op": "copy", "from": "\/baz\/1e0", "path": "\/boo" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid from field Testing 'test move with bad number should fail', doc '{ "foo": 1, "baz": [ 1, 2, 3, 4 ] }' patch '[ { "op": "move", "from": "\/baz\/1e0", "path": "\/foo" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid from field Testing 'test add with bad number should fail', doc '[ "foo", "sil" ]' patch '[ { "op": "add", "path": "\/1e0", "value": "bar" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field Testing 'missing 'path' parameter', doc '{ }' patch '[ { "op": "add", "value": "bar" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain 'path' field Testing ''path' parameter with null value', doc '{ }' patch '[ { "op": "add", "path": null, "value": "bar" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field Testing 'invalid JSON Pointer token', doc '{ }' patch '[ { "op": "add", "path": "foo", "value": "bar" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field Testing 'missing 'value' parameter to add', doc '[ 1 ]' patch '[ { "op": "add", "path": "\/-" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field Testing 'missing 'value' parameter to replace', doc '[ 1 ]' patch '[ { "op": "replace", "path": "\/0" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field Testing 'missing 'value' parameter to test', doc '[ null ]' patch '[ { "op": "test", "path": "\/0" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field Testing 'missing value parameter to test - where undef is falsy', doc '[ false ]' patch '[ { "op": "test", "path": "\/0" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain a 'value' field Testing 'missing from parameter to copy', doc '[ 1 ]' patch '[ { "op": "copy", "path": "\/-" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch does not contain a 'from' field Testing 'missing from location to copy', doc '{ "foo": 1 }' patch '[ { "op": "copy", "from": "\/bar", "path": "\/foo" } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by from field Testing 'missing from parameter to move', doc '{ "foo": 1 }' patch '[ { "op": "move", "path": "" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch does not contain a 'from' field Testing 'missing from location to move', doc '{ "foo": 1 }' patch '[ { "op": "move", "from": "\/bar", "path": "\/foo" } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by from field Testing 'duplicate ops', doc '{ "foo": "bar" }' patch '[ { "op": "move", "path": "\/baz", "value": "qux", "from": "\/foo" } ]' : SKIPPING - disabled in the test spec Testing 'unrecognized op should fail', doc '{ "foo": 1 }' patch '[ { "op": "spam", "path": "\/foo", "value": 1 } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object has invalid 'op' field Testing 'test with bad array number that has leading zeros', doc '[ "foo", "bar" ]' patch '[ { "op": "test", "path": "\/00", "value": "foo" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field Testing 'test with bad array number that has leading zeros', doc '[ "foo", "bar" ]' patch '[ { "op": "test", "path": "\/01", "value": "bar" } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Invalid path field Testing 'Removing nonexistent field', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "\/baz" } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field Testing 'Removing deep nonexistent path', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": "\/missing1\/missing2" } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field Testing 'Removing nonexistent index', doc '[ "foo", "bar" ]' patch '[ { "op": "remove", "path": "\/2" } ]' : OK + => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field Testing 'Patch with different capitalisation than doc', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/FOO", "value": "BAR" } ]' : OK