jsoncompat 0.4.2

JSON Schema and OpenAPI Compatibility Checker
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
//! Backward-compatibility checks for evolving JSON Schema documents and
//! OpenAPI 3.1 contracts.
//!
//! Build input documents with [`SchemaDocument::from_json`], then call
//! [`check_compat`] with a [`Role`]. This crate intentionally exposes only the
//! document-level JSON Schema compatibility API; lower-level resolved IR types
//! live in `json_schema_ast`. OpenAPI validation and lowering live in the
//! sibling `jsoncompat_openapi` crate; this crate layers compatibility reports
//! over those lowered request and response schemas.

// Re-export the document type needed by `check_compat` so application callers
// do not need a second direct dependency just to construct inputs.
use json_pointer::JsonPointer;
use json_schema_ast::{
    NodeId, SCHEMA_ARRAY_CHILD_KEYWORDS, SCHEMA_MAP_CHILD_KEYWORDS, SINGLE_SCHEMA_CHILD_KEYWORDS,
    SchemaNode, SchemaNodeKind,
};
pub use json_schema_ast::{SchemaBuildError, SchemaDocument};
use serde_json::{Map, Value};
use std::collections::HashSet;

mod json_pointer;
mod openapi_compat;
mod stamp;
mod subset;

pub use jsoncompat_openapi::{OpenApiDocument, OpenApiError, OpenApiLoweringError};
pub use openapi_compat::{
    OpenApiCompatibilityError, OpenApiCompatibilityIssue, OpenApiCompatibilityReport,
    OpenApiCompatibilitySurface, check_openapi_compat, validate_openapi_compatibility_input,
};
pub use stamp::{
    ENVELOPE_DATA_KEY, ENVELOPE_VERSION_KEY, STAMP_MANIFEST_VERSION, SchemaHistory,
    SchemaVersionEntry, StampBundle, StampError, StampManifest, StampResult, StampStatus,
    canonical_schema_hash, stamp_schema, write_stamp_manifest_atomic,
};
use subset::{
    explain_subschema_failure, explain_subschema_failure_emitted_values, is_subschema_of,
    is_subschema_of_emitted_values,
};

/// The role under which a compatibility check is performed.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Role {
    /// Evolving the *producer* (serializer).  A change is safe if every value
    /// produced by the _new_ schema is still accepted by the _old_ one.
    Serializer,
    /// Evolving the *consumer* (deserializer).  A change is safe if every value
    /// the old serializer could emit is still valid under the _new_ schema.
    Deserializer,
    /// We need to maintain full equivalence in both directions.
    Both,
}

/// Compatibility-check failures that are distinct from a proven incompatibility.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CompatibilityError {
    /// The old or new schema document failed canonicalization or resolution.
    #[error(transparent)]
    Schema(#[from] SchemaBuildError),
    /// Reference-scope keywords change how the document resolves names. They
    /// stay hard errors until the resolver models those scopes precisely.
    #[error(
        "JSON Schema compatibility checks do not support keyword '{keyword}' at '{pointer}' yet"
    )]
    UnsupportedCompatibilityKeyword { pointer: String, keyword: String },
    /// Number-schema bounds beyond the adjacent-integer-safe `f64` range can
    /// collapse distinct JSON integers in the resolved IR, so subset proofs
    /// must fail before comparison rather than overclaim.
    #[error(
        "JSON Schema compatibility checks do not support number bound '{keyword}' at '{pointer}' outside the exact f64 integer range [-9007199254740991, 9007199254740991] yet"
    )]
    UnsupportedCompatibilityNumberBound { pointer: String, keyword: String },
    /// Compatibility checks do not approximate fractional `number.multipleOf`
    /// inclusion with floating-point arithmetic.
    #[error("non-integral number multipleOf constraints are not supported by compatibility checks")]
    UnsupportedNonIntegralNumberMultipleOf,
}

/// Compatibility diagnostics that do not prevent a modeled comparison.
///
/// These warnings mean a schema uses valid JSON Schema syntax whose semantics
/// are not represented by the subset checker yet. Callers that need a complete
/// contract verdict should surface the warning alongside the modeled result.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum CompatibilityWarning {
    #[error(
        "JSON Schema compatibility checks do not model keyword '{keyword}' at '{pointer}'; comparison ignores that keyword"
    )]
    UnsupportedKeyword { pointer: String, keyword: String },
}

