brake 2026.11.3

A brake on breaking API changes. Checks OpenAPI, protobuf and GraphQL contracts for backward compatibility, hermetically.
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
//! `TypeRef` × `TypeRef` → the issues between them.
//!
//! Format-agnostic by construction: this module never learns whether it is
//! holding OpenAPI, protobuf or GraphQL. Every issue carries a JSON pointer
//! relative to the payload root, so a finding can name the field it is about
//! and a suppression can target that field structurally rather than by
//! matching text in a human-readable message.

use std::collections::{BTreeMap, BTreeSet};

use crate::contract::{Constraints, Field, Span, TypeRef, UnmodelledKind};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeDirection {
    Request,
    Response,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum TypeIssue {
    /// The accepted input shrank: existing callers stop being accepted.
    RequestTypeNarrowed {
        pointer: String,
        reason: String,
    },
    RequestFieldAddedRequired {
        pointer: String,
        field: String,
    },
    /// Additive, and therefore only a finding under `strict`.
    RequestFieldAddedOptional {
        pointer: String,
        field: String,
    },
    RequestVariantRemoved {
        pointer: String,
    },

    /// The produced output changed shape: existing readers break.
    ResponseTypeChanged {
        pointer: String,
        reason: String,
    },
    ResponseFieldRemoved {
        pointer: String,
        field: String,
    },
    ResponseFieldOptional {
        pointer: String,
        field: String,
    },
    /// Additive, and therefore only a finding under `strict`.
    ResponseFieldAdded {
        pointer: String,
        field: String,
    },
    ResponseEnumExtended {
        pointer: String,
    },
    ResponseVariantAdded {
        pointer: String,
    },

    /// A field kept its wire number and changed its name. Wire-compatible;
    /// breaks any JSON or generated-code consumer.
    FieldRenamed {
        pointer: String,
        from: String,
        to: String,
    },
    /// A field kept its name and changed its wire number. This is the
    /// protobuf break: the bytes on the wire no longer mean the same thing.
    FieldNumberChanged {
        pointer: String,
        field: String,
        from: i32,
        to: i32,
    },

    /// Something on this path could not be modelled, so the comparison is not
    /// a clean result — it is an unverified one, and says so.
    Partial {
        pointer: String,
        detail: String,
    },
}

/// An issue and the place it is about.
///
/// The span is the nearest enclosing field's, where the ingester supplied one.
/// Without it a finding about `customer_id` underlines the whole response —
/// the right file, the wrong line, and the line is what a reader checks first.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Located {
    pub issue: TypeIssue,
    pub span: Option<Span>,
}

/// Collects issues, stamping each with the field currently being compared.
///
/// Deliberately shaped like `Vec::push` so the thirty-odd call sites below
/// read unchanged: what varies is *where* an issue is, not what it says.
#[derive(Debug, Default)]
struct Issues {
    found: Vec<Located>,
    span: Option<Span>,
}

impl Issues {
    fn push(&mut self, issue: TypeIssue) {
        self.found.push(Located {
            issue,
            span: self.span.clone(),
        });
    }

    /// Run `body` with `span` as the current location, then restore.
    fn within<R>(&mut self, span: Option<Span>, body: impl FnOnce(&mut Self) -> R) -> R {
        // A field with no span of its own keeps its parent's, which is closer
        // than the payload and never worse.
        let previous = match span {
            Some(span) => self.span.replace(span),
            None => self.span.clone(),
        };
        let result = body(self);
        self.span = previous;
        result
    }
}

pub fn compare_request_type(base: &TypeRef, head: &TypeRef) -> Vec<TypeIssue> {
    compare_kinds(base, head, TypeDirection::Request)
}

pub fn compare_response_type(base: &TypeRef, head: &TypeRef) -> Vec<TypeIssue> {
    compare_kinds(base, head, TypeDirection::Response)
}

/// Result of evaluating the subtyping relation `<:`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SubtypeResult {
    /// Types satisfy the subtyping relation.
    Valid,
    /// Types are incompatible; contains the detected compatibility issues.
    Incompatible(Vec<TypeIssue>),
}

impl SubtypeResult {
    #[must_use]
    pub fn is_valid(&self) -> bool {
        matches!(self, Self::Valid)
    }

    #[must_use]
    pub fn issues(&self) -> &[TypeIssue] {
        match self {
            Self::Valid => &[],
            Self::Incompatible(issues) => issues,
        }
    }
}

/// Checks the structural subtyping relation `<:`.
///
/// In response position (covariance), head must be a subtype of base (`Head <: Base`).
/// In request position (contravariance), head must be a supertype of base (`Base <: Head`).
#[must_use]
pub fn check_subtype(
    sub: &TypeRef,
    sup: &TypeRef,
    direction: TypeDirection,
    pointer: &str,
) -> SubtypeResult {
    let mut issues = Issues::default();
    match direction {
        TypeDirection::Response => {
            // Covariance: sub = Head, sup = Base -> Head <: Base
            compare_inner(sup, sub, direction, pointer, &mut issues);
        }
        TypeDirection::Request => {
            // Contravariance: sub = Base, sup = Head -> Base <: Head
            compare_inner(sub, sup, direction, pointer, &mut issues);
        }
    }
    if issues.found.is_empty() {
        SubtypeResult::Valid
    } else {
        SubtypeResult::Incompatible(issues.found.into_iter().map(|l| l.issue).collect())
    }
}

/// Compare two types, reporting where each issue is.
#[must_use]
pub fn compare(base: &TypeRef, head: &TypeRef, direction: TypeDirection) -> Vec<Located> {
    let mut issues = Issues::default();
    compare_inner(base, head, direction, "", &mut issues);

    let mut found = issues.found;
    found.sort_by(|a, b| a.issue.cmp(&b.issue).then_with(|| a.span.cmp(&b.span)));
    found.dedup();
    found
}

/// The issues alone, for a caller that does not need locations.
#[must_use]
pub fn compare_kinds(base: &TypeRef, head: &TypeRef, direction: TypeDirection) -> Vec<TypeIssue> {
    compare(base, head, direction)
        .into_iter()
        .map(|located| located.issue)
        .collect()
}

