json-patch-ext 0.3.2

Unofficial extensions and helpers for json_patch
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! This module provides some unofficial "extensions" to the [jsonpatch](https://jsonpatch.com)
//! format for describing changes to a JSON document.  In particular, it adds the `*` operator as a
//! valid token for arrays in a JSON document.  It means: apply this change to all elements of this
//! array.  For example, consider the following document:
//!
//! ```json
//! {
//!   "foo": {
//!     "bar": [
//!       {"baz": 1},
//!       {"baz": 2},
//!       {"baz": 3},
//!     ]
//!   }
//! }
//! ```
//!
//! The pathspec `/foo/bar/*/baz` would reference the `baz` field of all three array entries in the
//! `bar` array.  It is an error to use `*` to reference a field that is not an array.  It is an
//! error to use `*` at the end of a path, e.g., `/foo/*`.
//!
//! Additionally, this crate will auto-create parent paths for the AddOperation only, e.g., the
//! result of applying `AddOperation{ path: "/foo/bar", value: 1 }` to the empty document will be
//!
//! ```json
//! { "foo": {"bar": 1}}
//! ```

mod errors;
mod macros;

use json_patch::patch;
// mark these as re-exports in the generated docs (maybe related to
// https://github.com/rust-lang/rust/issues/131180?)
#[doc(no_inline)]
pub use json_patch::{
    AddOperation,
    CopyOperation,
    MoveOperation,
    Patch,
    PatchOperation,
    RemoveOperation,
    ReplaceOperation,
    TestOperation,
};
#[doc(no_inline)]
pub use jsonptr::index::Index;
pub use jsonptr::{
    Pointer,
    PointerBuf,
    Token,
};
use serde_json::{
    Value,
    json,
};

pub use crate::errors::PatchError;

pub mod prelude {
    pub use super::{
        AddOperation,
        CopyOperation,
        MoveOperation,
        Patch,
        PatchError,
        PatchOperation,
        Pointer,
        PointerBuf,
        RemoveOperation,
        ReplaceOperation,
        TestOperation,
        Token,
        add_operation,
        copy_operation,
        escape,
        format_ptr,
        matches,
        move_operation,
        patch_ext,
        remove_operation,
        replace_operation,
        test_operation,
    };
}

// PatchMode controls what to do if the referenced element does not exist in the object.
#[derive(Debug, Clone, Copy)]
enum PatchMode {
    Error,
    Create,
    Skip,
}

pub fn add_operation(path: PointerBuf, value: Value) -> PatchOperation {
    PatchOperation::Add(AddOperation { path, value })
}

pub fn copy_operation(from: PointerBuf, path: PointerBuf) -> PatchOperation {
    PatchOperation::Copy(CopyOperation { from, path })
}

pub fn move_operation(from: PointerBuf, path: PointerBuf) -> PatchOperation {
    PatchOperation::Move(MoveOperation { from, path })
}

pub fn remove_operation(path: PointerBuf) -> PatchOperation {
    PatchOperation::Remove(RemoveOperation { path })
}

pub fn replace_operation(path: PointerBuf, value: Value) -> PatchOperation {
    PatchOperation::Replace(ReplaceOperation { path, value })
}

pub fn test_operation(path: PointerBuf, value: Value) -> PatchOperation {
    PatchOperation::Test(TestOperation { path, value })
}

pub fn escape(input: &str) -> String {
    Token::new(input).encoded().into()
}

pub fn matches<'a>(path: &Pointer, value: &'a Value) -> Vec<(PointerBuf, &'a Value)> {
    let Some(idx) = path.as_str().find("/*") else {
        // Base case -- no stars;
        // If we can't resolve, there's no match to be found
        if let Ok(v) = path.resolve(value) {
            return vec![(path.to_buf(), v)];
        } else {
            return vec![];
        }
    };

    // we checked the index above so unwrap is safe here
    let (head, cons) = path.split_at(idx).unwrap();
    let mut res = vec![];

    // If we can't resolve the head, or it's not an array, no match found
    let Ok(head_val) = head.resolve(value) else {
        return vec![];
    };
    let Some(next_array_val) = head_val.as_array() else {
        return vec![];
    };

    for (i, v) in next_array_val.iter().enumerate() {
        // /1 is a valid pointer so the unwrap below is fine
        let idx_str = format!("/{i}");
        let idx_path = PointerBuf::parse(&idx_str).unwrap();

        // The cons pointer either looks like /* or /*/something, so we need to split_front
        // to get the array marker out, and either return the current path if there's nothing
        // else, or recurse and concatenate the subpath(s) to the head
        if let Some((_, c)) = cons.split_front() {
            let subpaths = matches(c, v);
            res.extend(subpaths.iter().map(|(p, v)| (head.concat(&idx_path.concat(p)), *v)));
        } else {
            unreachable!("cons can't be root");
        }
    }
    res
}

