mirror of
https://github.com/json-c/json-c.git
synced 2026-03-23 23:19:06 +08:00
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.
This commit is contained in:
162
json_patch.c
162
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 <limits.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
if (!json_object_is_type(patch, json_type_array)) {
|
||||
_set_err(EFAULT, "Patch object is not of type json_type_array");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* errno should be set inside json_object_deep_copy() */
|
||||
if (json_object_deep_copy(base, res, NULL) < 0)
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user