fn compare_inner(
    base: &TypeRef,
    head: &TypeRef,
    direction: TypeDirection,
    pointer: &str,
    issues: &mut Issues,
) {
    if normalized(base) == normalized(head) {
        // Identical after normalisation, but an `Unknown` on both sides is
        // still an unverified path rather than a verified-equal one.
        collect_partials(base, pointer, issues);
        return;
    }

    match (base, head) {
        (TypeRef::Unknown(kind), other) | (other, TypeRef::Unknown(kind)) => {
            issues.push(TypeIssue::Partial {
                pointer: pointer.to_owned(),
                detail: kind.describe(),
            });
            // The other side may itself hide further unmodelled constructs.
            collect_partials(other, pointer, issues);
        }
        (TypeRef::Cycle(base_name), TypeRef::Cycle(head_name)) => {
            if base_name != head_name {
                push_changed(
                    direction,
                    pointer,
                    format!("recursive type changed from `{base_name}` to `{head_name}`"),
                    issues,
                );
            }
        }
        (
            TypeRef::Scalar {
                ty: base_ty,
                format: base_format,
                nullable: base_nullable,
                constraints: base_constraints,
            },
            TypeRef::Scalar {
                ty: head_ty,
                format: head_format,
                nullable: head_nullable,
                constraints: head_constraints,
            },
        ) => {
            if base_ty != head_ty {
                push_changed(
                    direction,
                    pointer,
                    format!("type changed from `{base_ty}` to `{head_ty}`"),
                    issues,
                );
                return;
            }
            compare_scalar_format(
                direction,
                pointer,
                base_format.as_deref(),
                head_format.as_deref(),
                issues,
            );
            compare_nullable(direction, pointer, *base_nullable, *head_nullable, issues);
            compare_constraints(
                direction,
                pointer,
                base_constraints,
                head_constraints,
                issues,
            );
        }
        (
            TypeRef::Enum {
                values: base_values,
                numbers: base_numbers,
            },
            TypeRef::Enum {
                values: head_values,
                numbers: head_numbers,
            },
        ) => {
            compare_enum_numbers(pointer, base_numbers, head_numbers, issues);
            compare_enum(direction, pointer, base_values, head_values, issues);
        }
        (
            TypeRef::Array {
                items: base_items,
                nullable: base_nullable,
            },
            TypeRef::Array {
                items: head_items,
                nullable: head_nullable,
            },
        ) => {
            compare_nullable(direction, pointer, *base_nullable, *head_nullable, issues);
            compare_inner(
                base_items,
                head_items,
                direction,
                &format!("{pointer}/items"),
                issues,
            );
        }
        (
            TypeRef::Object {
                fields: base_fields,
                additional: base_additional,
                nullable: base_nullable,
            },
            TypeRef::Object {
                fields: head_fields,
                additional: head_additional,
                nullable: head_nullable,
            },
        ) => {
            compare_nullable(direction, pointer, *base_nullable, *head_nullable, issues);
            if direction == TypeDirection::Request && *base_additional && !head_additional {
                issues.push(TypeIssue::RequestTypeNarrowed {
                    pointer: pointer.to_owned(),
                    reason: "`additionalProperties` tightened from true to false".to_owned(),
                });
            }
            compare_object_fields(direction, pointer, base_fields, head_fields, issues);
        }
        (
            TypeRef::Tuple {
                prefix_items: base_items,
                additional_items: base_additional,
                nullable: base_nullable,
            },
            TypeRef::Tuple {
                prefix_items: head_items,
                additional_items: head_additional,
                nullable: head_nullable,
            },
        ) => {
            compare_nullable(direction, pointer, *base_nullable, *head_nullable, issues);
            let min_len = base_items.len().min(head_items.len());
            for i in 0..min_len {
                compare_inner(
                    &base_items[i],
                    &head_items[i],
                    direction,
                    &format!("{pointer}/prefixItems/{i}"),
                    issues,
                );
            }
            match direction {
                TypeDirection::Request => {
                    if head_items.len() > base_items.len() && base_additional.is_none() {
                        issues.push(TypeIssue::RequestTypeNarrowed {
                            pointer: pointer.to_owned(),
                            reason: format!(
                                "tuple prefixItems expanded from {} to {} required elements",
                                base_items.len(),
                                head_items.len()
                            ),
                        });
                    }
                }
                TypeDirection::Response => {
                    if head_items.len() < base_items.len() && head_additional.is_none() {
                        push_changed(
                            direction,
                            pointer,
                            format!(
                                "tuple prefixItems reduced from {} to {} elements",
                                base_items.len(),
                                head_items.len()
                            ),
                            issues,
                        );
                    }
                }
            }
            match (base_additional, head_additional) {
                (Some(base_add), Some(head_add)) => {
                    compare_inner(
                        base_add,
                        head_add,
                        direction,
                        &format!("{pointer}/items"),
                        issues,
                    );
                }
                (Some(_), None) if direction == TypeDirection::Request => {
                    issues.push(TypeIssue::RequestTypeNarrowed {
                        pointer: pointer.to_owned(),
                        reason: "additional tuple items disallowed".to_owned(),
                    });
                }
                _ => {}
            }
        }
        (
            TypeRef::OneOf {
                variants: base_variants,
                discriminator: base_disc,
            },
            TypeRef::OneOf {
                variants: head_variants,
                discriminator: head_disc,
            },
        ) => {
            if let (Some(base_d), Some(head_d)) = (base_disc, head_disc) {
                if base_d.property_name != head_d.property_name {
                    push_changed(
                        direction,
                        pointer,
                        format!(
                            "discriminator property changed from `{}` to `{}`",
                            base_d.property_name, head_d.property_name
                        ),
                        issues,
                    );
                }
                for (key, target) in &base_d.mapping {
                    match head_d.mapping.get(key) {
                        Some(head_target) if head_target != target => {
                            push_changed(
                                direction,
                                pointer,
                                format!(
                                    "discriminator mapping for `{key}` changed from `{target}` to `{head_target}`"
                                ),
                                issues,
                            );
                        }
                        None => match direction {
                            TypeDirection::Request => {
                                issues.push(TypeIssue::RequestTypeNarrowed {
                                    pointer: pointer.to_owned(),
                                    reason: format!(
                                        "discriminator mapping key `{key}` was removed"
                                    ),
                                });
                            }
                            TypeDirection::Response => {
                                push_changed(
                                    direction,
                                    pointer,
                                    format!("discriminator mapping key `{key}` was removed"),
                                    issues,
                                );
                            }
                        },
                        _ => {}
                    }
                }
            } else if base_disc.is_some() && head_disc.is_none() {
                push_changed(
                    direction,
                    pointer,
                    "discriminator was removed".to_owned(),
                    issues,
                );
            }

            let base_set = variant_set(base_variants);
            let head_set = variant_set(head_variants);
            let removed = base_set.difference(&head_set).count();
            let added = head_set.difference(&base_set).count();

            match direction {
                // Removing a variant stops accepting input that used to work;
                // adding one is safe for a producer to send.
                TypeDirection::Request => {
                    if removed > 0 {
                        issues.push(TypeIssue::RequestVariantRemoved {
                            pointer: pointer.to_owned(),
                        });
                    }
                }
                // Adding a variant breaks an exhaustive reader; removing one
                // breaks a reader that still handles it.
                TypeDirection::Response => {
                    if added > 0 {
                        issues.push(TypeIssue::ResponseVariantAdded {
                            pointer: pointer.to_owned(),
                        });
                    }
                    if removed > 0 {
                        push_changed(
                            direction,
                            pointer,
                            "a `oneOf` variant was removed from the response".to_owned(),
                            issues,
                        );
                    }
                }
            }
            for variant in base_variants.iter().chain(head_variants) {
                collect_partials(variant, pointer, issues);
            }
        }
        (base_other, head_other) => {
            push_changed(
                direction,
                pointer,
                format!(
                    "type changed from {} to {}",
                    describe(base_other),
                    describe(head_other)
                ),
                issues,
            );
        }
    }
}