/// Return whether `new` is backward-compatible with `old` under `role`.
///
/// The checker is a structural subset proof over the documents' resolved
/// schema graphs:
///
/// * [`Role::Serializer`] checks `new ⊆ old`: every value produced under the
///   new schema must still be accepted by clients using the old schema.
/// * [`Role::Deserializer`] checks whether every value the old serializer
///   could emit is still accepted by the new schema.
/// * [`Role::Both`] requires both directions.
///
/// For [`Role::Deserializer`], object schemas are interpreted under the
/// assumption that serializers do not emit undeclared properties solely because
/// `additionalProperties` would permit them.
///
/// A return value of `Ok(false)` is a proven or conservative incompatibility.
/// A return value of `Err(_)` means the checker cannot soundly run on the input
/// schema or feature set.
pub fn check_compat(
    old: &SchemaDocument,
    new: &SchemaDocument,
    role: Role,
) -> Result<bool, CompatibilityError> {
    let old = compatibility_input(old)?.root;
    let new = compatibility_input(new)?.root;

    match role {
        Role::Serializer => Ok(is_subschema_of(new, old)),
        Role::Deserializer => Ok(is_subschema_of_emitted_values(old, new)),
        Role::Both => Ok(is_subschema_of(new, old) && is_subschema_of_emitted_values(old, new)),
    }
}

/// Return a best-effort static explanation for the first incompatibility under
/// `role`, or `Ok(None)` when the checker finds no incompatibility to explain.
///
/// This diagnostic path is intentionally narrower than [`check_compat`]: it
/// preserves the sound compatibility verdict while surfacing the most useful
/// structural reason the checker can identify.
pub fn explain_compat_failure(
    old: &SchemaDocument,
    new: &SchemaDocument,
    role: Role,
) -> Result<Option<String>, CompatibilityError> {
    let old = compatibility_input(old)?.root;
    let new = compatibility_input(new)?.root;

    let explanation = match role {
        Role::Serializer => {
            explain_subschema_failure(new, old).map(|explanation| explanation.render("new", "old"))
        }
        Role::Deserializer => explain_subschema_failure_emitted_values(old, new)
            .map(|explanation| explanation.render("old", "new")),
        Role::Both => explain_subschema_failure(new, old)
            .map(|explanation| explanation.render("new", "old"))
            .or_else(|| {
                explain_subschema_failure_emitted_values(old, new)
                    .map(|explanation| explanation.render("old", "new"))
            }),
    };
    Ok(explanation)
}

/// Return whether this schema can participate in compatibility checks.
///
/// `SchemaDocument::from_json` accepts the full document-level schema surface
/// modeled by the schema frontend. Compatibility still rejects inputs that
/// would make the subset check unsound; valid-but-unmodeled keywords are
/// reported separately through [`compatibility_warnings`].
pub fn validate_compatibility_input(schema: &SchemaDocument) -> Result<(), CompatibilityError> {
    compatibility_input(schema).map(|_| ())
}

/// Return non-fatal compatibility diagnostics for one schema document.
pub fn compatibility_warnings(
    schema: &SchemaDocument,
) -> Result<Vec<CompatibilityWarning>, CompatibilityError> {
    compatibility_input(schema).map(|input| input.warnings)
}

struct CompatibilityInput<'a> {
    root: &'a SchemaNode,
    warnings: Vec<CompatibilityWarning>,
}

fn compatibility_input(
    schema: &SchemaDocument,
) -> Result<CompatibilityInput<'_>, CompatibilityError> {
    let warnings = collect_source_keyword_warnings(schema)?;
    match schema.root() {
        Ok(root) => {
            schema.validate_source_schema()?;
            reject_unsupported_reference_keywords_after_source_validation(schema)?;
            reject_unsupported_compatibility_features(root)?;
            Ok(CompatibilityInput { root, warnings })
        }
        Err(source @ SchemaBuildError::UnsupportedReference { .. }) => {
            validate_source_schema_ignoring_non_local_refs(schema)?;
            reject_unsupported_reference_keywords_after_source_validation(schema)?;
            Err(source.into())
        }
        Err(source) => Err(source.into()),
    }
}