pub fn patch_ext(obj: &mut Value, p: PatchOperation) -> Result<(), PatchError> {
    match p {
        PatchOperation::Add(op) => add_or_replace(obj, &op.path, &op.value, false)?,
        PatchOperation::Remove(op) => remove(obj, &op.path)?,
        PatchOperation::Replace(op) => add_or_replace(obj, &op.path, &op.value, true)?,
        x => patch(obj, &[x])?,
    }
    Ok(())
}

fn add_or_replace(obj: &mut Value, path: &Pointer, value: &Value, replace: bool) -> Result<(), PatchError> {
    let Some((subpath, tail)) = path.split_back() else {
        return Ok(());
    };

    // "replace" requires that the path you're replacing already exist, therefore we set
    // create_if_not_exists = !replace.  We don't want to skip missing elements.
    let mode = if replace { PatchMode::Error } else { PatchMode::Create };
    for v in patch_ext_helper(subpath, obj, mode)? {
        match v {
            Value::Object(map) => {
                let key = tail.decoded().into();
                if replace && !map.contains_key(&key) {
                    return Err(PatchError::TargetDoesNotExist(path.to_string()));
                }
                map.insert(key, value.clone());
            },
            Value::Array(vec) => match tail.to_index()? {
                Index::Num(idx) => {
                    vec.get(idx).ok_or(PatchError::OutOfBounds(idx))?;
                    if replace {
                        vec[idx] = value.clone();
                    } else {
                        vec.insert(idx, value.clone());
                    }
                },
                Index::Next => {
                    vec.push(value.clone());
                },
            },
            _ => {
                return Err(PatchError::UnexpectedType(path.to_string()));
            },
        }
    }

    Ok(())
}

fn remove(obj: &mut Value, path: &Pointer) -> Result<(), PatchError> {
    let Some((subpath, key)) = path.split_back() else {
        *obj = Value::Null;
        return Ok(());
    };

    for v in patch_ext_helper(subpath, obj, PatchMode::Skip)? {
        match v {
            Value::Object(map) => {
                map.remove(key.decoded().as_ref());
            },
            Value::Array(vec) => {
                if key.decoded() == "*" {
                    vec.clear();
                } else if let Index::Num(idx) = key.to_index()? {
                    vec.get(idx).ok_or(PatchError::OutOfBounds(idx))?;
                    vec.remove(idx);
                } else {
                    return Err(PatchError::UnexpectedType(key.to_string()));
                }
            },
            _ => {
                return Err(PatchError::UnexpectedType(path.to_string()));
            },
        }
    }

    Ok(())
}

// Given JSON pointer, recursively walk through all the possible "end" values that the path
// references; return a mutable reference so we can make modifications at those points.
fn patch_ext_helper<'a>(
    path: &Pointer,
    value: &'a mut Value,
    mode: PatchMode,
) -> Result<Vec<&'a mut Value>, PatchError> {
    let Some(idx) = path.as_str().find("/*") else {
        if path.resolve(value).is_err() {
            match mode {
                PatchMode::Error => return Err(PatchError::TargetDoesNotExist(path.as_str().into())),
                PatchMode::Create => {
                    path.assign(value, json!({}))?;
                },
                PatchMode::Skip => return Ok(vec![]),
            }
        }
        return Ok(vec![path.resolve_mut(value)?]);
    };

    // we checked the index above so unwrap is safe here
    let (head, cons) = path.split_at(idx).unwrap();
    let mut res = vec![];

    // This is a little weird; if mode == Create, and the subpath up to this point doesn't exist,
    // we'll create an empty array which we won't iterate over at all.  I think that's
    // "approximately" fine and less surprising that not creating anything.
    if head.resolve(value).is_err() {
        match mode {
            PatchMode::Error => return Err(PatchError::TargetDoesNotExist(path.as_str().into())),
            PatchMode::Create => {
                path.assign(value, json!([]))?;
            },
            PatchMode::Skip => return Ok(vec![]),
        }
    }

    // Head now points at what we believe is an array; if not, it's an error.
    let next_array_val =
        head.resolve_mut(value)?.as_array_mut().ok_or(PatchError::UnexpectedType(head.as_str().into()))?;

    // Iterate over all the array values and recurse, returning all found values
    for v in next_array_val {
        // The cons pointer either looks like /* or /*/something, so we need to split_front
        // to get the array marker out, and either return the current value if there's nothing
        // else, or recurse and return all the found values
        if let Some((_, c)) = cons.split_front() {
            res.extend(patch_ext_helper(c, v, mode)?);
        } else {
            unreachable!("cons can't be root");
        }
    }
    Ok(res)
}