fn compare_object_fields(
    direction: TypeDirection,
    pointer: &str,
    base_fields: &BTreeMap<String, Field>,
    head_fields: &BTreeMap<String, Field>,
    issues: &mut Issues,
) {
    // Where the format has wire numbers, the number is the field's identity:
    // a rename with a stable number is wire-compatible and a renumber with a
    // stable name is a hard break. Comparing by name would report the first
    // and miss the second, which is exactly backwards.
    let pairs = if uses_wire_numbers(base_fields) && uses_wire_numbers(head_fields) {
        pair_by_number(base_fields, head_fields, pointer, issues)
    } else {
        pair_by_name(base_fields, head_fields)
    };

    for pairing in pairs {
        match pairing {
            Pairing::Both {
                name,
                base_field,
                head_field,
            } => {
                let field_pointer = field_pointer(pointer, name);
                issues.within(head_field.span.clone(), |issues| {
                    if direction == TypeDirection::Request
                        && !base_field.required
                        && head_field.required
                    {
                        issues.push(TypeIssue::RequestTypeNarrowed {
                            pointer: field_pointer.clone(),
                            reason: format!("field `{name}` became required"),
                        });
                    }
                    if direction == TypeDirection::Response
                        && base_field.required
                        && !head_field.required
                    {
                        issues.push(TypeIssue::ResponseFieldOptional {
                            pointer: field_pointer.clone(),
                            field: name.to_owned(),
                        });
                    }
                    compare_inner(
                        &base_field.ty,
                        &head_field.ty,
                        direction,
                        &field_pointer,
                        issues,
                    );
                });
            }
            Pairing::BaseOnly { name, base_field } => {
                let field_pointer = field_pointer(pointer, name);
                issues.span = base_field.span.clone();
                match direction {
                    TypeDirection::Request => issues.push(TypeIssue::RequestTypeNarrowed {
                        pointer: field_pointer,
                        reason: format!(
                            "request field `{name}` was removed and is no longer accepted"
                        ),
                    }),
                    TypeDirection::Response => issues.push(TypeIssue::ResponseFieldRemoved {
                        pointer: field_pointer,
                        field: name.to_owned(),
                    }),
                }
                issues.span = None;
            }
            Pairing::HeadOnly { name, head_field } => {
                let field_pointer = field_pointer(pointer, name);
                issues.span = head_field.span.clone();
                match (direction, head_field.required) {
                    (TypeDirection::Request, true) => {
                        issues.push(TypeIssue::RequestFieldAddedRequired {
                            pointer: field_pointer.clone(),
                            field: name.to_owned(),
                        });
                    }
                    (TypeDirection::Request, false) => {
                        issues.push(TypeIssue::RequestFieldAddedOptional {
                            pointer: field_pointer.clone(),
                            field: name.to_owned(),
                        });
                    }
                    (TypeDirection::Response, _) => {
                        issues.push(TypeIssue::ResponseFieldAdded {
                            pointer: field_pointer.clone(),
                            field: name.to_owned(),
                        });
                    }
                }
                collect_partials(&head_field.ty, &field_pointer, issues);
                issues.span = None;
            }
        }
    }
}

/// How a field on one side lines up with the other.
///
/// Borrows rather than clones: a `Field` carries its span and its whole type,
/// and the pairs are consumed in the loop that builds them.
enum Pairing<'a> {
    Both {
        name: &'a str,
        base_field: &'a Field,
        head_field: &'a Field,
    },
    BaseOnly {
        name: &'a str,
        base_field: &'a Field,
    },
    HeadOnly {
        name: &'a str,
        head_field: &'a Field,
    },
}

fn uses_wire_numbers(fields: &BTreeMap<String, Field>) -> bool {
    !fields.is_empty() && fields.values().all(|field| field.number.is_some())
}

fn pair_by_name<'a>(
    base_fields: &'a BTreeMap<String, Field>,
    head_fields: &'a BTreeMap<String, Field>,
) -> Vec<Pairing<'a>> {
    let mut pairs = Vec::new();
    for (name, base_field) in base_fields {
        match head_fields.get(name) {
            Some(head_field) => pairs.push(Pairing::Both {
                name,
                base_field,
                head_field,
            }),
            None => pairs.push(Pairing::BaseOnly { name, base_field }),
        }
    }
    for (name, head_field) in head_fields {
        if !base_fields.contains_key(name) {
            pairs.push(Pairing::HeadOnly { name, head_field });
        }
    }
    pairs
}

fn pair_by_number<'a>(
    base_fields: &'a BTreeMap<String, Field>,
    head_fields: &'a BTreeMap<String, Field>,
    pointer: &str,
    issues: &mut Issues,
) -> Vec<Pairing<'a>> {
    let by_number = |fields: &'a BTreeMap<String, Field>| {
        fields
            .iter()
            .filter_map(|(name, field)| field.number.map(|n| (n, (name.as_str(), field))))
            .collect::<BTreeMap<i32, (&'a str, &'a Field)>>()
    };
    let base_by_number = by_number(base_fields);
    let head_by_number = by_number(head_fields);

    // A field that kept its name but moved number is a wire break, and the
    // number-keyed pairing below would otherwise report it as one field
    // removed and an unrelated one added.
    for (base_name, base_field) in base_fields {
        let (Some(base_number), Some(head_field)) = (base_field.number, head_fields.get(base_name))
        else {
            continue;
        };
        if let Some(head_number) = head_field.number
            && head_number != base_number
        {
            issues.within(head_field.span.clone(), |issues| {
                issues.push(TypeIssue::FieldNumberChanged {
                    pointer: field_pointer(pointer, base_name),
                    field: base_name.clone(),
                    from: base_number,
                    to: head_number,
                });
            });
        }
    }

    let mut pairs = Vec::new();
    for (number, (base_name, base_field)) in &base_by_number {
        match head_by_number.get(number) {
            Some((head_name, head_field)) => {
                if head_name != base_name {
                    issues.within(head_field.span.clone(), |issues| {
                        issues.push(TypeIssue::FieldRenamed {
                            pointer: field_pointer(pointer, base_name),
                            from: (*base_name).to_owned(),
                            to: (*head_name).to_owned(),
                        });
                    });
                }
                pairs.push(Pairing::Both {
                    name: base_name,
                    base_field,
                    head_field,
                });
            }
            None => pairs.push(Pairing::BaseOnly {
                name: base_name,
                base_field,
            }),
        }
    }
    for (number, (head_name, head_field)) in &head_by_number {
        if !base_by_number.contains_key(number) {
            pairs.push(Pairing::HeadOnly {
                name: head_name,
                head_field,
            });
        }
    }
    pairs
}