const UNSUPPORTED_COMPATIBILITY_KEYWORDS: &[&str] = &[
    "additionalItems",
    "contentEncoding",
    "contentMediaType",
    "contentSchema",
    "dependencies",
    "dependentSchemas",
    "unevaluatedItems",
    "unevaluatedProperties",
];
const UNSUPPORTED_COMPATIBILITY_REFERENCE_KEYWORDS: &[&str] =
    &["$id", "$anchor", "$dynamicRef", "$dynamicAnchor"];
const MAX_EXACT_F64_INTEGER: f64 = 9_007_199_254_740_991.0;

fn collect_source_keyword_warnings(
    schema: &SchemaDocument,
) -> Result<Vec<CompatibilityWarning>, CompatibilityError> {
    reject_unsafe_number_bounds_in_schema_value(
        schema.source_schema_json(),
        &mut JsonPointer::root(),
    )?;
    collect_unsupported_keyword_family(
        schema.source_schema_json(),
        UNSUPPORTED_COMPATIBILITY_KEYWORDS,
    )
}

fn reject_unsupported_reference_keywords_after_source_validation(
    schema: &SchemaDocument,
) -> Result<(), CompatibilityError> {
    reject_unsupported_keyword_family(
        schema.source_schema_json(),
        UNSUPPORTED_COMPATIBILITY_REFERENCE_KEYWORDS,
    )
}

fn reject_unsupported_keyword_family(
    schema: &Value,
    keywords: &[&str],
) -> Result<(), CompatibilityError> {
    if let Some(CompatibilityWarning::UnsupportedKeyword { pointer, keyword }) =
        collect_unsupported_keyword_family(schema, keywords)?
            .into_iter()
            .next()
    {
        return Err(CompatibilityError::UnsupportedCompatibilityKeyword { pointer, keyword });
    }

    Ok(())
}

fn collect_unsupported_keyword_family(
    schema: &Value,
    keywords: &[&str],
) -> Result<Vec<CompatibilityWarning>, CompatibilityError> {
    let mut warnings = Vec::new();
    walk_source_schema_objects(schema, &mut JsonPointer::root(), &mut |object, pointer| {
        collect_unsupported_keywords_in_schema_object(object, pointer, keywords, &mut warnings);
        Ok(())
    })?;
    Ok(warnings)
}

fn validate_source_schema_ignoring_non_local_refs(
    schema: &SchemaDocument,
) -> Result<(), CompatibilityError> {
    let stripped = strip_non_local_schema_refs(schema.source_schema_json());
    let stripped = SchemaDocument::from_json(&stripped)?;
    stripped.validate_source_schema()?;
    Ok(())
}

fn strip_non_local_schema_refs(schema: &Value) -> Value {
    match schema {
        Value::Object(object) => {
            let mut stripped = Map::new();
            for (key, value) in object {
                let stripped_value = match key.as_str() {
                    "$ref"
                        if value
                            .as_str()
                            .is_some_and(|reference| !reference.starts_with("#/")) =>
                    {
                        None
                    }
                    key if SINGLE_SCHEMA_CHILD_KEYWORDS.contains(&key) => {
                        Some(strip_non_local_schema_refs(value))
                    }
                    key if SCHEMA_MAP_CHILD_KEYWORDS.contains(&key) => {
                        Some(strip_non_local_schema_ref_map(value))
                    }
                    key if SCHEMA_ARRAY_CHILD_KEYWORDS.contains(&key) => {
                        Some(strip_non_local_schema_ref_array(value))
                    }
                    _ => Some(value.clone()),
                };
                if let Some(stripped_value) = stripped_value {
                    stripped.insert(key.clone(), stripped_value);
                }
            }
            Value::Object(stripped)
        }
        _ => schema.clone(),
    }
}

fn strip_non_local_schema_ref_map(value: &Value) -> Value {
    match value {
        Value::Object(object) => Value::Object(
            object
                .iter()
                .map(|(name, schema)| (name.clone(), strip_non_local_schema_refs(schema)))
                .collect(),
        ),
        _ => value.clone(),
    }
}

