apcore-cli 0.8.0

Command-line interface for apcore modules
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
734
735
736
737
// apcore-cli — JSON Schema $ref inliner.
// Protocol spec: FE-08 (resolve_refs)

use serde_json::{Map, Value};
use std::collections::HashSet;
use thiserror::Error;

/// Maximum recursion depth for $ref resolution.
pub const MAX_REF_DEPTH: usize = 32;

// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------

/// Errors produced during `$ref` resolution.
#[derive(Debug, Error)]
pub enum RefResolverError {
    /// A `$ref` target could not be found in the schema's `$defs`.
    #[error("unresolvable $ref '{reference}' in module '{module_id}' (exit 45)")]
    Unresolvable {
        reference: String,
        module_id: String,
    },

    /// A circular reference chain was detected (exit 48).
    #[error("circular $ref detected in module '{module_id}' (exit 48)")]
    Circular { module_id: String },

    /// The maximum recursion depth was exceeded.
    #[error("$ref resolution exceeded max depth {max_depth} in module '{module_id}'")]
    MaxDepthExceeded { max_depth: usize, module_id: String },
}

// ---------------------------------------------------------------------------
// resolve_refs
// ---------------------------------------------------------------------------

/// Inline all `$ref` pointers in a JSON Schema value.
///
/// Resolves `$ref` values by looking them up in `schema["$defs"]` and
/// substituting the referenced schema in-place. Handles nested schemas
/// recursively up to `max_depth`.
///
/// # Arguments
/// * `schema`    — JSON Schema value (deep-copy is used internally)
/// * `max_depth` — maximum recursion depth before raising `MaxDepthExceeded`
/// * `module_id` — module identifier for error messages
///
/// # Errors
/// * `RefResolverError::Unresolvable` — unknown `$ref` target (exit 45)
/// * `RefResolverError::Circular`     — circular reference (exit 48)
/// * `RefResolverError::MaxDepthExceeded` — depth limit reached
pub fn resolve_refs(
    schema: &Value,
    max_depth: usize,
    module_id: &str,
) -> Result<Value, RefResolverError> {
    // Deep-copy; do not modify the caller's value.
    let copy = schema.clone();

    // Extract $defs / definitions ($defs takes precedence).
    let defs: Map<String, Value> = copy
        .get("$defs")
        .or_else(|| copy.get("definitions"))
        .and_then(|v| v.as_object())
        .cloned()
        .unwrap_or_default();

    let mut visiting: HashSet<String> = HashSet::new();
    let resolved = resolve_node(copy, &defs, 0, max_depth, &mut visiting, module_id)?;

    // Strip definition keys from result.
    let mut result = resolved;
    if let Some(obj) = result.as_object_mut() {
        obj.remove("$defs");
        obj.remove("definitions");
    }
    Ok(result)
}

// ---------------------------------------------------------------------------
// Composition helpers
// ---------------------------------------------------------------------------

/// Merge all branches for allOf: union properties (later wins on conflict),
/// concatenate required arrays. Dedups required first-seen-wins for cross-SDK
/// parity with TS (`new Set`) and Python (explicit seen-set). Audit
/// D9-NEW-002 (2026-05-08).
fn merge_allof(branches: Vec<Value>) -> Value {
    let mut merged_props = Map::new();
    let mut merged_required: Vec<Value> = Vec::new();

    for branch in branches {
        if let Some(props) = branch.get("properties").and_then(|v| v.as_object()) {
            for (k, v) in props {
                merged_props.insert(k.clone(), v.clone());
            }
        }
        if let Some(req) = branch.get("required").and_then(|v| v.as_array()) {
            for item in req {
                if !merged_required.contains(item) {
                    merged_required.push(item.clone());
                }
            }
        }
    }

    let mut result = Map::new();
    result.insert("properties".to_string(), Value::Object(merged_props));
    result.insert("required".to_string(), Value::Array(merged_required));
    Value::Object(result)
}

/// Compute the intersection of required field sets across branches.
fn intersect_required_sets(sets: Vec<HashSet<String>>) -> Vec<Value> {
    if sets.is_empty() {
        return Vec::new();
    }
    let mut iter = sets.into_iter();
    let first = iter.next().unwrap();
    iter.fold(first, |acc, set| acc.intersection(&set).cloned().collect())
        .into_iter()
        .map(Value::String)
        .collect()
}