fn compare_enum(
    direction: TypeDirection,
    pointer: &str,
    base_values: &BTreeSet<String>,
    head_values: &BTreeSet<String>,
    issues: &mut Issues,
) {
    let removed = base_values.difference(head_values).count();
    let added = head_values.difference(base_values).count();

    match direction {
        TypeDirection::Request => {
            if removed > 0 {
                let gone = base_values
                    .difference(head_values)
                    .cloned()
                    .collect::<Vec<_>>()
                    .join(", ");
                issues.push(TypeIssue::RequestTypeNarrowed {
                    pointer: pointer.to_owned(),
                    reason: format!("enum no longer accepts: {gone}"),
                });
            }
        }
        TypeDirection::Response => {
            if removed > 0 {
                let gone = base_values
                    .difference(head_values)
                    .cloned()
                    .collect::<Vec<_>>()
                    .join(", ");
                push_changed(
                    direction,
                    pointer,
                    format!("response enum no longer produces: {gone}"),
                    issues,
                );
            }
            if added > 0 {
                issues.push(TypeIssue::ResponseEnumExtended {
                    pointer: pointer.to_owned(),
                });
            }
        }
    }
}

/// An enum value's wire number is its identity where the format has one, for
/// the same reason a message field's is: renumbering changes what the bytes
/// mean to every already-deployed client.
fn compare_enum_numbers(
    pointer: &str,
    base_numbers: &BTreeMap<String, i32>,
    head_numbers: &BTreeMap<String, i32>,
    issues: &mut Issues,
) {
    if base_numbers.is_empty() || head_numbers.is_empty() {
        return;
    }

    for (name, base_number) in base_numbers {
        if let Some(head_number) = head_numbers.get(name)
            && head_number != base_number
        {
            issues.push(TypeIssue::FieldNumberChanged {
                pointer: field_pointer(pointer, name),
                field: name.clone(),
                from: *base_number,
                to: *head_number,
            });
        }
    }

    let invert = |numbers: &BTreeMap<String, i32>| {
        numbers
            .iter()
            .map(|(name, number)| (*number, name.clone()))
            .collect::<BTreeMap<i32, String>>()
    };
    for (number, base_name) in invert(base_numbers) {
        if let Some(head_name) = invert(head_numbers).get(&number)
            && *head_name != base_name
        {
            issues.push(TypeIssue::FieldRenamed {
                pointer: field_pointer(pointer, &base_name),
                from: base_name.clone(),
                to: head_name.clone(),
            });
        }
    }
}

fn compare_scalar_format(
    direction: TypeDirection,
    pointer: &str,
    base_format: Option<&str>,
    head_format: Option<&str>,
    issues: &mut Issues,
) {
    if base_format == head_format {
        return;
    }
    match (direction, base_format, head_format) {
        // Constraining a previously unconstrained input rejects callers.
        (TypeDirection::Request, None, Some(added)) => {
            issues.push(TypeIssue::RequestTypeNarrowed {
                pointer: pointer.to_owned(),
                reason: format!("format `{added}` now constrains a previously free value"),
            });
        }
        // Relaxing an input, or documenting an output more precisely, is safe.
        (TypeDirection::Request, Some(_), None) | (TypeDirection::Response, None, Some(_)) => {}
        (_, base, head) => push_changed(
            direction,
            pointer,
            format!(
                "format changed from `{}` to `{}`",
                base.unwrap_or("none"),
                head.unwrap_or("none")
            ),
            issues,
        ),
    }
}

fn compare_nullable(
    direction: TypeDirection,
    pointer: &str,
    base_nullable: bool,
    head_nullable: bool,
    issues: &mut Issues,
) {
    match (direction, base_nullable, head_nullable) {
        // Refusing a null that used to be accepted breaks a caller sending it.
        (TypeDirection::Request, true, false) => issues.push(TypeIssue::RequestTypeNarrowed {
            pointer: pointer.to_owned(),
            reason: "no longer accepts null".to_owned(),
        }),
        // Producing a null where one was never possible breaks a reader whose
        // type for this field is not optional.
        (TypeDirection::Response, false, true) => push_changed(
            direction,
            pointer,
            "may now be null, where it previously could not be".to_owned(),
            issues,
        ),
        _ => {}
    }
}

fn compare_constraints(
    direction: TypeDirection,
    pointer: &str,
    base: &Constraints,
    head: &Constraints,
    issues: &mut Issues,
) {
    if base == head {
        return;
    }

    let mut narrowings = Vec::new();
    if tightened_upper(base.max_length, head.max_length) {
        narrowings.push(format!(
            "`maxLength` tightened from {} to {}",
            describe_bound(base.max_length),
            describe_bound(head.max_length)
        ));
    }
    if tightened_lower(base.min_length, head.min_length) {
        narrowings.push(format!(
            "`minLength` tightened from {} to {}",
            describe_bound(base.min_length),
            describe_bound(head.min_length)
        ));
    }
    if tightened_numeric_upper(base.maximum.as_deref(), head.maximum.as_deref()) {
        narrowings.push(format!(
            "`maximum` tightened from {} to {}",
            base.maximum.as_deref().unwrap_or("unbounded"),
            head.maximum.as_deref().unwrap_or("unbounded")
        ));
    }
    if tightened_numeric_lower(base.minimum.as_deref(), head.minimum.as_deref()) {
        narrowings.push(format!(
            "`minimum` tightened from {} to {}",
            base.minimum.as_deref().unwrap_or("unbounded"),
            head.minimum.as_deref().unwrap_or("unbounded")
        ));
    }
    if base.pattern != head.pattern && head.pattern.is_some() {
        narrowings.push(match &base.pattern {
            // Two patterns cannot be compared for containment without
            // executing them, so any change is treated as a narrowing rather
            // than assumed safe.
            Some(previous) => format!(
                "`pattern` changed from `{previous}` to `{}`",
                head.pattern.as_deref().unwrap_or_default()
            ),
            None => format!(
                "`pattern` `{}` now constrains a previously free value",
                head.pattern.as_deref().unwrap_or_default()
            ),
        });
    }

    if narrowings.is_empty() {
        return;
    }
    let reason = narrowings.join("; ");
    match direction {
        TypeDirection::Request => issues.push(TypeIssue::RequestTypeNarrowed {
            pointer: pointer.to_owned(),
            reason,
        }),
        // A narrower output is not a break on its own: a reader that coped
        // with the wider range still copes. Only the request side fires.
        TypeDirection::Response => {}
    }
}

fn tightened_upper(base: Option<u64>, head: Option<u64>) -> bool {
    match (base, head) {
        (_, None) => false,
        (None, Some(_)) => true,
        (Some(base), Some(head)) => head < base,
    }
}

fn tightened_lower(base: Option<u64>, head: Option<u64>) -> bool {
    match (base, head) {
        (_, None) => false,
        (None, Some(head)) => head > 0,
        (Some(base), Some(head)) => head > base,
    }
}