fn strip_non_local_schema_ref_array(value: &Value) -> Value {
    match value {
        Value::Array(items) => {
            Value::Array(items.iter().map(strip_non_local_schema_refs).collect())
        }
        _ => value.clone(),
    }
}

fn walk_source_schema_objects(
    schema: &Value,
    pointer: &mut JsonPointer,
    visit: &mut impl FnMut(&Map<String, Value>, &JsonPointer) -> Result<(), CompatibilityError>,
) -> Result<(), CompatibilityError> {
    match schema {
        Value::Bool(_) => Ok(()),
        Value::Object(object) => {
            visit(object, pointer)?;

            for keyword in SINGLE_SCHEMA_CHILD_KEYWORDS {
                if let Some(child) = object.get(keyword) {
                    pointer.push(keyword);
                    walk_source_schema_objects(child, pointer, visit)?;
                    pointer.pop();
                }
            }

            for keyword in SCHEMA_MAP_CHILD_KEYWORDS {
                if let Some(children) = object.get(keyword).and_then(Value::as_object) {
                    pointer.push(keyword);
                    for (name, child) in children {
                        pointer.push(name);
                        walk_source_schema_objects(child, pointer, visit)?;
                        pointer.pop();
                    }
                    pointer.pop();
                }
            }

            for keyword in SCHEMA_ARRAY_CHILD_KEYWORDS {
                if let Some(children) = object.get(keyword).and_then(Value::as_array) {
                    pointer.push(keyword);
                    for (index, child) in children.iter().enumerate() {
                        pointer.push(index.to_string());
                        walk_source_schema_objects(child, pointer, visit)?;
                        pointer.pop();
                    }
                    pointer.pop();
                }
            }

            Ok(())
        }
        _ => Ok(()),
    }
}

fn collect_unsupported_keywords_in_schema_object(
    object: &Map<String, Value>,
    pointer: &JsonPointer,
    keywords: &[&str],
    warnings: &mut Vec<CompatibilityWarning>,
) {
    for keyword in keywords {
        if object.contains_key(*keyword) {
            let mut keyword_pointer = pointer.clone();
            keyword_pointer.push(*keyword);
            warnings.push(CompatibilityWarning::UnsupportedKeyword {
                pointer: keyword_pointer.render(),
                keyword: (*keyword).to_owned(),
            });
        }
    }
}

fn reject_unsafe_number_bounds_in_schema_value(
    schema: &Value,
    pointer: &mut JsonPointer,
) -> Result<(), CompatibilityError> {
    walk_source_schema_objects(
        schema,
        pointer,
        &mut reject_unsafe_number_bounds_in_schema_object,
    )
}

fn reject_unsafe_number_bounds_in_schema_object(
    object: &Map<String, Value>,
    pointer: &JsonPointer,
) -> Result<(), CompatibilityError> {
    if schema_object_has_integer_only_numeric_domain(object) {
        return Ok(());
    }

    for keyword in ["minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum"] {
        let Some(value) = object.get(keyword) else {
            continue;
        };
        if !number_bound_is_outside_exact_f64_integer_range(value) {
            continue;
        }

        let mut keyword_pointer = pointer.clone();
        keyword_pointer.push(keyword);
        return Err(CompatibilityError::UnsupportedCompatibilityNumberBound {
            pointer: keyword_pointer.render(),
            keyword: keyword.to_owned(),
        });
    }

    Ok(())
}

fn schema_object_has_integer_only_numeric_domain(object: &Map<String, Value>) -> bool {
    match object.get("type") {
        Some(Value::String(schema_type)) => schema_type == "integer",
        Some(Value::Array(schema_types)) => {
            let mut has_integer = false;
            for schema_type in schema_types {
                let Some(schema_type) = schema_type.as_str() else {
                    return false;
                };
                match schema_type {
                    "integer" => has_integer = true,
                    "number" => return false,
                    _ => {}
                }
            }
            has_integer
        }
        _ => false,
    }
}

fn number_bound_is_outside_exact_f64_integer_range(value: &Value) -> bool {
    value
        .as_f64()
        .is_some_and(|value| value.is_finite() && value.abs() > MAX_EXACT_F64_INTEGER)
}

fn reject_unsupported_compatibility_features(
    schema: &SchemaNode,
) -> Result<(), CompatibilityError> {
    reject_unsupported_node(schema, &mut HashSet::new())
}