/// Merge all branches for anyOf/oneOf: union properties, required = intersection.
fn merge_anyof(branches: Vec<Value>) -> Value {
    let mut merged_props = Map::new();
    let mut all_required_sets: Vec<HashSet<String>> = Vec::new();

    for branch in branches {
        if let Some(props) = branch.get("properties").and_then(|v| v.as_object()) {
            for (k, v) in props {
                merged_props.insert(k.clone(), v.clone());
            }
        }
        let set: HashSet<String> = branch
            .get("required")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(str::to_string))
                    .collect()
            })
            .unwrap_or_default();
        all_required_sets.push(set);
    }

    let intersection = intersect_required_sets(all_required_sets);

    let mut result = Map::new();
    result.insert("properties".to_string(), Value::Object(merged_props));
    result.insert("required".to_string(), Value::Array(intersection));
    Value::Object(result)
}

// ---------------------------------------------------------------------------
// resolve_node (private helper)
// ---------------------------------------------------------------------------

fn resolve_node(
    node: Value,
    defs: &Map<String, Value>,
    depth: usize,
    max_depth: usize,
    visiting: &mut HashSet<String>,
    module_id: &str,
) -> Result<Value, RefResolverError> {
    let obj = match node {
        Value::Object(map) => map,
        other => return Ok(other),
    };

    // Handle $ref substitution.
    if let Some(ref_val) = obj.get("$ref") {
        let ref_path = ref_val.as_str().unwrap_or("").to_string();

        if depth >= max_depth {
            return Err(RefResolverError::MaxDepthExceeded {
                max_depth,
                module_id: module_id.to_string(),
            });
        }

        if visiting.contains(&ref_path) {
            return Err(RefResolverError::Circular {
                module_id: module_id.to_string(),
            });
        }

        // Extract key: "#/$defs/Address" → "Address"
        let key = ref_path.split('/').next_back().unwrap_or("").to_string();

        let def = defs
            .get(&key)
            .cloned()
            .ok_or_else(|| RefResolverError::Unresolvable {
                reference: ref_path.clone(),
                module_id: module_id.to_string(),
            })?;

        visiting.insert(ref_path.clone());
        let result = resolve_node(def, defs, depth + 1, max_depth, visiting, module_id)?;
        // Keep ref_path in visiting for the duration of this chain to detect cycles.
        // It remains in visiting intentionally — siblings go through a fresh chain
        // because we only remove entries when unwinding past the insertion point.
        // However, for sibling $refs (two different properties referencing the same def),
        // we must remove the entry after resolving so they don't block each other.
        visiting.remove(&ref_path);
        return Ok(result);
    }

    // Handle allOf: merge properties (later wins), concatenate required.
    if obj.contains_key("allOf") {
        let sub_schemas = obj
            .get("allOf")
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default();

        // Resolve each branch first (handles nested $refs).
        let mut resolved_branches = Vec::with_capacity(sub_schemas.len());
        for sub in sub_schemas {
            let resolved_sub = resolve_node(sub, defs, depth + 1, max_depth, visiting, module_id)?;
            resolved_branches.push(resolved_sub);
        }

        let merged = merge_allof(resolved_branches);
        let merged_map = match merged {
            Value::Object(m) => m,
            _ => Map::new(),
        };

        // Carry over non-composition keys from the parent node.
        let mut result_map = merged_map;

        // Seed parent node's own `properties`/`required` into the merged result
        // AFTER branch merging — parent properties that are NOT already present
        // from any branch are inserted here. This matches Python behaviour where
        // `{properties:{x:...}, allOf:[{properties:{y:...}}]}` preserves both
        // x and y (branches win on conflict; parent fills gaps).
        if let Some(parent_props) = obj.get("properties").and_then(|v| v.as_object()) {
            if let Some(Value::Object(merged_props)) = result_map.get_mut("properties") {
                for (k, v) in parent_props {
                    merged_props.entry(k.clone()).or_insert_with(|| v.clone());
                }
            }
        }
        if let Some(parent_req) = obj.get("required").and_then(|v| v.as_array()) {
            if let Some(Value::Array(merged_req)) = result_map.get_mut("required") {
                for item in parent_req {
                    if !merged_req.contains(item) {
                        merged_req.push(item.clone());
                    }
                }
            }
        }

        for (k, v) in &obj {
            if k != "allOf" && !result_map.contains_key(k) {
                result_map.insert(k.clone(), v.clone());
            }
        }
        return Ok(Value::Object(result_map));
    }

    // Handle anyOf / oneOf (same merge logic, intersection of required).
    for keyword in &["anyOf", "oneOf"] {
        if obj.contains_key(*keyword) {
            let sub_schemas = obj
                .get(*keyword)
                .and_then(|v| v.as_array())
                .cloned()
                .unwrap_or_default();

            let mut resolved_branches = Vec::with_capacity(sub_schemas.len());
            for sub in sub_schemas {
                let resolved_sub =
                    resolve_node(sub, defs, depth + 1, max_depth, visiting, module_id)?;
                resolved_branches.push(resolved_sub);
            }

            let merged = merge_anyof(resolved_branches);
            let merged_map = match merged {
                Value::Object(m) => m,
                _ => Map::new(),
            };

            let mut result_map = merged_map;

            // Seed parent node's own properties so sibling fields under a
            // parent that mixes top-level `properties` with `anyOf`/`oneOf`
            // are preserved (parity with `allOf` above and Python
            // ref_resolver.py:100-101). Audit D11-NEW-001 (2026-05-08).
            if let Some(parent_props) = obj.get("properties").and_then(|v| v.as_object()) {
                if let Some(Value::Object(merged_props)) = result_map.get_mut("properties") {
                    for (k, v) in parent_props {
                        merged_props.entry(k.clone()).or_insert_with(|| v.clone());
                    }
                }
            }

            // Merge parent's sibling `required` with the branch intersection,
            // deduplicating and preserving sibling-first order. Per JSON
            // Schema semantics, a parent's `required` applies in addition
            // to the anyOf/oneOf branch intersection. Parity with Python
            // ref_resolver.py:114-118. Audit D11-NEW-001 (2026-05-08).
            if let Some(parent_req) = obj.get("required").and_then(|v| v.as_array()) {
                if let Some(Value::Array(merged_req)) = result_map.get_mut("required") {
                    // Build sibling-first deduplicated list.
                    let mut combined: Vec<Value> = Vec::new();
                    let mut seen: HashSet<String> = HashSet::new();
                    for item in parent_req.iter().chain(merged_req.iter()) {
                        if let Some(s) = item.as_str() {
                            if seen.insert(s.to_string()) {
                                combined.push(item.clone());
                            }
                        }
                    }
                    *merged_req = combined;
                }
            }

            for (k, v) in &obj {
                if k != *keyword && !result_map.contains_key(k) {
                    result_map.insert(k.clone(), v.clone());
                }
            }
            return Ok(Value::Object(result_map));
        }
    }

    // Recursively resolve all values in the object map.
    let mut resolved_map = Map::with_capacity(obj.len());
    for (k, v) in obj {
        let resolved_v = resolve_node(v, defs, depth, max_depth, visiting, module_id)?;
        resolved_map.insert(k, resolved_v);
    }

    Ok(Value::Object(resolved_map))
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_resolve_refs_no_refs_unchanged() {
        // A schema without any $ref must be returned unchanged.
        let schema = json!({
            "type": "object",
            "properties": {
                "name": {"type": "string"}
            }
        });
        let result = resolve_refs(&schema, 32, "test.module");
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert_eq!(resolved["properties"]["name"]["type"], "string");
    }

    #[test]
    fn test_resolve_refs_simple_ref() {
        // A single $ref must be inlined from $defs.
        let schema = json!({
            "$defs": {
                "MyString": {"type": "string", "description": "A name"}
            },
            "type": "object",
            "properties": {
                "name": {"$ref": "#/$defs/MyString"}
            }
        });
        let result = resolve_refs(&schema, 32, "test.module");
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert_eq!(resolved["properties"]["name"]["type"], "string");
        assert_eq!(resolved["properties"]["name"]["description"], "A name");
        // $defs must be stripped from result.
        assert!(resolved.get("$defs").is_none());
    }

    #[test]
    fn test_resolve_refs_definitions_key_also_supported() {
        // Some schemas use "definitions" instead of "$defs".
        let schema = json!({
            "definitions": {
                "Addr": {"type": "string"}
            },
            "properties": {
                "city": {"$ref": "#/definitions/Addr"}
            }
        });
        let result = resolve_refs(&schema, 32, "test.module");
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert_eq!(resolved["properties"]["city"]["type"], "string");
        assert!(resolved.get("definitions").is_none());
    }

    #[test]
    fn test_resolve_refs_unresolvable_returns_error() {
        // An unknown $ref must yield RefResolverError::Unresolvable.
        let schema = json!({
            "type": "object",
            "properties": {
                "x": {"$ref": "#/$defs/DoesNotExist"}
            }
        });
        let result = resolve_refs(&schema, 32, "test.module");
        assert!(
            matches!(result, Err(RefResolverError::Unresolvable { .. })),
            "expected Unresolvable, got: {result:?}"
        );
    }

    #[test]
    fn test_resolve_refs_circular_returns_error() {
        // A circular $ref chain must yield RefResolverError::Circular or MaxDepthExceeded.
        let schema = json!({
            "$defs": {
                "A": {"$ref": "#/$defs/B"},
                "B": {"$ref": "#/$defs/A"}
            },
            "properties": {
                "x": {"$ref": "#/$defs/A"}
            }
        });
        let result = resolve_refs(&schema, 32, "test.module");
        assert!(
            matches!(
                result,
                Err(RefResolverError::Circular { .. })
                    | Err(RefResolverError::MaxDepthExceeded { .. })
            ),
            "expected Circular or MaxDepthExceeded, got: {result:?}"
        );
    }

    #[test]
    fn test_resolve_refs_max_depth_exceeded() {
        // max_depth=0 means the first $ref hit immediately fails.
        let schema = json!({
            "$defs": {
                "Inner": {"type": "string"}
            },
            "properties": {
                "x": {"$ref": "#/$defs/Inner"}
            }
        });
        let result = resolve_refs(&schema, 0, "test.module");
        assert!(
            matches!(result, Err(RefResolverError::MaxDepthExceeded { .. })),
            "expected MaxDepthExceeded, got: {result:?}"
        );
    }

    #[test]
    fn test_resolve_refs_nested_defs() {
        // $refs inside nested object properties must all be resolved.
        let schema = json!({
            "$defs": {
                "City": {"type": "string"}
            },
            "properties": {
                "address": {
                    "type": "object",
                    "properties": {
                        "city": {"$ref": "#/$defs/City"}
                    }
                }
            }
        });
        let result = resolve_refs(&schema, 32, "test.module");
        assert!(result.is_ok());
        let resolved = result.unwrap();
        assert_eq!(
            resolved["properties"]["address"]["properties"]["city"]["type"],
            "string"
        );
    }

    #[test]
    fn test_resolve_refs_does_not_mutate_input() {
        // The original schema must not be modified.
        let schema = json!({
            "$defs": {"T": {"type": "integer"}},
            "properties": {"x": {"$ref": "#/$defs/T"}}
        });
        let _ = resolve_refs(&schema, 32, "test.module");
        // Input schema still has $ref (not mutated).
        assert_eq!(schema["properties"]["x"]["$ref"], "#/$defs/T");
    }

    #[test]
    fn test_resolve_refs_sibling_refs_same_def() {
        // Two different properties referencing the same $def must both resolve correctly.
        let schema = json!({
            "$defs": {
                "Str": {"type": "string"}
            },
            "properties": {
                "a": {"$ref": "#/$defs/Str"},
                "b": {"$ref": "#/$defs/Str"}
            }
        });
        let result = resolve_refs(&schema, 32, "test.module");
        assert!(result.is_ok(), "sibling refs failed: {result:?}");
        let resolved = result.unwrap();
        assert_eq!(resolved["properties"]["a"]["type"], "string");
        assert_eq!(resolved["properties"]["b"]["type"], "string");
    }

    // --- Schema composition tests ---

    #[test]
    fn test_allof_merges_properties() {
        let schema = json!({
            "allOf": [
                {
                    "properties": {"a": {"type": "string"}},
                    "required": ["a"]
                },
                {
                    "properties": {"b": {"type": "integer"}},
                    "required": ["b"]
                }
            ]
        });
        let result = resolve_refs(&schema, 32, "mod").unwrap();
        assert_eq!(result["properties"]["a"]["type"], "string");
        assert_eq!(result["properties"]["b"]["type"], "integer");
        let required: Vec<&str> = result["required"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str())
            .collect();
        assert!(required.contains(&"a"));
        assert!(required.contains(&"b"));
    }

    #[test]
    fn test_allof_later_schema_wins_on_conflict() {
        let schema = json!({
            "allOf": [
                {"properties": {"x": {"type": "string"}}},
                {"properties": {"x": {"type": "integer"}}}
            ]
        });
        let result = resolve_refs(&schema, 32, "mod").unwrap();
        // Later sub-schema wins: x must be integer.
        assert_eq!(result["properties"]["x"]["type"], "integer");
    }

    #[test]
    fn test_allof_copies_non_composition_keys() {
        let schema = json!({
            "description": "My type",
            "allOf": [
                {"properties": {"a": {"type": "string"}}}
            ]
        });
        let result = resolve_refs(&schema, 32, "mod").unwrap();
        // "description" must survive in the merged result.
        assert_eq!(result["description"], "My type");
    }

    #[test]
    fn test_anyof_unions_properties() {
        let schema = json!({
            "anyOf": [
                {"properties": {"a": {"type": "string"}}, "required": ["a"]},
                {"properties": {"b": {"type": "integer"}}, "required": ["b"]}
            ]
        });
        let result = resolve_refs(&schema, 32, "mod").unwrap();
        // Both properties must appear.
        assert!(result["properties"].get("a").is_some());
        assert!(result["properties"].get("b").is_some());
    }

    #[test]
    fn test_anyof_required_is_intersection() {
        let schema = json!({
            "anyOf": [
                {"properties": {"a": {"type": "string"}, "b": {"type": "string"}}, "required": ["a", "b"]},
                {"properties": {"a": {"type": "string"}, "c": {"type": "string"}}, "required": ["a", "c"]}
            ]
        });
        let result = resolve_refs(&schema, 32, "mod").unwrap();
        let required: Vec<&str> = result["required"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str())
            .collect();
        // Only "a" appears in both branches — it is the intersection.
        assert!(
            required.contains(&"a"),
            "a must be required (in both branches)"
        );
        assert!(
            !required.contains(&"b"),
            "b must not be required (only in first branch)"
        );
        assert!(
            !required.contains(&"c"),
            "c must not be required (only in second branch)"
        );
    }

    #[test]
    fn test_anyof_empty_required_when_no_overlap() {
        let schema = json!({
            "anyOf": [
                {"properties": {"a": {"type": "string"}}, "required": ["a"]},
                {"properties": {"b": {"type": "integer"}}, "required": ["b"]}
            ]
        });
        let result = resolve_refs(&schema, 32, "mod").unwrap();
        let required = result["required"].as_array().unwrap();
        assert!(
            required.is_empty(),
            "no fields are required in both branches"
        );
    }

    #[test]
    fn test_oneof_behaves_like_anyof() {
        let schema = json!({
            "oneOf": [
                {"properties": {"x": {"type": "string"}}, "required": ["x"]},
                {"properties": {"y": {"type": "integer"}}, "required": ["y"]}
            ]
        });
        let result = resolve_refs(&schema, 32, "mod").unwrap();
        assert!(result["properties"].get("x").is_some());
        assert!(result["properties"].get("y").is_some());
        assert!(result["required"].as_array().unwrap().is_empty());
    }

    #[test]
    fn test_allof_with_nested_ref() {
        // allOf sub-schema that itself contains a $ref.
        let schema = json!({
            "$defs": {
                "Base": {"properties": {"id": {"type": "integer"}}, "required": ["id"]}
            },
            "allOf": [
                {"$ref": "#/$defs/Base"},
                {"properties": {"name": {"type": "string"}}}
            ]
        });
        let result = resolve_refs(&schema, 32, "mod").unwrap();
        assert_eq!(result["properties"]["id"]["type"], "integer");
        assert_eq!(result["properties"]["name"]["type"], "string");
        let required: Vec<&str> = result["required"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str())
            .collect();
        assert!(required.contains(&"id"));
    }

    /// Audit D11-NEW-001 (2026-05-08): a parent's `required` applies in
    /// addition to anyOf/oneOf branch intersection — sibling required must
    /// not be silently dropped. Cross-SDK parity with Python ref_resolver.py.
    #[test]
    fn test_anyof_preserves_parent_sibling_required() {
        let schema = json!({
            "type": "object",
            "required": ["x"],
            "anyOf": [
                {"properties": {"a": {"type": "string"}}, "required": ["a"]},
                {"properties": {"a": {"type": "integer"}}, "required": ["a"]},
            ]
        });
        let result = resolve_refs(&schema, 32, "mod").unwrap();
        let required: Vec<&str> = result["required"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str())
            .collect();
        // Sibling-first ordering: parent "x" before branch-intersection "a".
        assert_eq!(required, vec!["x", "a"]);
    }

    #[test]
    fn test_oneof_preserves_parent_sibling_required() {
        let schema = json!({
            "type": "object",
            "required": ["host", "port"],
            "oneOf": [
                {"properties": {"mode": {"const": "http"}}, "required": ["scheme"]},
                {"properties": {"mode": {"const": "tcp"}}, "required": ["scheme"]},
            ]
        });
        let result = resolve_refs(&schema, 32, "mod").unwrap();
        let required: Vec<&str> = result["required"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str())
            .collect();
        assert_eq!(required, vec!["host", "port", "scheme"]);
    }

    #[test]
    fn test_anyof_dedupes_overlap_between_sibling_and_branch_intersection() {
        let schema = json!({
            "type": "object",
            "required": ["a"],
            "anyOf": [
                {"required": ["a", "b"]},
                {"required": ["a", "c"]},
            ]
        });
        let result = resolve_refs(&schema, 32, "mod").unwrap();
        let required: Vec<&str> = result["required"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str())
            .collect();
        // Sibling "a" present once; branch intersection ["a"] dedup-skipped.
        assert_eq!(required, vec!["a"]);
    }
}