jsonapi_core 0.2.1

A typed JSON:API v1.1 serialization library for Rust
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//! Atomic operations request types.

use std::collections::HashSet;

use serde::de;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::{Identity, PrimaryData, Resource};

/// Request envelope carrying an ordered list of atomic operations.
///
/// Wire form: `{"atomic:operations": [...]}`. Execution order is significant —
/// later operations may reference `lid` values introduced by earlier `add` ops.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct AtomicRequest {
    /// Ordered list of operations to execute atomically.
    ///
    /// The array must be present on the wire; an absent `atomic:operations`
    /// key is a deserialization error.
    #[serde(rename = "atomic:operations")]
    pub operations: Vec<AtomicOperation>,
}

/// A single atomic operation.
///
/// Discriminated on the wire by the `op` field, which takes the lowercase
/// values `"add"`, `"update"`, or `"remove"`.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "lowercase")]
pub enum AtomicOperation {
    /// Create a new resource, or add to a to-many relationship.
    Add {
        /// Where the operation applies. Often empty for top-level resource creation.
        #[serde(flatten)]
        target: OperationTarget,
        /// Resource(s) being created, or identifier(s) being added to a relationship.
        data: PrimaryData<Resource>,
    },
    /// Update an existing resource or relationship.
    Update {
        /// Target of the update.
        #[serde(flatten)]
        target: OperationTarget,
        /// Replacement resource or relationship linkage.
        data: PrimaryData<Resource>,
    },
    /// Delete a resource or remove linkage from a relationship.
    Remove {
        /// Target of the removal.
        #[serde(flatten)]
        target: OperationTarget,
    },
}

/// Where an atomic operation applies.
///
/// Three legal wire states are representable:
///
/// | `ref`    | `href`   | Meaning                                               |
/// |----------|----------|-------------------------------------------------------|
/// | `Some`   | `None`   | Structured pointer (type + id/lid + optional rel).    |
/// | `None`   | `Some`   | URL pointer (alternative to `ref`).                   |
/// | `None`   | `None`   | No target — e.g. `add` creating a top-level resource. |
///
/// Having both set is spec-illegal. Use [`OperationTarget::is_valid`] to check,
/// or [`AtomicRequest::validate_lid_refs`](crate::AtomicRequest::validate_lid_refs)
/// which also flags this case along with `lid` reference errors.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct OperationTarget {
    /// Structured reference to the target resource.
    #[serde(rename = "ref", default, skip_serializing_if = "Option::is_none")]
    pub r#ref: Option<OperationRef>,

    /// URL pointer to the target (alternative to `ref`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub href: Option<String>,
}

impl OperationTarget {
    /// Returns `false` if both `ref` and `href` are set (spec violation).
    #[must_use]
    pub fn is_valid(&self) -> bool {
        !(self.r#ref.is_some() && self.href.is_some())
    }
}

/// Structured reference to the target of an atomic operation.
///
/// Reuses [`Identity`](crate::Identity) for id/lid consistency with
/// [`ResourceIdentifier`](crate::ResourceIdentifier). An optional
/// `relationship` name narrows the operation to a specific relationship
/// of the referenced resource.
#[derive(Debug, Clone, PartialEq)]
pub struct OperationRef {
    /// JSON:API type string.
    pub type_: String,
    /// Server-assigned id or client-local lid.
    pub identity: Identity,
    /// Relationship name, if this op targets a relationship rather than the resource itself.
    pub relationship: Option<String>,
}

#[derive(Serialize)]
struct OperationRefSerRepr<'a> {
    #[serde(rename = "type")]
    type_: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    lid: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    relationship: Option<&'a str>,
}

#[derive(Deserialize)]
struct OperationRefDeRepr {
    #[serde(rename = "type")]
    type_: String,
    #[serde(default)]
    id: Option<String>,
    #[serde(default)]
    lid: Option<String>,
    #[serde(default)]
    relationship: Option<String>,
}