fn reject_unsupported_node(
    schema: &SchemaNode,
    visited_nodes: &mut HashSet<NodeId>,
) -> Result<(), CompatibilityError> {
    if !visited_nodes.insert(schema.id()) {
        return Ok(());
    }

    match schema.kind() {
        SchemaNodeKind::Number {
            multiple_of: Some(multiple_of),
            ..
        } if !multiple_of.is_integer_valued() => {
            return Err(CompatibilityError::UnsupportedNonIntegralNumberMultipleOf);
        }
        SchemaNodeKind::Object {
            properties,
            pattern_properties,
            additional,
            property_names,
            ..
        } => {
            for property in properties.values() {
                reject_unsupported_node(property, visited_nodes)?;
            }
            for property in pattern_properties.values() {
                reject_unsupported_node(&property.schema, visited_nodes)?;
            }
            reject_unsupported_node(additional, visited_nodes)?;
            reject_unsupported_node(property_names, visited_nodes)?;
        }
        SchemaNodeKind::Array {
            prefix_items,
            items,
            contains,
            ..
        } => {
            for prefix_item in prefix_items {
                reject_unsupported_node(prefix_item, visited_nodes)?;
            }
            reject_unsupported_node(items, visited_nodes)?;
            if let Some(contains) = contains {
                reject_unsupported_node(&contains.schema, visited_nodes)?;
            }
        }
        SchemaNodeKind::AllOf(subschemas)
        | SchemaNodeKind::AnyOf(subschemas)
        | SchemaNodeKind::OneOf(subschemas) => {
            for subschema in subschemas {
                reject_unsupported_node(subschema, visited_nodes)?;
            }
        }
        SchemaNodeKind::Not(subschema) => reject_unsupported_node(subschema, visited_nodes)?,
        SchemaNodeKind::IfThenElse {
            if_schema,
            then_schema,
            else_schema,
        } => {
            reject_unsupported_node(if_schema, visited_nodes)?;
            if let Some(then_schema) = then_schema {
                reject_unsupported_node(then_schema, visited_nodes)?;
            }
            if let Some(else_schema) = else_schema {
                reject_unsupported_node(else_schema, visited_nodes)?;
            }
        }
        _ => {}
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{
        CompatibilityError, CompatibilityWarning, Role, SchemaBuildError, SchemaDocument,
        check_compat, compatibility_warnings,
    };
    use serde_json::json;

    fn schema(raw: serde_json::Value) -> SchemaDocument {
        SchemaDocument::from_json(&raw).expect("schema should parse")
    }

    #[test]
    fn check_compat_rejects_non_integral_number_multiple_of() {
        let old = schema(json!({ "type": "number", "multipleOf": 0.2 }));
        let new = schema(json!({ "type": "number" }));

        let error = check_compat(&old, &new, Role::Serializer)
            .expect_err("non-integral number multipleOf is unsupported");
        assert!(matches!(
            error,
            CompatibilityError::UnsupportedNonIntegralNumberMultipleOf
        ));
    }

    #[test]
    fn check_compat_accepts_integral_number_multiple_of() {
        let old = schema(json!({ "type": "number", "multipleOf": 2 }));
        let new = schema(json!({ "type": "integer", "multipleOf": 4 }));

        assert!(
            check_compat(&old, &new, Role::Serializer)
                .expect("integral number multipleOf remains supported")
        );
    }

    #[test]
    fn check_compat_rejects_number_bounds_beyond_the_exact_f64_integer_range() {
        let old = schema(json!({
            "type": "number",
            "maximum": 9_007_199_254_740_992_i64
        }));
        let new = schema(json!({
            "enum": [9_007_199_254_740_993_i64]
        }));

        assert!(new.is_valid(&json!(9_007_199_254_740_993_i64)).unwrap());
        assert!(!old.is_valid(&json!(9_007_199_254_740_993_i64)).unwrap());

        let error = check_compat(&old, &new, Role::Serializer)
            .expect_err("unsafe number bounds must fail before subset comparison");
        assert!(matches!(
            error,
            CompatibilityError::UnsupportedCompatibilityNumberBound {
                ref pointer,
                ref keyword,
            } if pointer == "#/maximum" && keyword == "maximum"
        ));
    }

    #[test]
    fn check_compat_keeps_large_integer_bounds_supported_exactly() {
        let old = schema(json!({
            "type": "integer",
            "maximum": 9_007_199_254_740_992_i64
        }));
        let new = schema(json!({
            "type": "integer",
            "maximum": 9_007_199_254_740_991_i64
        }));

        assert!(
            check_compat(&old, &new, Role::Serializer).expect("integer-only bounds remain exact")
        );
    }

    #[test]
    fn compatibility_warnings_report_valid_but_unmodeled_schema_keywords_with_precise_pointers() {
        for (raw, pointer, keyword) in [
            (
                json!({
                    "type": "object",
                    "properties": {
                        "payload": {
                            "dependentSchemas": {
                                "kind": { "required": ["detail"] }
                            }
                        }
                    }
                }),
                "#/properties/payload/dependentSchemas",
                "dependentSchemas",
            ),
            (
                json!({
                    "type": "object",
                    "dependencies": {
                        "kind": ["detail"]
                    }
                }),
                "#/dependencies",
                "dependencies",
            ),
            (
                json!({
                    "additionalItems": false
                }),
                "#/additionalItems",
                "additionalItems",
            ),
        ] {
            let old = schema(raw);
            let warnings = compatibility_warnings(&old).expect("warning collection should succeed");

            assert_eq!(
                warnings,
                vec![CompatibilityWarning::UnsupportedKeyword {
                    pointer: pointer.to_owned(),
                    keyword: keyword.to_owned(),
                }]
            );
        }
    }

    #[test]
    fn check_compat_rejects_reference_scope_keywords_with_precise_pointers() {
        for (raw, pointer, keyword) in [
            (
                json!({
                    "$id": "https://example.com/schemas/value.json",
                    "type": "string"
                }),
                "#/$id",
                "$id",
            ),
            (
                json!({
                    "$anchor": "value",
                    "type": "string"
                }),
                "#/$anchor",
                "$anchor",
            ),
            (
                json!({
                    "$dynamicRef": "#",
                    "type": "string"
                }),
                "#/$dynamicRef",
                "$dynamicRef",
            ),
            (
                json!({
                    "$dynamicAnchor": "value",
                    "type": "string"
                }),
                "#/$dynamicAnchor",
                "$dynamicAnchor",
            ),
        ] {
            let old = schema(raw);
            let new = schema(json!({}));

            let error = check_compat(&old, &new, Role::Both)
                .expect_err("reference-scope keywords must remain hard compatibility errors");
            assert!(matches!(
                error,
                CompatibilityError::UnsupportedCompatibilityKeyword {
                    pointer: ref actual_pointer,
                    keyword: ref actual_keyword,
                } if actual_pointer == pointer && actual_keyword == keyword
            ));
        }
    }

    #[test]
    fn check_compat_rejects_reference_scope_keywords_inside_unused_defs() {
        let old = schema(json!({
            "$defs": {
                "Unused": {
                    "$id": "https://example.com/schemas/unused.json",
                    "type": "string"
                }
            },
            "type": "string"
        }));
        let new = schema(json!({ "type": "string" }));

        let error = check_compat(&old, &new, Role::Both)
            .expect_err("unused defs must not hide unsupported reference-scope keywords");
        assert!(matches!(
            error,
            CompatibilityError::UnsupportedCompatibilityKeyword {
                pointer: ref actual_pointer,
                keyword: ref actual_keyword,
            } if actual_pointer == "#/$defs/Unused/$id" && actual_keyword == "$id"
        ));
    }

    #[test]
    fn compatibility_warnings_report_valid_but_unmodeled_keywords_inside_unused_defs() {
        let old = schema(json!({
            "$defs": {
                "Unused": {
                    "type": "object",
                    "dependentSchemas": {
                        "kind": { "required": ["detail"] }
                    }
                }
            },
            "type": "string"
        }));
        let warnings =
            compatibility_warnings(&old).expect("warning collection should inspect unused defs");

        assert_eq!(
            warnings,
            vec![CompatibilityWarning::UnsupportedKeyword {
                pointer: "#/$defs/Unused/dependentSchemas".to_owned(),
                keyword: "dependentSchemas".to_owned(),
            }]
        );
    }

    #[test]
    fn check_compat_accepts_identical_schemas_with_unmodeled_keyword_warnings() {
        let old = schema(json!({
            "type": "object",
            "dependentSchemas": {
                "kind": { "required": ["detail"] }
            }
        }));
        let new = schema(json!({
            "type": "object",
            "dependentSchemas": {
                "kind": { "required": ["detail"] }
            }
        }));

        assert!(
            check_compat(&old, &new, Role::Both)
                .expect("unmodeled keywords should warn instead of failing the modeled verdict")
        );
    }

    #[test]
    fn check_compat_rejects_backend_invalid_ref_bearing_schemas_before_comparison() {
        let old = schema(json!({
            "$defs": {
                "Value": { "type": "string" }
            },
            "$ref": "#/$defs/Value",
            "deprecated": "eventually"
        }));
        let new = schema(json!({ "type": "string" }));

        let error = check_compat(&old, &new, Role::Serializer)
            .expect_err("raw-schema backend validation must still run after local refs resolve")
            .to_string();

        assert!(
            error.contains("failed to compile raw schema validator"),
            "{error}"
        );
    }

    #[test]
    fn check_compat_rejects_backend_invalid_identity_ref_bearing_schemas_before_comparison() {
        let old = schema(json!({
            "$id": "https://example.com/schemas/value.json",
            "type": "string",
            "deprecated": "eventually"
        }));
        let new = schema(json!({ "type": "string" }));

        let error = check_compat(&old, &new, Role::Serializer)
            .expect_err("raw-schema backend validation must run before unsupported identity refs")
            .to_string();

        assert!(
            error.contains("failed to compile raw schema validator"),
            "{error}"
        );
    }

    #[test]
    fn check_compat_rejects_backend_invalid_non_local_ref_bearing_schemas_before_comparison() {
        let old = schema(json!({
            "$ref": "https://example.com/schemas/value.json",
            "deprecated": "eventually"
        }));
        let new = schema(json!({ "type": "string" }));

        let error = check_compat(&old, &new, Role::Serializer)
            .expect_err("raw-schema backend validation must inspect siblings of non-local refs")
            .to_string();

        assert!(
            error.contains("failed to compile raw schema validator"),
            "{error}"
        );
    }

    #[test]
    fn check_compat_keeps_non_local_ref_errors_explicit_after_source_validation() {
        let old = schema(json!({
            "$ref": "https://example.com/schemas/value.json"
        }));
        let new = schema(json!({ "type": "string" }));

        let error = check_compat(&old, &new, Role::Serializer)
            .expect_err("non-local refs stay unsupported even after raw validation runs");

        assert!(matches!(
            error,
            CompatibilityError::Schema(SchemaBuildError::UnsupportedReference { ref_path })
                if ref_path == "https://example.com/schemas/value.json"
        ));
    }

    #[test]
    fn check_compat_does_not_treat_const_payload_keys_as_schema_keywords() {
        let old = schema(json!({
            "const": {
                "dependentSchemas": {
                    "kind": { "required": ["detail"] }
                }
            }
        }));
        let new = schema(json!({
            "const": {
                "dependentSchemas": {
                    "kind": { "required": ["detail"] }
                }
            }
        }));

        assert!(
            check_compat(&old, &new, Role::Both)
                .expect("const payload keys are data, not schema keywords")
        );
    }

    #[test]
    fn check_compat_treats_optional_added_property_as_deserializer_compatible() {
        let old = schema(json!({
            "type": "object",
            "properties": {
                "name": { "type": "string" },
                "age": { "type": "integer" }
            },
            "required": ["name", "age"]
        }));
        let new = schema(json!({
            "type": "object",
            "properties": {
                "name": { "type": "string" },
                "age": { "type": "integer" },
                "is_active": { "type": "boolean", "default": true }
            },
            "required": ["name", "age"]
        }));

        assert!(
            check_compat(&old, &new, Role::Deserializer)
                .expect("optional added property should remain deserializer-compatible")
        );
        assert!(
            check_compat(&old, &new, Role::Both)
                .expect("optional added property should remain compatible in both directions")
        );
    }
}