#[cfg(test)]
mod tests {
    use assertables::*;
    use rstest::*;

    use super::*;
    use crate as json_patch_ext; // make the macros work in the tests

    #[fixture]
    fn data() -> Value {
        json!({
            "foo": [
                {"baz": {"buzz": 0}},
                {"baz": {"quzz": 1}},
                {"baz": {"fixx": 2}},
            ],
        })
    }

    #[rstest]
    fn test_matches_1(data: Value) {
        let path = format_ptr!("/foo");
        let m: Vec<_> = matches(&path, &data).iter().map(|(p, _)| p.clone()).collect();
        assert_eq!(m, vec![format_ptr!("/foo")]);
    }

    #[rstest]
    fn test_matches_2(data: Value) {
        let path = format_ptr!("/foo/*/baz");
        let m: Vec<_> = matches(&path, &data).iter().map(|(p, _)| p.clone()).collect();
        assert_eq!(m, vec![format_ptr!("/foo/0/baz"), format_ptr!("/foo/1/baz"), format_ptr!("/foo/2/baz")]);
    }

    #[rstest]
    fn test_matches_3(data: Value) {
        let path = format_ptr!("/foo/*");
        let m: Vec<_> = matches(&path, &data).iter().map(|(p, _)| p.clone()).collect();
        assert_eq!(m, vec![format_ptr!("/foo/0"), format_ptr!("/foo/1"), format_ptr!("/foo/2")]);
    }

    #[rstest]
    #[case(format_ptr!("/foo/*/baz/fixx"))]
    #[case(format_ptr!("/foo/2/baz/fixx"))]
    fn test_matches_4(#[case] path: PointerBuf, data: Value) {
        let m: Vec<_> = matches(&path, &data).iter().map(|(p, _)| p.clone()).collect();
        assert_eq!(m, vec![format_ptr!("/foo/2/baz/fixx")]);
    }

    #[rstest]
    fn test_matches_root() {
        let path = format_ptr!("/*");
        let data = json!(["foo", "bar"]);
        let m: Vec<_> = matches(&path, &data).iter().map(|(p, _)| p.clone()).collect();
        assert_eq!(m, vec![format_ptr!("/0"), format_ptr!("/1")]);
    }

    #[rstest]
    #[case(format_ptr!("/*"))]
    #[case(format_ptr!("/food"))]
    #[case(format_ptr!("/foo/3/baz"))]
    #[case(format_ptr!("/foo/bar/baz"))]
    #[case(format_ptr!("/foo/0/baz/fixx"))]
    fn test_no_match(#[case] path: PointerBuf, data: Value) {
        let m = matches(&path, &data);
        assert_is_empty!(m);
    }

    #[rstest]
    fn test_patch_ext_add(mut data: Value) {
        let path = format_ptr!("/foo/*/baz/buzz");
        let res = patch_ext(&mut data, add_operation(path, json!(42)));
        assert_ok!(res);
        assert_eq!(
            data,
            json!({
                "foo": [
                    {"baz": {"buzz": 42 }},
                    {"baz": {"quzz": 1, "buzz": 42}},
                    {"baz": {"fixx": 2, "buzz": 42}},
                ],
            })
        );
    }

    #[rstest]
    fn test_patch_ext_add_vec1(mut data: Value) {
        let path = format_ptr!("/foo/1");
        let res = patch_ext(&mut data, add_operation(path, json!(42)));
        assert_ok!(res);
        assert_eq!(
            data,
            json!({
                "foo": [
                    {"baz": {"buzz": 0}},
                    42,
                    {"baz": {"quzz": 1}},
                    {"baz": {"fixx": 2}},
                ],
            })
        );
    }

    #[rstest]
    fn test_patch_ext_add_vec2(mut data: Value) {
        let path = format_ptr!("/foo/-");
        let res = patch_ext(&mut data, add_operation(path, json!(42)));
        assert_ok!(res);
        assert_eq!(
            data,
            json!({
                "foo": [
                    {"baz": {"buzz": 0}},
                    {"baz": {"quzz": 1}},
                    {"baz": {"fixx": 2}},
                    42,
                ],
            })
        );
    }