impl Serialize for OperationRef {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let (id, lid) = match &self.identity {
            Identity::Id(id) => (Some(id.as_str()), None),
            Identity::Lid(lid) => (None, Some(lid.as_str())),
        };
        OperationRefSerRepr {
            type_: &self.type_,
            id,
            lid,
            relationship: self.relationship.as_deref(),
        }
        .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for OperationRef {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let repr = OperationRefDeRepr::deserialize(deserializer)?;
        let identity = match (repr.id, repr.lid) {
            (Some(_), Some(_)) => {
                return Err(de::Error::custom(
                    "operation ref must not have both `id` and `lid`",
                ));
            }
            (Some(id), None) => Identity::Id(id),
            (None, Some(lid)) => Identity::Lid(lid),
            (None, None) => return Err(de::Error::custom("operation ref must have `id` or `lid`")),
        };
        Ok(OperationRef {
            type_: repr.type_,
            identity,
            relationship: repr.relationship,
        })
    }
}

impl AtomicRequest {
    /// Check that every `lid` referenced by an operation is introduced by a
    /// strictly earlier `add` operation, that no `lid` is introduced twice,
    /// and that no operation targets both `ref` and `href`.
    ///
    /// Returns `Error::InvalidAtomicOperation` at the first offending index.
    ///
    /// Serialization and deserialization of `AtomicRequest` remain infallible
    /// with respect to `lid` semantics; pre-flight validation is opt-in.
    pub fn validate_lid_refs(&self) -> crate::Result<()> {
        let mut introduced: HashSet<String> = HashSet::new();

        for (index, op) in self.operations.iter().enumerate() {
            let target = op.target();

            if !target.is_valid() {
                return Err(crate::Error::InvalidAtomicOperation {
                    index,
                    reason: "operation target must not have both `ref` and `href`".into(),
                });
            }

            if let Some(r) = &target.r#ref
                && let Identity::Lid(lid) = &r.identity
                && !introduced.contains(lid)
            {
                return Err(crate::Error::InvalidAtomicOperation {
                    index,
                    reason: format!("ref.lid `{lid}` not introduced by an earlier `add`"),
                });
            }

            if let AtomicOperation::Add { data, .. } = op {
                for resource in primary_data_resources(data) {
                    if let Some(lid) = &resource.lid
                        && !introduced.insert(lid.clone())
                    {
                        return Err(crate::Error::InvalidAtomicOperation {
                            index,
                            reason: format!("duplicate `lid` introduction: `{lid}`"),
                        });
                    }
                }
            }
        }

        Ok(())
    }
}

impl AtomicOperation {
    /// Borrow the operation's target (present on all variants).
    fn target(&self) -> &OperationTarget {
        match self {
            AtomicOperation::Add { target, .. }
            | AtomicOperation::Update { target, .. }
            | AtomicOperation::Remove { target } => target,
        }
    }
}

/// Iterate over the resources inside a `PrimaryData<Resource>`, skipping `Null`.
fn primary_data_resources(
    data: &PrimaryData<Resource>,
) -> Box<dyn Iterator<Item = &Resource> + '_> {
    match data {
        PrimaryData::Null => Box::new(std::iter::empty()),
        PrimaryData::Single(boxed) => Box::new(std::iter::once(boxed.as_ref())),
        PrimaryData::Many(vec) => Box::new(vec.iter()),
    }
}

#[cfg(test)]
mod operation_ref_tests {
    use super::*;
    use crate::Identity;

    #[test]
    fn serializes_with_id() {
        let r = OperationRef {
            type_: "articles".into(),
            identity: Identity::Id("1".into()),
            relationship: None,
        };
        assert_eq!(
            serde_json::to_string(&r).unwrap(),
            r#"{"type":"articles","id":"1"}"#
        );
    }

    #[test]
    fn serializes_with_lid() {
        let r = OperationRef {
            type_: "articles".into(),
            identity: Identity::Lid("local-1".into()),
            relationship: None,
        };
        assert_eq!(
            serde_json::to_string(&r).unwrap(),
            r#"{"type":"articles","lid":"local-1"}"#
        );
    }

    #[test]
    fn serializes_with_relationship() {
        let r = OperationRef {
            type_: "articles".into(),
            identity: Identity::Id("1".into()),
            relationship: Some("comments".into()),
        };
        assert_eq!(
            serde_json::to_string(&r).unwrap(),
            r#"{"type":"articles","id":"1","relationship":"comments"}"#
        );
    }