fn tightened_numeric_upper(base: Option<&str>, head: Option<&str>) -> bool {
    match (base, head) {
        (_, None) => false,
        (None, Some(_)) => true,
        (Some(base), Some(head)) => match (base.parse::<f64>(), head.parse::<f64>()) {
            (Ok(base), Ok(head)) => head < base,
            // An unparseable bound that changed is reported rather than
            // ignored: silently skipping it would be a false clean.
            _ => base != head,
        },
    }
}

fn tightened_numeric_lower(base: Option<&str>, head: Option<&str>) -> bool {
    match (base, head) {
        (_, None) => false,
        (None, Some(_)) => true,
        (Some(base), Some(head)) => match (base.parse::<f64>(), head.parse::<f64>()) {
            (Ok(base), Ok(head)) => head > base,
            _ => base != head,
        },
    }
}

fn describe_bound(bound: Option<u64>) -> String {
    bound.map_or_else(|| "unbounded".to_owned(), |value| value.to_string())
}

/// Walk a type that is not being structurally compared and report anything on
/// it that could not be modelled. Without this, an `Unknown` nested inside a
/// type that is equal on both sides would read as verified.
fn collect_partials(ty: &TypeRef, pointer: &str, issues: &mut Issues) {
    match ty {
        TypeRef::Unknown(kind) => issues.push(TypeIssue::Partial {
            pointer: pointer.to_owned(),
            detail: kind.describe(),
        }),
        TypeRef::Array { items, .. } => {
            collect_partials(items, &format!("{pointer}/items"), issues);
        }
        TypeRef::Tuple {
            prefix_items,
            additional_items,
            ..
        } => {
            for (index, item) in prefix_items.iter().enumerate() {
                collect_partials(item, &format!("{pointer}/prefixItems/{index}"), issues);
            }
            if let Some(additional) = additional_items {
                collect_partials(additional, &format!("{pointer}/items"), issues);
            }
        }
        TypeRef::Object { fields, .. } => {
            for (name, field) in fields {
                collect_partials(&field.ty, &field_pointer(pointer, name), issues);
            }
        }
        TypeRef::OneOf { variants, .. } => {
            for (index, variant) in variants.iter().enumerate() {
                collect_partials(variant, &format!("{pointer}/{index}"), issues);
            }
        }
        TypeRef::Scalar { .. } | TypeRef::Enum { .. } | TypeRef::Cycle(_) => {}
    }
}

fn push_changed(direction: TypeDirection, pointer: &str, reason: String, issues: &mut Issues) {
    match direction {
        TypeDirection::Request => issues.push(TypeIssue::RequestTypeNarrowed {
            pointer: pointer.to_owned(),
            reason,
        }),
        TypeDirection::Response => issues.push(TypeIssue::ResponseTypeChanged {
            pointer: pointer.to_owned(),
            reason,
        }),
    }
}

fn field_pointer(pointer: &str, name: &str) -> String {
    if name.is_empty() {
        return pointer.to_owned();
    }
    format!("{pointer}/{}", name.replace('~', "~0").replace('/', "~1"))
}

fn describe(ty: &TypeRef) -> String {
    match ty {
        TypeRef::Scalar { ty, .. } => format!("`{ty}`"),
        TypeRef::Enum { .. } => "an enum".to_owned(),
        TypeRef::Array { .. } => "an array".to_owned(),
        TypeRef::Tuple { .. } => "a tuple".to_owned(),
        TypeRef::Object { .. } => "an object".to_owned(),
        TypeRef::OneOf { .. } => "a union".to_owned(),
        TypeRef::Cycle(name) => format!("a reference to `{name}`"),
        TypeRef::Unknown(_) => "an unmodelled construct".to_owned(),
    }
}

/// Fold the spellings that mean the same thing, so a faithful translation
/// between OpenAPI 3.0 and 3.1 registers as no change at all.
fn normalized(ty: &TypeRef) -> TypeRef {
    match ty {
        TypeRef::Tuple {
            prefix_items,
            additional_items,
            nullable,
        } => TypeRef::Tuple {
            prefix_items: prefix_items.iter().map(normalized).collect(),
            additional_items: additional_items
                .as_ref()
                .map(|item| Box::new(normalized(item))),
            nullable: *nullable,
        },
        TypeRef::OneOf {
            variants,
            discriminator,
        } => fold_nullable_union(variants).unwrap_or(TypeRef::OneOf {
            variants: {
                let mut folded = variants.iter().map(normalized).collect::<Vec<_>>();
                folded.sort_by_key(|variant| format!("{variant:?}"));
                folded
            },
            discriminator: discriminator.clone(),
        }),
        TypeRef::Array { items, nullable } => TypeRef::Array {
            items: Box::new(normalized(items)),
            nullable: *nullable,
        },
        TypeRef::Object {
            fields,
            additional,
            nullable,
        } => TypeRef::Object {
            fields: fields
                .iter()
                .map(|(name, field)| {
                    (
                        name.clone(),
                        Field {
                            ty: normalized(&field.ty),
                            required: field.required,
                            deprecated: field.deprecated,
                            number: field.number,
                            span: field.span.clone(),
                        },
                    )
                })
                .collect(),
            additional: *additional,
            nullable: *nullable,
        },
        _ => ty.clone(),
    }
}

/// `oneOf: [T, null]` in 3.1 means what `nullable: true` meant in 3.0.
fn fold_nullable_union(variants: &[TypeRef]) -> Option<TypeRef> {
    if variants.len() != 2 {
        return None;
    }
    let mut saw_null = false;
    let mut other = None;
    for variant in variants {
        match variant {
            TypeRef::Scalar { ty, .. } if ty == "null" => saw_null = true,
            candidate => other = Some(candidate),
        }
    }
    if !saw_null {
        return None;
    }

    match normalized(other?) {
        TypeRef::Scalar {
            ty,
            format,
            constraints,
            ..
        } => Some(TypeRef::Scalar {
            ty,
            format,
            nullable: true,
            constraints,
        }),
        TypeRef::Array { items, .. } => Some(TypeRef::Array {
            items,
            nullable: true,
        }),
        TypeRef::Object {
            fields, additional, ..
        } => Some(TypeRef::Object {
            fields,
            additional,
            nullable: true,
        }),
        _ => None,
    }
}

fn variant_set(variants: &[TypeRef]) -> BTreeSet<String> {
    variants
        .iter()
        .map(|variant| format!("{:?}", normalized(variant)))
        .collect()
}

/// Does this type carry anything the ingester could not model?
#[must_use]
pub fn has_unmodelled(ty: &TypeRef) -> bool {
    let mut issues = Issues::default();
    collect_partials(ty, "", &mut issues);
    !issues.found.is_empty()
}