    #[rstest]
    fn test_patch_ext_add_vec_err(mut data: Value) {
        let path = format_ptr!("/foo/a");
        let res = patch_ext(&mut data, add_operation(path, json!(42)));
        assert_err!(res);
    }

    #[rstest]
    fn test_patch_ext_add_escaped() {
        let path = format_ptr!("/foo/bar/{}", escape("testing.sh/baz"));
        let mut data = json!({});
        let res = patch_ext(&mut data, add_operation(path, json!(42)));
        assert_ok!(res);
        assert_eq!(data, json!({"foo": {"bar": {"testing.sh/baz": 42}}}));
    }

    #[rstest]
    fn test_patch_ext_replace(mut data: Value) {
        let path = format_ptr!("/foo/*/baz");
        let res = patch_ext(&mut data, replace_operation(path, json!(42)));
        assert_ok!(res);
        assert_eq!(
            data,
            json!({
                "foo": [
                    {"baz": 42},
                    {"baz": 42},
                    {"baz": 42},
                ],
            })
        );
    }

    #[rstest]
    fn test_patch_ext_replace_vec1(mut data: Value) {
        let path = format_ptr!("/foo/1");
        let res = patch_ext(&mut data, replace_operation(path, json!(42)));
        assert_ok!(res);
        assert_eq!(
            data,
            json!({
                "foo": [
                    {"baz": {"buzz": 0}},
                    42,
                    {"baz": {"fixx": 2}},
                ],
            })
        );
    }

    #[rstest]
    fn test_patch_ext_replace_vec2(mut data: Value) {
        let path = format_ptr!("/foo/-");
        let res = patch_ext(&mut data, replace_operation(path, json!(42)));
        assert_ok!(res);
        assert_eq!(
            data,
            json!({
                "foo": [
                    {"baz": {"buzz": 0}},
                    {"baz": {"quzz": 1}},
                    {"baz": {"fixx": 2}},
                    42,
                ],
            })
        );
    }

    #[rstest]
    fn test_patch_ext_replace_err(mut data: Value) {
        let path = format_ptr!("/foo/*/baz/buzz");
        let res = patch_ext(&mut data, replace_operation(path, json!(42)));
        assert_err!(res);
    }

    #[rstest]
    fn test_patch_ext_remove_root(mut data: Value) {
        let path = format_ptr!("");
        let res = patch_ext(&mut data, remove_operation(path));
        assert_ok!(res);
        assert_eq!(data, json!(null));
    }

    #[rstest]
    fn test_patch_ext_remove(mut data: Value) {
        let path = format_ptr!("/foo/*/baz/quzz");
        let res = patch_ext(&mut data, remove_operation(path));
        assert_ok!(res);
        assert_eq!(
            data,
            json!({
                "foo": [
                    {"baz": {"buzz": 0}},
                    {"baz": {}},
                    {"baz": {"fixx": 2}},
                ],
            })
        );
    }

    #[rstest]
    fn test_patch_ext_remove_wildcard(mut data: Value) {
        let path = format_ptr!("/foo/*");
        let res = patch_ext(&mut data, remove_operation(path));
        assert_ok!(res);
        assert_eq!(data, json!({"foo": []}));
    }

    #[rstest]
    fn test_patch_ext_remove_nested_wildcards() {
        let mut data = json!([[
            [{"bar": "baz"}, {"qwerty": "yuiop"}],
            [{"fuzz": "quzz"}],
            [{"asdf": "hjkl"}],
        ]]);
        let path = format_ptr!("/*/*/*");
        let res = patch_ext(&mut data, remove_operation(path));
        assert_ok!(res);
        assert_eq!(data, json!([[[], [], []]]));
    }

    #[rstest]
    fn test_patch_ext_remove_vec(mut data: Value) {
        let path = format_ptr!("/foo/1");
        let res = patch_ext(&mut data, remove_operation(path));
        assert_ok!(res);
        assert_eq!(
            data,
            json!({
                "foo": [
                    {"baz": {"buzz": 0}},
                    {"baz": {"fixx": 2}},
                ],
            })
        );
    }

    #[rstest]
    fn test_patch_ext_remove_vec_err(mut data: Value) {
        let path = format_ptr!("/foo/-");
        let res = patch_ext(&mut data, remove_operation(path));
        assert_err!(res);
    }
}