    #[test]
    fn deserializes_with_id() {
        let r: OperationRef = serde_json::from_str(r#"{"type":"articles","id":"1"}"#).unwrap();
        assert_eq!(r.type_, "articles");
        assert_eq!(r.identity, Identity::Id("1".into()));
        assert_eq!(r.relationship, None);
    }

    #[test]
    fn deserializes_with_lid() {
        let r: OperationRef =
            serde_json::from_str(r#"{"type":"articles","lid":"local-1"}"#).unwrap();
        assert_eq!(r.identity, Identity::Lid("local-1".into()));
    }

    #[test]
    fn deserializes_with_relationship() {
        let r: OperationRef =
            serde_json::from_str(r#"{"type":"articles","id":"1","relationship":"comments"}"#)
                .unwrap();
        assert_eq!(r.relationship, Some("comments".into()));
    }

    #[test]
    fn rejects_missing_identity() {
        let result: std::result::Result<OperationRef, _> =
            serde_json::from_str(r#"{"type":"articles"}"#);
        assert!(result.is_err());
    }

    #[test]
    fn rejects_both_id_and_lid() {
        let result: std::result::Result<OperationRef, _> =
            serde_json::from_str(r#"{"type":"articles","id":"1","lid":"local-1"}"#);
        assert!(result.is_err());
    }
}

#[cfg(test)]
mod operation_target_tests {
    use super::*;
    use crate::Identity;

    fn ref_only() -> OperationTarget {
        OperationTarget {
            r#ref: Some(OperationRef {
                type_: "articles".into(),
                identity: Identity::Id("1".into()),
                relationship: None,
            }),
            href: None,
        }
    }

    fn href_only() -> OperationTarget {
        OperationTarget {
            r#ref: None,
            href: Some("/articles/1".into()),
        }
    }

    fn neither() -> OperationTarget {
        OperationTarget {
            r#ref: None,
            href: None,
        }
    }

    fn both() -> OperationTarget {
        OperationTarget {
            r#ref: Some(OperationRef {
                type_: "articles".into(),
                identity: Identity::Id("1".into()),
                relationship: None,
            }),
            href: Some("/articles/1".into()),
        }
    }

    #[test]
    fn is_valid_ref_only() {
        assert!(ref_only().is_valid());
    }

    #[test]
    fn is_valid_href_only() {
        assert!(href_only().is_valid());
    }

    #[test]
    fn is_valid_neither() {
        assert!(neither().is_valid());
    }

    #[test]
    fn is_invalid_when_both_set() {
        assert!(!both().is_valid());
    }

    #[test]
    fn serializes_ref_only() {
        let json = serde_json::to_value(ref_only()).unwrap();
        assert_eq!(json["ref"]["type"], "articles");
        assert_eq!(json["ref"]["id"], "1");
        assert!(json.get("href").is_none());
    }

    #[test]
    fn serializes_href_only() {
        let json = serde_json::to_value(href_only()).unwrap();
        assert_eq!(json["href"], "/articles/1");
        assert!(json.get("ref").is_none());
    }

    #[test]
    fn serializes_neither_as_empty_object() {
        let json = serde_json::to_string(&neither()).unwrap();
        assert_eq!(json, "{}");
    }

    #[test]
    fn deserializes_ref_only() {
        let t: OperationTarget =
            serde_json::from_str(r#"{"ref":{"type":"articles","id":"1"}}"#).unwrap();
        assert!(t.r#ref.is_some());
        assert!(t.href.is_none());
    }

    #[test]
    fn deserializes_href_only() {
        let t: OperationTarget = serde_json::from_str(r#"{"href":"/articles/1"}"#).unwrap();
        assert!(t.r#ref.is_none());
        assert_eq!(t.href.as_deref(), Some("/articles/1"));
    }

    #[test]
    fn deserializes_empty_object() {
        let t: OperationTarget = serde_json::from_str("{}").unwrap();
        assert!(t.r#ref.is_none());
        assert!(t.href.is_none());
    }
}

#[cfg(test)]
mod atomic_operation_tests {
    use super::*;
    use crate::{Identity, PrimaryData, Resource};

    fn sample_ref() -> OperationRef {
        OperationRef {
            type_: "articles".into(),
            identity: Identity::Id("1".into()),
            relationship: None,
        }
    }

    fn sample_resource() -> Resource {
        Resource {
            type_: "articles".into(),
            id: None,
            lid: Some("local-1".into()),
            attributes: serde_json::json!({"title": "Hello"}),
            relationships: std::collections::BTreeMap::new(),
            links: None,
            meta: None,
        }
    }

    #[test]
    fn add_op_serializes_with_lowercase_op() {
        let op = AtomicOperation::Add {
            target: OperationTarget::default(),
            data: PrimaryData::Single(Box::new(sample_resource())),
        };
        let json = serde_json::to_value(&op).unwrap();
        assert_eq!(json["op"], "add");
    }

    #[test]
    fn update_op_serializes_with_lowercase_op() {
        let op = AtomicOperation::Update {
            target: OperationTarget {
                r#ref: Some(sample_ref()),
                href: None,
            },
            data: PrimaryData::Single(Box::new(sample_resource())),
        };
        let json = serde_json::to_value(&op).unwrap();
        assert_eq!(json["op"], "update");
        assert_eq!(json["ref"]["type"], "articles");
    }

    #[test]
    fn remove_op_has_no_data_field() {
        let op = AtomicOperation::Remove {
            target: OperationTarget {
                r#ref: Some(sample_ref()),
                href: None,
            },
        };
        let json = serde_json::to_value(&op).unwrap();
        assert_eq!(json["op"], "remove");
        assert_eq!(json["ref"]["id"], "1");
        assert!(json.get("data").is_none());
    }

    #[test]
    fn deserializes_add() {
        let json = r#"{"op":"add","data":{"type":"articles","lid":"l1","attributes":{}}}"#;
        let op: AtomicOperation = serde_json::from_str(json).unwrap();
        match op {
            AtomicOperation::Add { target, data } => {
                assert!(target.r#ref.is_none() && target.href.is_none());
                assert!(matches!(data, PrimaryData::Single(_)));
            }
            _ => panic!("expected Add variant"),
        }
    }

    #[test]
    fn deserializes_update_with_ref() {
        let json = r#"{"op":"update","ref":{"type":"articles","id":"1"},"data":{"type":"articles","id":"1","attributes":{"title":"New"}}}"#;
        let op: AtomicOperation = serde_json::from_str(json).unwrap();
        match op {
            AtomicOperation::Update { target, .. } => {
                assert_eq!(target.r#ref.unwrap().type_, "articles");
            }
            _ => panic!("expected Update variant"),
        }
    }

    #[test]
    fn deserializes_remove() {
        let json = r#"{"op":"remove","ref":{"type":"articles","id":"1"}}"#;
        let op: AtomicOperation = serde_json::from_str(json).unwrap();
        assert!(matches!(op, AtomicOperation::Remove { .. }));
    }

    #[test]
    fn rejects_unknown_op() {
        let json = r#"{"op":"frobnicate","ref":{"type":"articles","id":"1"}}"#;
        let result: std::result::Result<AtomicOperation, _> = serde_json::from_str(json);
        assert!(result.is_err());
    }

    #[test]
    fn add_many_to_relationship_round_trip() {
        let op = AtomicOperation::Add {
            target: OperationTarget {
                r#ref: Some(OperationRef {
                    type_: "articles".into(),
                    identity: Identity::Id("1".into()),
                    relationship: Some("tags".into()),
                }),
                href: None,
            },
            data: PrimaryData::Many(vec![Resource {
                type_: "tags".into(),
                id: Some("5".into()),
                lid: None,
                attributes: serde_json::json!({}),
                relationships: std::collections::BTreeMap::new(),
                links: None,
                meta: None,
            }]),
        };
        let json = serde_json::to_value(&op).unwrap();
        assert_eq!(json["op"], "add");
        assert_eq!(json["ref"]["relationship"], "tags");
        assert_eq!(json["data"][0]["type"], "tags");

        let round: AtomicOperation = serde_json::from_value(json).unwrap();
        assert_eq!(round, op);
    }
}

#[cfg(test)]
mod atomic_request_tests {
    use super::*;
    use crate::{Identity, PrimaryData, Resource};

    #[test]
    fn empty_request_round_trips() {
        let req = AtomicRequest::default();
        let json = serde_json::to_string(&req).unwrap();
        assert_eq!(json, r#"{"atomic:operations":[]}"#);
        let round: AtomicRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(round, req);
    }

    #[test]
    fn multi_op_request_round_trips() {
        let req = AtomicRequest {
            operations: vec![
                AtomicOperation::Add {
                    target: OperationTarget::default(),
                    data: PrimaryData::Single(Box::new(Resource {
                        type_: "people".into(),
                        id: None,
                        lid: Some("p1".into()),
                        attributes: serde_json::json!({"name": "Dan"}),
                        relationships: std::collections::BTreeMap::new(),
                        links: None,
                        meta: None,
                    })),
                },
                AtomicOperation::Remove {
                    target: OperationTarget {
                        r#ref: Some(OperationRef {
                            type_: "articles".into(),
                            identity: Identity::Id("9".into()),
                            relationship: None,
                        }),
                        href: None,
                    },
                },
            ],
        };
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["atomic:operations"].as_array().unwrap().len(), 2);
        assert_eq!(json["atomic:operations"][0]["op"], "add");
        assert_eq!(json["atomic:operations"][1]["op"], "remove");

        let round: AtomicRequest = serde_json::from_value(json).unwrap();
        assert_eq!(round, req);
    }

    #[test]
    fn missing_operations_field_is_error() {
        // Spec requires `atomic:operations`. Missing it must fail.
        let result: std::result::Result<AtomicRequest, _> = serde_json::from_str("{}");
        assert!(result.is_err());
    }
}

#[cfg(test)]
mod validate_lid_refs_tests {
    use super::*;
    use crate::{Error, Identity, PrimaryData, Resource};

    fn add_with_lid(lid: &str) -> AtomicOperation {
        AtomicOperation::Add {
            target: OperationTarget::default(),
            data: PrimaryData::Single(Box::new(Resource {
                type_: "people".into(),
                id: None,
                lid: Some(lid.into()),
                attributes: serde_json::json!({}),
                relationships: std::collections::BTreeMap::new(),
                links: None,
                meta: None,
            })),
        }
    }

    fn update_ref_lid(lid: &str) -> AtomicOperation {
        AtomicOperation::Update {
            target: OperationTarget {
                r#ref: Some(OperationRef {
                    type_: "people".into(),
                    identity: Identity::Lid(lid.into()),
                    relationship: None,
                }),
                href: None,
            },
            data: PrimaryData::Single(Box::new(Resource {
                type_: "people".into(),
                id: None,
                lid: Some(lid.into()),
                attributes: serde_json::json!({"name": "X"}),
                relationships: std::collections::BTreeMap::new(),
                links: None,
                meta: None,
            })),
        }
    }

    #[test]
    fn happy_path_introduce_then_reference() {
        let req = AtomicRequest {
            operations: vec![add_with_lid("p1"), update_ref_lid("p1")],
        };
        assert!(req.validate_lid_refs().is_ok());
    }

    #[test]
    fn empty_request_is_valid() {
        let req = AtomicRequest::default();
        assert!(req.validate_lid_refs().is_ok());
    }

    #[test]
    fn reference_before_introduction_errors() {
        let req = AtomicRequest {
            operations: vec![update_ref_lid("p1"), add_with_lid("p1")],
        };
        match req.validate_lid_refs().unwrap_err() {
            Error::InvalidAtomicOperation { index, reason } => {
                assert_eq!(index, 0);
                assert!(reason.contains("p1"));
            }
            other => panic!("expected InvalidAtomicOperation, got {other:?}"),
        }
    }

    #[test]
    fn unresolvable_lid_errors() {
        let req = AtomicRequest {
            operations: vec![update_ref_lid("ghost")],
        };
        match req.validate_lid_refs().unwrap_err() {
            Error::InvalidAtomicOperation { index, .. } => assert_eq!(index, 0),
            other => panic!("expected InvalidAtomicOperation, got {other:?}"),
        }
    }

    #[test]
    fn duplicate_lid_introduction_errors() {
        let req = AtomicRequest {
            operations: vec![add_with_lid("p1"), add_with_lid("p1")],
        };
        match req.validate_lid_refs().unwrap_err() {
            Error::InvalidAtomicOperation { index, reason } => {
                assert_eq!(index, 1);
                assert!(reason.contains("duplicate"));
            }
            other => panic!("expected InvalidAtomicOperation, got {other:?}"),
        }
    }

    #[test]
    fn target_with_both_ref_and_href_errors() {
        let req = AtomicRequest {
            operations: vec![AtomicOperation::Remove {
                target: OperationTarget {
                    r#ref: Some(OperationRef {
                        type_: "articles".into(),
                        identity: Identity::Id("1".into()),
                        relationship: None,
                    }),
                    href: Some("/articles/1".into()),
                },
            }],
        };
        match req.validate_lid_refs().unwrap_err() {
            Error::InvalidAtomicOperation { index, reason } => {
                assert_eq!(index, 0);
                assert!(reason.contains("ref") && reason.contains("href"));
            }
            other => panic!("expected InvalidAtomicOperation, got {other:?}"),
        }
    }
}