/// The kinds present on a type, for reporting without a comparison.
#[must_use]
pub fn unmodelled_kinds(ty: &TypeRef) -> Vec<UnmodelledKind> {
    fn walk(ty: &TypeRef, out: &mut Vec<UnmodelledKind>) {
        match ty {
            TypeRef::Unknown(kind) => out.push(kind.clone()),
            TypeRef::Array { items, .. } => walk(items, out),
            TypeRef::Tuple {
                prefix_items,
                additional_items,
                ..
            } => {
                for item in prefix_items {
                    walk(item, out);
                }
                if let Some(additional) = additional_items {
                    walk(additional, out);
                }
            }
            TypeRef::Object { fields, .. } => {
                for field in fields.values() {
                    walk(&field.ty, out);
                }
            }
            TypeRef::OneOf { variants, .. } => {
                for variant in variants {
                    walk(variant, out);
                }
            }
            _ => {}
        }
    }
    let mut out = Vec::new();
    walk(ty, &mut out);
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::contract::Discriminator;
    use std::collections::{BTreeMap, BTreeSet};

    fn scalar(ty: &str, nullable: bool) -> TypeRef {
        TypeRef::Scalar {
            ty: ty.to_owned(),
            format: None,
            nullable,
            constraints: Constraints::default(),
        }
    }

    fn object(fields: Vec<(&str, TypeRef, bool)>, additional: bool) -> TypeRef {
        TypeRef::Object {
            fields: fields
                .into_iter()
                .map(|(name, ty, required)| (name.to_owned(), Field::new(ty, required)))
                .collect(),
            additional,
            nullable: false,
        }
    }

    fn numbered(fields: Vec<(&str, i32, TypeRef)>) -> TypeRef {
        TypeRef::Object {
            fields: fields
                .into_iter()
                .map(|(name, number, ty)| {
                    (
                        name.to_owned(),
                        Field {
                            ty,
                            required: false,
                            deprecated: false,
                            number: Some(number),
                            span: None,
                        },
                    )
                })
                .collect(),
            additional: false,
            nullable: false,
        }
    }

    #[test]
    fn identical_types_produce_nothing() {
        assert!(
            compare_request_type(&scalar("string", false), &scalar("string", false)).is_empty()
        );
    }

    #[test]
    fn cycle_with_same_name_is_compatible() {
        assert!(
            compare_request_type(
                &TypeRef::Cycle("Payment".to_owned()),
                &TypeRef::Cycle("Payment".to_owned())
            )
            .is_empty()
        );
    }

    #[test]
    fn cycle_name_change_is_incompatible() {
        let issues = compare_response_type(
            &TypeRef::Cycle("Payment".to_owned()),
            &TypeRef::Cycle("Invoice".to_owned()),
        );
        assert!(matches!(issues[0], TypeIssue::ResponseTypeChanged { .. }));
    }

    #[test]
    fn unknown_on_either_side_reports_partial_never_clean() {
        let unknown = TypeRef::Unknown(UnmodelledKind::ExternalRef("common.yaml#/X".to_owned()));
        let issues = compare_response_type(
            &object(vec![("id", scalar("string", false), true)], true),
            &unknown,
        );
        assert!(
            issues
                .iter()
                .any(|issue| matches!(issue, TypeIssue::Partial { .. })),
            "an unmodelled construct must never compare as clean: {issues:?}"
        );
    }

    #[test]
    fn unknown_identical_on_both_sides_is_still_partial() {
        let unknown = TypeRef::Unknown(UnmodelledKind::SchemaDeferred);
        let issues = compare_response_type(&unknown, &unknown);
        assert!(matches!(issues[0], TypeIssue::Partial { .. }));
    }

    #[test]
    fn unknown_nested_in_an_equal_type_is_still_partial() {
        let ty = object(
            vec![(
                "child",
                TypeRef::Unknown(UnmodelledKind::Unsupported("not".to_owned())),
                false,
            )],
            true,
        );
        let issues = compare_response_type(&ty, &ty);
        assert_eq!(issues.len(), 1);
        assert!(matches!(
            &issues[0],
            TypeIssue::Partial { pointer, .. } if pointer == "/child"
        ));
    }

    #[test]
    fn response_field_removal_is_its_own_issue_with_a_pointer() {
        let base = object(
            vec![
                ("id", scalar("string", false), true),
                ("customer_id", scalar("string", false), true),
            ],
            true,
        );
        let head = object(vec![("id", scalar("string", false), true)], true);

        let issues = compare_response_type(&base, &head);
        assert!(issues.iter().any(|issue| matches!(
            issue,
            TypeIssue::ResponseFieldRemoved { field, pointer }
                if field == "customer_id" && pointer == "/customer_id"
        )));
    }

    #[test]
    fn response_field_becoming_optional_is_its_own_issue() {
        let base = object(vec![("id", scalar("string", false), true)], true);
        let head = object(vec![("id", scalar("string", false), false)], true);

        let issues = compare_response_type(&base, &head);
        assert!(issues.iter().any(|issue| matches!(
            issue,
            TypeIssue::ResponseFieldOptional { field, .. } if field == "id"
        )));
    }

    #[test]
    fn added_response_field_is_additive_and_reported_separately() {
        let base = object(vec![("id", scalar("string", false), true)], true);
        let head = object(
            vec![
                ("id", scalar("string", false), true),
                ("extra", scalar("string", false), false),
            ],
            true,
        );

        let issues = compare_response_type(&base, &head);
        assert_eq!(issues.len(), 1);
        assert!(matches!(
            &issues[0],
            TypeIssue::ResponseFieldAdded { field, .. } if field == "extra"
        ));
    }

    #[test]
    fn request_detects_oneof_variant_removal_and_ignores_addition() {
        let base = TypeRef::OneOf {
            variants: vec![scalar("string", false), scalar("integer", false)],
            discriminator: None,
        };
        let head = TypeRef::OneOf {
            variants: vec![scalar("string", false)],
            discriminator: None,
        };

        assert!(
            compare_request_type(&base, &head)
                .iter()
                .any(|issue| matches!(issue, TypeIssue::RequestVariantRemoved { .. }))
        );
        assert!(compare_request_type(&head, &base).is_empty());
    }

    #[test]
    fn response_detects_oneof_variant_addition() {
        let base = TypeRef::OneOf {
            variants: vec![scalar("string", false)],
            discriminator: None,
        };
        let head = TypeRef::OneOf {
            variants: vec![scalar("string", false), scalar("integer", false)],
            discriminator: None,
        };

        assert!(
            compare_response_type(&base, &head)
                .iter()
                .any(|issue| matches!(issue, TypeIssue::ResponseVariantAdded { .. }))
        );
    }

    #[test]
    fn request_detects_additional_properties_tightening() {
        let issues = compare_request_type(&object(vec![], true), &object(vec![], false));
        assert!(issues.iter().any(|issue| matches!(
            issue,
            TypeIssue::RequestTypeNarrowed { reason, .. }
                if reason.contains("additionalProperties")
        )));
    }

    #[test]
    fn normalizes_openapi_nullable_between_30_and_31() {
        let openapi_30 = scalar("string", true);
        let openapi_31 = TypeRef::OneOf {
            variants: vec![scalar("string", false), scalar("null", false)],
            discriminator: None,
        };

        assert!(compare_request_type(&openapi_30, &openapi_31).is_empty());
        assert!(compare_response_type(&openapi_30, &openapi_31).is_empty());
    }

    #[test]
    fn request_enum_narrowing_is_detected_and_widening_is_not() {
        let wide = TypeRef::Enum {
            values: BTreeSet::from(["pending".to_owned(), "paid".to_owned()]),
            numbers: BTreeMap::new(),
        };
        let narrow = TypeRef::Enum {
            values: BTreeSet::from(["pending".to_owned()]),
            numbers: BTreeMap::new(),
        };

        assert!(compare_request_type(&wide, &narrow).iter().any(|issue| {
            matches!(issue, TypeIssue::RequestTypeNarrowed { reason, .. } if reason.contains("paid"))
        }));
        assert!(compare_request_type(&narrow, &wide).is_empty());
    }

    #[test]
    fn response_enum_extension_is_detected_and_narrowing_is_a_change() {
        let narrow = TypeRef::Enum {
            values: BTreeSet::from(["pending".to_owned()]),
            numbers: BTreeMap::new(),
        };
        let wide = TypeRef::Enum {
            values: BTreeSet::from(["pending".to_owned(), "paid".to_owned()]),
            numbers: BTreeMap::new(),
        };

        assert!(
            compare_response_type(&narrow, &wide)
                .iter()
                .any(|issue| matches!(issue, TypeIssue::ResponseEnumExtended { .. }))
        );
        assert!(
            compare_response_type(&wide, &narrow)
                .iter()
                .any(|issue| matches!(issue, TypeIssue::ResponseTypeChanged { .. }))
        );
    }

    #[test]
    fn request_required_field_addition_is_distinct_from_optional() {
        let base = object(vec![], true);
        let required = object(vec![("id", scalar("string", false), true)], true);
        let optional = object(vec![("id", scalar("string", false), false)], true);

        assert!(
            compare_request_type(&base, &required)
                .iter()
                .any(|issue| matches!(issue, TypeIssue::RequestFieldAddedRequired { .. }))
        );
        assert!(
            compare_request_type(&base, &optional)
                .iter()
                .any(|issue| matches!(issue, TypeIssue::RequestFieldAddedOptional { .. }))
        );
    }

    #[test]
    fn tightening_max_length_narrows_a_request() {
        let base = TypeRef::Scalar {
            ty: "string".to_owned(),
            format: None,
            nullable: false,
            constraints: Constraints {
                max_length: Some(100),
                ..Constraints::default()
            },
        };
        let head = TypeRef::Scalar {
            ty: "string".to_owned(),
            format: None,
            nullable: false,
            constraints: Constraints {
                max_length: Some(10),
                ..Constraints::default()
            },
        };

        assert!(compare_request_type(&base, &head).iter().any(|issue| {
            matches!(issue, TypeIssue::RequestTypeNarrowed { reason, .. } if reason.contains("maxLength"))
        }));
        // Relaxing it is safe.
        assert!(compare_request_type(&head, &base).is_empty());
    }

    #[test]
    fn adding_a_bound_where_there_was_none_narrows_a_request() {
        let base = scalar("integer", false);
        let head = TypeRef::Scalar {
            ty: "integer".to_owned(),
            format: None,
            nullable: false,
            constraints: Constraints {
                maximum: Some("10".to_owned()),
                ..Constraints::default()
            },
        };
        assert!(compare_request_type(&base, &head).iter().any(|issue| {
            matches!(issue, TypeIssue::RequestTypeNarrowed { reason, .. } if reason.contains("maximum"))
        }));
    }

    #[test]
    fn request_that_stops_accepting_null_is_narrowed() {
        assert!(
            compare_request_type(&scalar("string", true), &scalar("string", false))
                .iter()
                .any(|issue| matches!(
                    issue,
                    TypeIssue::RequestTypeNarrowed { reason, .. } if reason.contains("null")
                ))
        );
    }

    #[test]
    fn response_that_starts_producing_null_is_a_change() {
        assert!(
            compare_response_type(&scalar("string", false), &scalar("string", true))
                .iter()
                .any(|issue| matches!(issue, TypeIssue::ResponseTypeChanged { .. }))
        );
        // The reverse tightens the output, which no reader minds.
        assert!(
            compare_response_type(&scalar("string", true), &scalar("string", false)).is_empty()
        );
    }

    #[test]
    fn field_renumbered_is_a_wire_break() {
        let base = numbered(vec![("id", 1, scalar("string", false))]);
        let head = numbered(vec![("id", 7, scalar("string", false))]);

        let issues = compare_response_type(&base, &head);
        assert!(
            issues.iter().any(|issue| matches!(
                issue,
                TypeIssue::FieldNumberChanged { field, from, to, .. }
                    if field == "id" && *from == 1 && *to == 7
            )),
            "renumbering a field is the protobuf break: {issues:?}"
        );
    }

    #[test]
    fn field_renamed_with_a_stable_number_is_a_rename_not_a_removal() {
        let base = numbered(vec![("id", 1, scalar("string", false))]);
        let head = numbered(vec![("identifier", 1, scalar("string", false))]);

        let issues = compare_response_type(&base, &head);
        assert!(issues.iter().any(|issue| matches!(
            issue,
            TypeIssue::FieldRenamed { from, to, .. } if from == "id" && to == "identifier"
        )));
        assert!(
            !issues
                .iter()
                .any(|issue| matches!(issue, TypeIssue::ResponseFieldRemoved { .. })),
            "a stable wire number means the field was not removed: {issues:?}"
        );
    }

    #[test]
    fn numbered_field_removal_is_still_a_removal() {
        let base = numbered(vec![
            ("id", 1, scalar("string", false)),
            ("note", 2, scalar("string", false)),
        ]);
        let head = numbered(vec![("id", 1, scalar("string", false))]);

        assert!(
            compare_response_type(&base, &head)
                .iter()
                .any(|issue| matches!(
                    issue,
                    TypeIssue::ResponseFieldRemoved { field, .. } if field == "note"
                ))
        );
    }

    #[test]
    fn check_subtype_evaluates_covariance_and_contravariance() {
        let wide_str = scalar("string", true);
        let narrow_str = scalar("string", false);

        // Covariance in response: Head must be subtype of Base (Head <: Base)
        // narrow_str <: wide_str is valid
        let cov_valid = check_subtype(&narrow_str, &wide_str, TypeDirection::Response, "/response");
        assert!(cov_valid.is_valid());
        assert!(cov_valid.issues().is_empty());

        // wide_str <: narrow_str in response produces issues
        let cov_invalid =
            check_subtype(&wide_str, &narrow_str, TypeDirection::Response, "/response");
        assert!(!cov_invalid.is_valid());
        assert!(!cov_invalid.issues().is_empty());

        // Contravariance in request: Base must be subtype of Head (Base <: Head)
        // Base = narrow_str, Head = wide_str -> narrow_str <: wide_str is valid
        let contra_valid =
            check_subtype(&narrow_str, &wide_str, TypeDirection::Request, "/request");
        assert!(contra_valid.is_valid());
        assert!(contra_valid.issues().is_empty());

        // Base = wide_str, Head = narrow_str -> wide_str <: narrow_str in request produces narrowing issue
        let contra_invalid =
            check_subtype(&wide_str, &narrow_str, TypeDirection::Request, "/request");
        assert!(!contra_invalid.is_valid());
        assert!(!contra_invalid.issues().is_empty());
    }

    #[test]
    fn check_subtype_structural_objects_and_enums() {
        // Enums:
        let enum_ab = TypeRef::Enum {
            values: BTreeSet::from(["A".to_owned(), "B".to_owned()]),
            numbers: BTreeMap::new(),
        };
        let enum_abc = TypeRef::Enum {
            values: BTreeSet::from(["A".to_owned(), "B".to_owned(), "C".to_owned()]),
            numbers: BTreeMap::new(),
        };

        // Response: extending enum values in response produces ResponseEnumExtended issue
        let resp_extended = check_subtype(&enum_abc, &enum_ab, TypeDirection::Response, "/enum");
        assert!(!resp_extended.is_valid());
        assert!(
            resp_extended
                .issues()
                .iter()
                .any(|i| matches!(i, TypeIssue::ResponseEnumExtended { .. }))
        );

        // Request: enum_abc as Head accepts all variants of Base enum_ab (valid widening)
        assert!(check_subtype(&enum_ab, &enum_abc, TypeDirection::Request, "/enum").is_valid());
        // Request: enum_ab as Head rejects variant C from Base enum_abc (invalid narrowing)
        let req_narrowed = check_subtype(&enum_abc, &enum_ab, TypeDirection::Request, "/enum");
        assert!(!req_narrowed.is_valid());
        assert!(
            req_narrowed
                .issues()
                .iter()
                .any(|i| matches!(i, TypeIssue::RequestTypeNarrowed { .. }))
        );

        // Objects:
        let obj_base = object(vec![("id", scalar("string", false), true)], true);
        let obj_optional_field = object(
            vec![
                ("id", scalar("string", false), true),
                ("note", scalar("string", false), false),
            ],
            true,
        );
        let obj_required_field = object(
            vec![
                ("id", scalar("string", false), true),
                ("token", scalar("string", false), true),
            ],
            true,
        );

        // Response: adding a field to response produces ResponseFieldAdded issue (additive change)
        let resp_added = check_subtype(
            &obj_optional_field,
            &obj_base,
            TypeDirection::Response,
            "/obj",
        );
        assert!(
            resp_added
                .issues()
                .iter()
                .any(|i| matches!(i, TypeIssue::ResponseFieldAdded { .. }))
        );

        // Response: removing a field from response produces ResponseFieldRemoved issue
        let resp_removed = check_subtype(
            &obj_base,
            &obj_optional_field,
            TypeDirection::Response,
            "/obj",
        );
        assert!(
            resp_removed
                .issues()
                .iter()
                .any(|i| matches!(i, TypeIssue::ResponseFieldRemoved { .. }))
        );

        // Request: adding optional field to request produces RequestFieldAddedOptional issue (additive notice)
        let req_optional = check_subtype(
            &obj_base,
            &obj_optional_field,
            TypeDirection::Request,
            "/obj",
        );
        assert!(
            req_optional
                .issues()
                .iter()
                .any(|i| matches!(i, TypeIssue::RequestFieldAddedOptional { .. }))
        );

        // Request: adding required field to request is a break (incompatible)
        let req_break = check_subtype(
            &obj_base,
            &obj_required_field,
            TypeDirection::Request,
            "/obj",
        );
        assert!(!req_break.is_valid());
        assert!(
            req_break
                .issues()
                .iter()
                .any(|i| matches!(i, TypeIssue::RequestFieldAddedRequired { .. }))
        );

        // Arrays:
        let arr_narrow = TypeRef::Array {
            items: Box::new(scalar("string", false)),
            nullable: false,
        };
        let arr_wide = TypeRef::Array {
            items: Box::new(scalar("string", true)),
            nullable: false,
        };
        // Response: narrow item <: wide item in response is valid
        assert!(check_subtype(&arr_narrow, &arr_wide, TypeDirection::Response, "/arr").is_valid());
        // Response: wide item <: narrow item in response produces issue
        assert!(!check_subtype(&arr_wide, &arr_narrow, TypeDirection::Response, "/arr").is_valid());

        // Request: narrow item <: wide item in request is valid (Head accepts null)
        assert!(check_subtype(&arr_narrow, &arr_wide, TypeDirection::Request, "/arr").is_valid());
        // Request: wide item <: narrow item in request produces issue (Head rejects null)
        assert!(!check_subtype(&arr_wide, &arr_narrow, TypeDirection::Request, "/arr").is_valid());

        // Tuples:
        let tuple_2 = TypeRef::Tuple {
            prefix_items: vec![scalar("string", false), scalar("integer", false)],
            additional_items: None,
            nullable: false,
        };
        let tuple_3 = TypeRef::Tuple {
            prefix_items: vec![
                scalar("string", false),
                scalar("integer", false),
                scalar("boolean", false),
            ],
            additional_items: None,
            nullable: false,
        };
        // Response: tuple with fewer items reduces tuple length -> ResponseTypeChanged
        let resp_tuple_reduced = compare_response_type(&tuple_3, &tuple_2);
        assert!(
            resp_tuple_reduced
                .iter()
                .any(|i| matches!(i, TypeIssue::ResponseTypeChanged { .. }))
        );

        // Request: tuple requiring more items narrows request input -> RequestTypeNarrowed
        let req_tuple_expanded = compare_request_type(&tuple_2, &tuple_3);
        assert!(
            req_tuple_expanded
                .iter()
                .any(|i| matches!(i, TypeIssue::RequestTypeNarrowed { .. }))
        );

        // Discriminator mappings:
        let disc_base = Discriminator {
            property_name: "kind".to_owned(),
            mapping: BTreeMap::from([
                ("cat".to_owned(), "Cat".to_owned()),
                ("dog".to_owned(), "Dog".to_owned()),
            ]),
        };
        let disc_head_removed = Discriminator {
            property_name: "kind".to_owned(),
            mapping: BTreeMap::from([("cat".to_owned(), "Cat".to_owned())]),
        };
        let union_base = TypeRef::OneOf {
            variants: vec![scalar("string", false)],
            discriminator: Some(disc_base),
        };
        let union_head_removed = TypeRef::OneOf {
            variants: vec![scalar("string", false)],
            discriminator: Some(disc_head_removed),
        };

        // Removing a mapping in request narrows request
        let req_disc_removed = compare_request_type(&union_base, &union_head_removed);
        assert!(req_disc_removed.iter().any(
            |i| matches!(i, TypeIssue::RequestTypeNarrowed { reason, .. } if reason.contains("dog"))
        ));

        // Removing a mapping in response changes response
        let resp_disc_removed = compare_response_type(&union_base, &union_head_removed);
        assert!(resp_disc_removed.iter().any(
            |i| matches!(i, TypeIssue::ResponseTypeChanged { reason, .. } if reason.contains("dog"))
        ));
    }
}