roas 0.10.0

Rust OpenAPI Specification
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
//! Forward conversion from OpenAPI v3.0 to OpenAPI v3.1.
//!
//! Converts a [`crate::v3_0::spec::Spec`] into a
//! [`crate::v3_1::spec::Spec`] by reshaping the document on-the-fly via
//! `serde_json::Value`. v3.0 and v3.1 share most of their JSON shape;
//! the rewrites here cover the breaking schema-shape changes that JSON
//! Schema 2020-12 introduced and the file-upload encoding migration.
//!
//! Following the official "Upgrading from 3.0 to 3.1" guide
//! (<https://learn.openapis.org/upgrading/v3.0-to-v3.1.html>):
//!
//! 1. `openapi: "3.0.x"` → `openapi: "3.1.2"`.
//! 2. Schema `nullable: true` is dropped and the parent `type: <T>` is
//!    promoted to `type: [<T>, "null"]` so the schema deserializes as a
//!    `MultiSchema`.
//! 3. Schema `exclusiveMinimum: true` (the draft-04 boolean modifier) +
//!    `minimum: <n>` collapses into `exclusiveMinimum: <n>`. Same for
//!    `exclusiveMaximum`.
//! 4. Schema `example: <v>` becomes `examples: [<v>]` (the JSON Schema
//!    keyword name and shape).
//! 5. File-upload media types in `content` maps are migrated:
//!    * Schema `format: binary` properties inside `multipart/*` content
//!      become `contentMediaType: application/octet-stream` (the
//!      `format` keyword is dropped).
//!    * Schema `type: string, format: base64` anywhere becomes
//!      `type: string, contentEncoding: base64`.
//!    * `application/octet-stream` body schema
//!      `{type: string, format: binary}` becomes the empty schema
//!      `{}` — the form the migration guide recommends, routed
//!      through the [`crate::v3_1::schema::EmptySchema`] variant so
//!      it round-trips byte-for-byte.
//!
//! Lossless edges:
//!
//! * v3.0's `webhooks`-shaped extension data (if any) sits in
//!   `extensions` already; we don't synthesise top-level `webhooks`.
//! * `jsonSchemaDialect` stays absent — v3.1's default (`base`) is fine.
//! * `paths` becomes optional in v3.1, but we always emit it because
//!   v3.0 always had it.
//!
//! The conversion serialises the v3.0 input with serde, runs the
//! transforms, and deserialises as a v3.1 spec. If the input is a
//! valid v3.0 document the output is a structurally valid v3.1
//! document; semantic regressions are surfaced by `Spec::validate`.

use crate::v3_0::spec::Spec as V30Spec;
use crate::v3_1::spec::Spec as V31Spec;
use serde_json::{Map, Value};

impl From<V30Spec> for V31Spec {
    fn from(v30: V30Spec) -> Self {
        let mut value = serde_json::to_value(v30).expect("v3_0::Spec serializes");
        transform_spec(&mut value);
        serde_json::from_value(value).expect("transformed spec deserializes as v3_1::Spec")
    }
}

fn transform_spec(spec: &mut Value) {
    let Value::Object(obj) = spec else {
        return;
    };
    obj.insert("openapi".into(), Value::String("3.1.2".to_owned()));

    // Walk the document with two passes: a schema-shape rewrite applied
    // at every Schema-Object-shaped node, and a content-map walker that
    // rewrites file-upload schemas in light of their owning media type.
    walk_content_aware(spec);
    transform_schemas_recursive(spec);
}

/// Position of the current node relative to schema boundaries.
/// Threading this through the recursive walker keeps the schema-only
/// rewrites from touching instance-valued payloads while still
/// reaching every real sub-schema.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Pos {
    /// We haven't entered a schema yet. The walker still needs to
    /// recurse — schemas appear under known structural keys
    /// (`schema`, `schemas`, `allOf` / `anyOf` / `oneOf`, `not`, …).
    Generic,
    /// The current value is itself a Schema Object.
    Schema,
    /// The current value is a `BTreeMap<String, Schema>` (e.g.
    /// `properties`, `components.schemas`). Each entry's value is a
    /// schema.
    SchemaMap,
    /// The current value is a `Link` Object. Its `parameters` and
    /// `requestBody` fields hold arbitrary JSON (free-form
    /// runtime-expression maps and bodies) and must not be walked.
    Link,
    /// The current value is a `BTreeMap<String, Link>` (e.g.
    /// `components.links`, `Response.links`). Each entry's value is
    /// a Link.
    LinkMap,
}

/// Apply schema-shape rewrites — `nullable: true` → `type` array, the
/// boolean `exclusive*` modifier → numeric `exclusive*`, single
/// `example` → `examples: [example]`, and `format: base64` →
/// `contentEncoding: base64` — at every Schema Object reached via the
/// document's structural shape. Sub-schemas inside `properties`,
/// `items`, `allOf`, etc. are walked; instance-valued payloads
/// (`example`, `examples`, `default`, `enum`, `const`, `ExampleObject.value`)
/// are skipped so user-supplied JSON that happens to contain
/// schema-shaped keys (e.g. an example with `type` and `nullable`)
/// round-trips byte-for-byte.
fn transform_schemas_recursive(value: &mut Value) {
    walk(value, Pos::Generic);
}

fn walk(value: &mut Value, pos: Pos) {
    match value {
        Value::Object(obj) => match pos {
            Pos::Schema => walk_schema_object(obj),
            Pos::SchemaMap => {
                for (_, v) in obj.iter_mut() {
                    walk(v, Pos::Schema);
                }
            }
            Pos::Link => walk_link_object(obj),
            Pos::LinkMap => {
                for (_, v) in obj.iter_mut() {
                    walk(v, Pos::Link);
                }
            }
            Pos::Generic => walk_generic_object(obj),
        },
        Value::Array(arr) => {
            for v in arr.iter_mut() {
                walk(v, pos);
            }
        }
        _ => {}
    }
}

/// Walk a Link Object's keys. Link's `parameters`
/// (`Map<String, runtime-expression>`) and `requestBody` (free-form
/// JSON / runtime expression) are opaque user payloads and must not
/// be touched — they may legally contain `schema`, `nullable`,
/// `content`, etc. without being OpenAPI schema or content shapes.
fn walk_link_object(obj: &mut Map<String, Value>) {
    for (k, v) in obj.iter_mut() {
        if is_extension_key(k) {
            continue;
        }
        match k.as_str() {
            "parameters" | "requestBody" => continue,
            // `server` and any future Link fields are walked
            // generically.
            _ => walk(v, Pos::Generic),
        }
    }
}

fn walk_schema_object(obj: &mut Map<String, Value>) {
    normalize_nullable(obj);
    normalize_exclusive_bound(obj, "exclusiveMinimum", "minimum");
    normalize_exclusive_bound(obj, "exclusiveMaximum", "maximum");
    normalize_example_to_examples(obj);
    normalize_base64_format(obj);
    for (k, v) in obj.iter_mut() {
        // Inside a Schema: instance-valued keywords carry user data;
        // sub-schema keywords lead to more schemas; everything else is
        // schema-adjacent metadata (xml, discriminator, externalDocs)
        // that we walk generically. `x-*` Specification Extensions
        // hold arbitrary JSON and must round-trip byte-for-byte.
        if is_extension_key(k) {
            continue;
        }
        match k.as_str() {
            "example" | "examples" | "default" | "enum" | "const" => continue,
            "items"
            | "not"
            | "additionalProperties"
            | "contains"
            | "propertyNames"
            | "if"
            | "then"
            | "else"
            | "unevaluatedItems"
            | "unevaluatedProperties" => walk(v, Pos::Schema),
            "allOf" | "anyOf" | "oneOf" | "prefixItems" => walk(v, Pos::Schema),
            "properties" | "patternProperties" | "$defs" | "definitions" | "dependentSchemas" => {
                walk(v, Pos::SchemaMap)
            }
            _ => walk(v, Pos::Generic),
        }
    }
}

fn walk_generic_object(obj: &mut Map<String, Value>) {
    for (k, v) in obj.iter_mut() {
        // `x-*` Specification Extensions are opaque user payloads
        // that must round-trip unchanged.
        if is_extension_key(k) {
            continue;
        }
        match k.as_str() {
            // `schema` lives on Parameter / Header / MediaType; its
            // value is a Schema Object.
            "schema" => walk(v, Pos::Schema),
            // `schemas` is the components-level map of named schemas.
            "schemas" => walk(v, Pos::SchemaMap),
            // `links` is a map of named Link Objects. Both
            // `components.links` and `Response.links` use this key.
            "links" => walk(v, Pos::LinkMap),
            // ExampleObject's instance value, and the
            // example / examples carriers on MediaType / Parameter /
            // Header. Either an instance value, or a map of
            // ExampleObjects (which themselves carry instance values
            // — never schemas). Skip recursion entirely.
            "example" | "examples" | "value" => continue,
            _ => walk(v, Pos::Generic),
        }
    }
}

/// Per OAS / JSON Schema, fields with the `x-` prefix are
/// Specification Extensions: arbitrary user JSON the spec promises to
/// preserve untouched. The walkers skip recursion through these so
/// extension payloads round-trip byte-for-byte.
fn is_extension_key(k: &str) -> bool {
    k.starts_with("x-")
}

/// `type: <T>` + `nullable: true` → `type: [<T>, "null"]`, and a bare
/// `nullable: true` paired with `type: [<T>, …]` adds `"null"` to
/// the array. `nullable: false` (or absent) is a no-op except that
/// the redundant `nullable` field is dropped — v3.1 has no such
/// keyword.
///
/// `nullable: true` on a schema with **no** `type` is a no-op
/// modulo the dropped keyword: in OAS 3.0 a schema without `type`
/// is already unconstrained (it allows any JSON value, including
/// null), so we don't synthesise a `type: ["null"]` — that would
/// flip the semantics from "matches anything" to "null only".
///
/// The input arrives via `serde_json::to_value(&v3_0::Spec)`, so
/// `nullable` is always either absent or a JSON boolean — anything
/// else would have failed v3.0 deserialization upstream.
fn normalize_nullable(obj: &mut Map<String, Value>) {
    let nullable = matches!(obj.remove("nullable"), Some(Value::Bool(true)));
    if !nullable {
        return;
    }
    match obj.remove("type") {
        Some(Value::String(t)) if t != "null" => {
            obj.insert(
                "type".into(),
                Value::Array(vec![Value::String(t), Value::String("null".into())]),
            );
        }
        Some(Value::Array(mut arr)) => {
            if !arr.iter().any(|v| v.as_str() == Some("null")) {
                arr.push(Value::String("null".into()));
            }
            obj.insert("type".into(), Value::Array(arr));
        }
        Some(other) => {
            // Restore unrecognised type values verbatim.
            obj.insert("type".into(), other);
        }
        None => {
            // No `type` to add `null` to. Drop `nullable` (already
            // removed above) and leave the schema typeless: a v3.0
            // schema with no `type` already allows any value.
        }
    }
}

/// Collapse the v3.0 `exclusive<bound>: true` + `<bound>: <n>` pair
/// into v3.1's numeric `exclusive<bound>: <n>`, dropping the
/// now-redundant inclusive bound. `exclusive<bound>: false` is just
/// removed — v3.1 has no boolean form. If the value is already a
/// number (already-3.1-shaped or weird input), leave it alone.
fn normalize_exclusive_bound(
    obj: &mut Map<String, Value>,
    exclusive_key: &str,
    inclusive_key: &str,
) {
    match obj.get(exclusive_key) {
        Some(Value::Bool(true)) => {
            obj.remove(exclusive_key);
            if let Some(bound) = obj.remove(inclusive_key) {
                obj.insert(exclusive_key.to_owned(), bound);
            }
        }
        Some(Value::Bool(false)) => {
            obj.remove(exclusive_key);
        }
        _ => {}
    }
}

/// Single `example` → `examples: [example]`. Schemas that already
/// have an `examples` array win; the deprecated `example` is dropped
/// to match v3.1's "examples is the source of truth" stance. The
/// caller is responsible for invoking this only on Schema Objects
/// (the position-aware walker handles that via [`Pos::Schema`]);
/// `Parameter`, `Header`, and `MediaType` keep their `example` field
/// in v3.1.
fn normalize_example_to_examples(obj: &mut Map<String, Value>) {
    let Some(example) = obj.remove("example") else {
        return;
    };
    if obj.contains_key("examples") {
        return;
    }
    obj.insert("examples".into(), Value::Array(vec![example]));
}

/// `type: string, format: base64` → `type: string,
/// contentEncoding: base64`. v3.1 follows JSON Schema 2020-12, which
/// dropped the OAS-only `format: base64` in favour of the standard
/// `contentEncoding` keyword.
///
/// Accepts both the bare-string `type: "string"` form and the array
/// form `type: ["string", "null"]` produced upstream by
/// [`normalize_nullable`] for nullable string schemas.
fn normalize_base64_format(obj: &mut Map<String, Value>) {
    if !type_includes_string(obj) {
        return;
    }
    if obj.get("format").and_then(|v| v.as_str()) != Some("base64") {
        return;
    }
    obj.remove("format");
    obj.insert("contentEncoding".into(), Value::String("base64".into()));
}

fn type_includes_string(obj: &Map<String, Value>) -> bool {
    match obj.get("type") {
        Some(Value::String(s)) => s == "string",
        Some(Value::Array(arr)) => arr.iter().any(|v| v.as_str() == Some("string")),
        _ => false,
    }
}

/// Walk `content: { <mime>: { schema, … } }` maps and apply the
/// content-aware file-upload rewrites.
///
/// * Inside any `multipart/*` media type, rewrite each property whose
///   schema is `{type: string, format: binary}` into
///   `{type: string, contentMediaType: application/octet-stream}` —
///   `format: binary` was deprecated in 3.1 in favour of the standard
///   `contentMediaType` keyword.
/// * For `application/octet-stream`, replace
///   `{type: string, format: binary}` with the empty schema `{}`,
///   the form the migration guide recommends. `v3_1::Schema` carries
///   a first-class [`crate::v3_1::schema::EmptySchema`] variant that
///   round-trips cleanly: `{}` deserialises as `Schema::Empty`
///   (added in PR #117) instead of being normalised to
///   `{type: object}` by `ObjectSchema::default()`.
fn walk_content_aware(value: &mut Value) {
    walk_content_aware_with(value, /* in_link = */ false);
}

fn walk_content_aware_with(value: &mut Value, in_link: bool) {
    match value {
        Value::Object(obj) => {
            // The `content` key on a Link is a property name, not the
            // OAS Content Map; in fact Link doesn't define `content`
            // at all, but a free-form `Link.requestBody` could
            // contain one. Skip the rewrite entirely while inside a
            // Link.
            if !in_link && let Some(Value::Object(content)) = obj.get_mut("content") {
                rewrite_content_map(content);
            }
            for (k, v) in obj.iter_mut() {
                // `x-*` extensions are opaque — the spec promises they
                // round-trip byte-for-byte. Skip before any other
                // dispatch.
                if is_extension_key(k) {
                    continue;
                }
                if in_link {
                    // Link's `parameters` (map of arbitrary JSON) and
                    // `requestBody` (free-form) are opaque payloads.
                    if matches!(k.as_str(), "parameters" | "requestBody") {
                        continue;
                    }
                    // Other Link fields (server, description) are
                    // walked normally — no Link entries below them.
                    walk_content_aware_with(v, false);
                    continue;
                }
                // Skip instance-valued payloads — a user-supplied
                // example / default / enum / const that happens to
                // contain a `content`-shaped sub-object would
                // otherwise get its file-upload schemas rewritten.
                if matches!(
                    k.as_str(),
                    "example" | "examples" | "default" | "enum" | "const" | "value"
                ) {
                    continue;
                }
                // `links` is a map of named Link objects; transition
                // into Link context for the entries.
                if k == "links" {
                    if let Value::Object(map) = v {
                        for (_, entry) in map.iter_mut() {
                            walk_content_aware_with(entry, true);
                        }
                    }
                    continue;
                }
                walk_content_aware_with(v, false);
            }
        }
        Value::Array(arr) => {
            for v in arr.iter_mut() {
                walk_content_aware_with(v, in_link);
            }
        }
        _ => {}
    }
}

fn rewrite_content_map(content: &mut Map<String, Value>) {
    for (mime, media_type) in content.iter_mut() {
        let Value::Object(media) = media_type else {
            continue;
        };
        let Some(schema) = media.get_mut("schema") else {
            continue;
        };
        // OAS media-type keys may carry parameters (`application/
        // octet-stream; charset=binary`); compare against just the
        // `type/subtype` head. Per RFC 7231 the type / subtype tokens
        // are case-insensitive (`Application/Octet-Stream` is the
        // same media type as `application/octet-stream`), so use
        // ASCII-case-insensitive comparisons throughout.
        let mime_main = mime_main_type(mime);
        if mime_main.eq_ignore_ascii_case("application/octet-stream") {
            // `{type: string, format: binary}` → `{}` (the empty
            // schema, JSON Schema 2020-12's "matches anything"
            // idiom). Routes through `Schema::Empty(EmptySchema)`
            // on the typed deserialisation.
            //
            // Only fires when the schema has nothing besides `type`
            // and `format` — preserving any additional annotations
            // (`description`, `title`, `nullable`, …) is safer than
            // silently dropping them. Schemas with extras stay in
            // their v3.0 form, which is still valid v3.1
            // (`format: binary` is just a JSON Schema annotation).
            if let Value::Object(s) = schema
                && is_string_binary(s)
                && s.len() == 2
            {
                *schema = Value::Object(Map::new());
            }
        } else if is_multipart_mime(mime_main)
            && let Value::Object(s) = schema
        {
            rewrite_string_binary_subschemas(s);
        }
    }
}

/// Return just the `type/subtype` portion of a media-type header
/// value, stripping any RFC-7231 parameters after the first `;`.
fn mime_main_type(mime: &str) -> &str {
    mime.split(';').next().unwrap_or(mime).trim()
}

fn is_multipart_mime(mime: &str) -> bool {
    // Type/subtype tokens are ASCII-case-insensitive per RFC 7231.
    // `mime` ultimately comes from arbitrary user JSON keys and may
    // contain non-ASCII / multi-byte UTF-8, so use the non-panicking
    // `str::get` form rather than slicing at an arbitrary byte
    // offset.
    let prefix = "multipart/";
    mime.get(..prefix.len())
        .is_some_and(|h| h.eq_ignore_ascii_case(prefix))
}

fn is_string_binary(schema: &Map<String, Value>) -> bool {
    schema.get("type").and_then(|v| v.as_str()) == Some("string")
        && schema.get("format").and_then(|v| v.as_str()) == Some("binary")
}

/// Recursively walk a multipart schema tree and rewrite every
/// `{type: string, format: binary}` subschema to
/// `{type: string, contentMediaType: application/octet-stream}`.
///
/// v2/v3.0's `format: binary` annotation can sit anywhere inside a
/// multipart schema — directly on a property, on `items` of an array
/// property, on a nested `properties.<name>` schema, on `allOf`
/// branches, etc. The walker visits all of them; instance-valued
/// payloads (`example`, `default`, `enum`, …) are skipped so a
/// user-supplied example whose shape happens to mirror a string-binary
/// schema isn't mutated.
fn rewrite_string_binary_subschemas(schema: &mut Map<String, Value>) {
    if is_string_binary(schema) {
        schema.remove("format");
        schema.insert(
            "contentMediaType".into(),
            Value::String("application/octet-stream".into()),
        );
        // A string-binary schema is a leaf — no schema substructure
        // to recurse into.
        return;
    }
    for (k, v) in schema.iter_mut() {
        match k.as_str() {
            // Schema-level instance keys carry user data, never schemas.
            "example" | "examples" | "default" | "enum" | "const" => continue,
            // Sub-schema keys (single nested schema).
            "items"
            | "not"
            | "additionalProperties"
            | "contains"
            | "propertyNames"
            | "if"
            | "then"
            | "else"
            | "unevaluatedItems"
            | "unevaluatedProperties" => {
                if let Value::Object(s) = v {
                    rewrite_string_binary_subschemas(s);
                }
            }
            // Sub-schema arrays.
            "allOf" | "anyOf" | "oneOf" | "prefixItems" => {
                if let Value::Array(arr) = v {
                    for entry in arr.iter_mut() {
                        if let Value::Object(s) = entry {
                            rewrite_string_binary_subschemas(s);
                        }
                    }
                }
            }
            // Schema-by-name maps.
            "properties" | "patternProperties" | "$defs" | "definitions" | "dependentSchemas" => {
                if let Value::Object(map) = v {
                    for (_, entry) in map.iter_mut() {
                        if let Value::Object(s) = entry {
                            rewrite_string_binary_subschemas(s);
                        }
                    }
                }
            }
            _ => {}
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::v3_0::spec::Spec as V30Spec;
    use crate::v3_1::spec::Spec as V31Spec;
    use crate::validation::{IGNORE_UNUSED, Options, Validate};

    fn v30_from_json(s: &str) -> V30Spec {
        serde_json::from_str(s).expect("v3.0 spec parses")
    }

    fn convert(raw: &str) -> Value {
        let v30: V30Spec = v30_from_json(raw);
        let v31: V31Spec = v30.into();
        serde_json::to_value(&v31).unwrap()
    }

    #[test]
    fn openapi_version_lifted() {
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {}
        }"##;
        let value = convert(raw);
        assert_eq!(value["openapi"], "3.1.2");
    }

    #[test]
    fn nullable_promotes_type_into_array() {
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "MaybeName": {"type": "string", "nullable": true},
                    "MaybeBool": {"type": "boolean"}
                }
            }
        }"##;
        let value = convert(raw);
        let maybe_name = &value["components"]["schemas"]["MaybeName"];
        assert_eq!(maybe_name["type"], serde_json::json!(["string", "null"]));
        assert!(
            maybe_name.get("nullable").is_none(),
            "nullable keyword removed"
        );
        // Non-nullable schema is untouched.
        let maybe_bool = &value["components"]["schemas"]["MaybeBool"];
        assert_eq!(maybe_bool["type"], "boolean");
    }

    #[test]
    fn nullable_without_type_stays_typeless() {
        // OAS 3.0 `nullable: true` only adds `null` to an explicit
        // `type`. A schema with no `type` is already unconstrained
        // (allows any value including null), so the conversion must
        // drop `nullable` without synthesising a `type: ["null"]` —
        // that would change semantics from "any" to "null only".
        //
        // The typed `From<v3_0::Spec>` path can't actually deliver a
        // typeless schema to `normalize_nullable` (v3_0's
        // `ObjectSchema` re-serialises an explicit `type: object`),
        // so exercise the walker directly on hand-built JSON to pin
        // the defensive behaviour down.
        let mut v: Value = serde_json::json!({"nullable": true, "description": "free-form"});
        super::walk(&mut v, super::Pos::Schema);
        let free = &v;
        assert!(
            free.get("type").is_none(),
            "no `type` should be synthesised, got {free}"
        );
        assert!(free.get("nullable").is_none(), "nullable removed");
        assert_eq!(free["description"], "free-form");
    }

    #[test]
    fn nullable_string_with_constraints_round_trips_via_extensions() {
        // A nullable string with constraints (`minLength`, `pattern`,
        // `enum`) becomes a `MultiSchema` whose first-class fields
        // are very limited; the type-specific keywords are preserved
        // through the schema extensions catch-all so they round-trip
        // unchanged at the JSON level. Pin this down so the limitation
        // is visible — adding first-class fields to MultiSchema is a
        // separate piece of work.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Slug": {
                        "type": "string",
                        "nullable": true,
                        "minLength": 3,
                        "maxLength": 32,
                        "pattern": "^[a-z][a-z0-9-]*$",
                        "enum": ["alpha", "beta"]
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let slug = &value["components"]["schemas"]["Slug"];
        assert_eq!(slug["type"], serde_json::json!(["string", "null"]));
        assert_eq!(slug["minLength"], 3);
        assert_eq!(slug["maxLength"], 32);
        assert_eq!(slug["pattern"], "^[a-z][a-z0-9-]*$");
        assert_eq!(slug["enum"], serde_json::json!(["alpha", "beta"]));
    }

    #[test]
    fn nullable_object_with_properties_round_trips_via_extensions() {
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Pet": {
                        "type": "object",
                        "nullable": true,
                        "required": ["id"],
                        "properties": {
                            "id": {"type": "integer"},
                            "name": {"type": "string", "nullable": true}
                        }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let pet = &value["components"]["schemas"]["Pet"];
        assert_eq!(pet["type"], serde_json::json!(["object", "null"]));
        assert_eq!(pet["required"], serde_json::json!(["id"]));
        assert_eq!(pet["properties"]["id"]["type"], "integer");
        // Recursive `nullable` rewrite reaches nested properties too.
        assert_eq!(
            pet["properties"]["name"]["type"],
            serde_json::json!(["string", "null"])
        );
    }

    #[test]
    fn nullable_array_with_items_round_trips_via_extensions() {
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Tags": {
                        "type": "array",
                        "nullable": true,
                        "minItems": 1,
                        "uniqueItems": true,
                        "items": {"type": "string"}
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let tags = &value["components"]["schemas"]["Tags"];
        assert_eq!(tags["type"], serde_json::json!(["array", "null"]));
        assert_eq!(tags["minItems"], 1);
        assert_eq!(tags["uniqueItems"], true);
        assert_eq!(tags["items"]["type"], "string");
    }

    #[test]
    fn exclusive_bound_collapses_to_number() {
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Pos": {
                        "type": "integer",
                        "minimum": 0,
                        "exclusiveMinimum": true,
                        "maximum": 100,
                        "exclusiveMaximum": true
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let pos = &value["components"]["schemas"]["Pos"];
        assert_eq!(pos["exclusiveMinimum"], 0);
        assert_eq!(pos["exclusiveMaximum"], 100);
        assert!(pos.get("minimum").is_none(), "redundant minimum dropped");
        assert!(pos.get("maximum").is_none(), "redundant maximum dropped");
    }

    #[test]
    fn exclusive_bound_false_is_just_removed() {
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "InclOnly": {
                        "type": "integer",
                        "minimum": 0,
                        "exclusiveMinimum": false
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let s = &value["components"]["schemas"]["InclOnly"];
        assert_eq!(s["minimum"], 0);
        assert!(s.get("exclusiveMinimum").is_none());
    }

    #[test]
    fn schema_example_becomes_examples_array() {
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Pet": {"type": "string", "example": "fedora"}
                }
            }
        }"##;
        let value = convert(raw);
        let pet = &value["components"]["schemas"]["Pet"];
        assert_eq!(pet["examples"], serde_json::json!(["fedora"]));
        assert!(pet.get("example").is_none());
    }

    #[test]
    fn schema_example_payload_is_preserved_byte_for_byte() {
        // The example value is instance data — it can legitimately
        // contain keys like `nullable`, `type`, `exclusiveMinimum`
        // without being a schema. None of the schema rewrites should
        // touch it.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Cfg": {
                        "type": "object",
                        "example": {
                            "nullable": true,
                            "type": "string",
                            "exclusiveMinimum": true,
                            "minimum": 0,
                            "format": "base64"
                        }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        // The example value migrates to an `examples` array (per the
        // schema-level `example → examples` rewrite) but its contents
        // round-trip verbatim.
        let payload = &value["components"]["schemas"]["Cfg"]["examples"][0];
        assert_eq!(payload["nullable"], true);
        assert_eq!(payload["type"], "string");
        assert_eq!(payload["exclusiveMinimum"], true);
        assert_eq!(payload["minimum"], 0);
        assert_eq!(payload["format"], "base64");
    }

    #[test]
    fn schema_default_payload_is_preserved_byte_for_byte() {
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Cfg": {
                        "type": "object",
                        "default": {"type": "string", "nullable": true}
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let default = &value["components"]["schemas"]["Cfg"]["default"];
        assert_eq!(default["type"], "string");
        assert_eq!(default["nullable"], true);
    }

    #[test]
    fn property_named_example_is_walked_as_a_subschema() {
        // The schema rewrites must reach a sub-schema whose property
        // name happens to be `example` (or any other instance-valued
        // keyword). The instance-key skip is gated on the parent
        // being a schema, so a `properties` map does NOT skip its
        // child entries.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Cfg": {
                        "type": "object",
                        "properties": {
                            "example": {"type": "string", "nullable": true},
                            "default": {"type": "integer", "nullable": true}
                        }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let props = &value["components"]["schemas"]["Cfg"]["properties"];
        assert_eq!(
            props["example"]["type"],
            serde_json::json!(["string", "null"])
        );
        assert_eq!(
            props["default"]["type"],
            serde_json::json!(["integer", "null"])
        );
    }

    #[test]
    fn media_type_examples_payload_is_preserved_byte_for_byte() {
        // `MediaType.examples` is a map of named ExampleObjects. The
        // ExampleObject's `value` field is instance data; recursing
        // through the `examples` key would mutate it.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/x": {
                    "get": {
                        "responses": {
                            "200": {
                                "description": "ok",
                                "content": {
                                    "application/json": {
                                        "schema": {"type": "object"},
                                        "examples": {
                                            "trap": {
                                                "value": {
                                                    "type": "string",
                                                    "nullable": true,
                                                    "format": "base64"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let trap = &value["paths"]["/x"]["get"]["responses"]["200"]["content"]["application/json"]
            ["examples"]["trap"]["value"];
        assert_eq!(trap["type"], "string");
        assert_eq!(trap["nullable"], true);
        assert_eq!(trap["format"], "base64");
    }

    #[test]
    fn parameter_example_is_kept_as_is() {
        // `Parameter` keeps its `example` field in 3.1; only Schema's
        // `example` migrates to `examples`.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/items": {
                    "get": {
                        "parameters": [{
                            "in": "query",
                            "name": "limit",
                            "schema": {"type": "integer"},
                            "example": 10
                        }],
                        "responses": { "200": { "description": "ok" } }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let p = &value["paths"]["/items"]["get"]["parameters"][0];
        assert_eq!(p["example"], 10);
    }

    #[test]
    fn octet_stream_binary_schema_becomes_empty_schema() {
        // `{type: string, format: binary}` under
        // `application/octet-stream` is the v3.0 idiom for "raw bytes
        // body". v3.1's equivalent is the empty schema `{}` (the
        // form the official upgrade guide recommends), routed
        // through the `Schema::Empty(EmptySchema)` variant so it
        // round-trips byte-for-byte.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/upload": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "application/octet-stream": {
                                    "schema": {"type": "string", "format": "binary"}
                                }
                            }
                        },
                        "responses": { "200": { "description": "ok" } }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let schema = &value["paths"]["/upload"]["post"]["requestBody"]["content"]["application/octet-stream"]
            ["schema"];
        // The result is the literal empty object `{}`, neither
        // `{type: object}` (the old ObjectSchema-default artefact)
        // nor `true` (the boolean-schema fallback).
        assert!(schema.is_object());
        assert!(
            schema.as_object().unwrap().is_empty(),
            "octet-stream schema should be `{{}}`, got {schema}"
        );
    }

    #[test]
    fn octet_stream_with_non_binary_schema_is_kept() {
        // A typed schema (e.g. base64 text) under
        // `application/octet-stream` is not the binary idiom — keep it
        // as-is (only `format: base64` flips to `contentEncoding`).
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/upload": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "application/octet-stream": {
                                    "schema": {"type": "string", "format": "base64"}
                                }
                            }
                        },
                        "responses": { "200": { "description": "ok" } }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let schema = &value["paths"]["/upload"]["post"]["requestBody"]["content"]["application/octet-stream"]
            ["schema"];
        assert_eq!(schema["type"], "string");
        assert_eq!(schema["contentEncoding"], "base64");
    }

    #[test]
    fn multipart_binary_property_uses_content_media_type() {
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/upload": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "multipart/form-data": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "file": {"type": "string", "format": "binary"},
                                            "name": {"type": "string"}
                                        }
                                    }
                                }
                            }
                        },
                        "responses": { "200": { "description": "ok" } }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let props = &value["paths"]["/upload"]["post"]["requestBody"]["content"]["multipart/form-data"]
            ["schema"]["properties"];
        let file = &props["file"];
        assert_eq!(file["type"], "string");
        assert_eq!(file["contentMediaType"], "application/octet-stream");
        assert!(file.get("format").is_none(), "format dropped");
        // Non-binary properties stay as-is.
        assert_eq!(props["name"]["type"], "string");
    }

    #[test]
    fn multipart_binary_array_items_uses_content_media_type() {
        // `format: binary` can sit on `items` of an array property
        // (multipart upload of multiple files). v3.1 expects the
        // `contentMediaType` rewrite there too — not just on
        // top-level properties.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/upload": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "multipart/form-data": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "files": {
                                                "type": "array",
                                                "items": {"type": "string", "format": "binary"}
                                            }
                                        }
                                    }
                                }
                            }
                        },
                        "responses": { "200": { "description": "ok" } }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let items = &value["paths"]["/upload"]["post"]["requestBody"]["content"]["multipart/form-data"]
            ["schema"]["properties"]["files"]["items"];
        assert_eq!(items["type"], "string");
        assert_eq!(items["contentMediaType"], "application/octet-stream");
        assert!(items.get("format").is_none(), "format dropped on items too");
    }

    #[test]
    fn multipart_nested_binary_property_uses_content_media_type() {
        // A binary deep inside a nested object schema is rewritten too.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/upload": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "multipart/form-data": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "envelope": {
                                                "type": "object",
                                                "properties": {
                                                    "blob": {"type": "string", "format": "binary"}
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        },
                        "responses": { "200": { "description": "ok" } }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let blob = &value["paths"]["/upload"]["post"]["requestBody"]["content"]["multipart/form-data"]
            ["schema"]["properties"]["envelope"]["properties"]["blob"];
        assert_eq!(blob["contentMediaType"], "application/octet-stream");
        assert!(blob.get("format").is_none());
    }

    #[test]
    fn content_aware_walk_skips_example_payload() {
        // A schema whose `example` payload contains a `content` map
        // and binary schemas must NOT be rewritten — that's
        // user-supplied instance data, not an OAS Content map.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Doc": {
                        "type": "object",
                        "example": {
                            "content": {
                                "application/octet-stream": {
                                    "schema": {"type": "string", "format": "binary"}
                                },
                                "multipart/form-data": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "f": {"type": "string", "format": "binary"}
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let payload = &value["components"]["schemas"]["Doc"]["examples"][0];
        // The example value's nested binary schema is preserved
        // verbatim — no octet-stream-empty rewrite, no
        // contentMediaType rewrite.
        let octet_schema = &payload["content"]["application/octet-stream"]["schema"];
        assert_eq!(octet_schema["type"], "string");
        assert_eq!(octet_schema["format"], "binary");
        let multipart_field =
            &payload["content"]["multipart/form-data"]["schema"]["properties"]["f"];
        assert_eq!(multipart_field["type"], "string");
        assert_eq!(multipart_field["format"], "binary");
        assert!(
            multipart_field.get("contentMediaType").is_none(),
            "example payload must not gain contentMediaType"
        );
    }

    #[test]
    fn non_ascii_media_type_key_does_not_panic() {
        // `is_multipart_mime` previously sliced the key at a
        // hard-coded byte offset (`mime[..prefix.len()]`). Map keys
        // can hold arbitrary UTF-8, so a non-ASCII / multi-byte head
        // would panic. Use `str::get` instead — the conversion must
        // tolerate (and pass through) arbitrary keys.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/x": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "🦀/binary": {
                                    "schema": {"type": "string", "format": "binary"}
                                }
                            }
                        },
                        "responses": { "200": { "description": "ok" } }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        // Key is preserved verbatim and not rewritten — neither
        // multipart nor octet-stream applies.
        let schema = &value["paths"]["/x"]["post"]["requestBody"]["content"]["🦀/binary"]["schema"];
        assert_eq!(schema["type"], "string");
        assert_eq!(schema["format"], "binary");
    }

    #[test]
    fn media_type_match_is_case_insensitive() {
        // RFC 7231 type/subtype tokens are ASCII-case-insensitive,
        // so `Application/Octet-Stream` and `Multipart/Form-Data`
        // must trigger the same rewrites as their lowercase forms.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/upload": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "Application/Octet-Stream": {
                                    "schema": {"type": "string", "format": "binary"}
                                },
                                "Multipart/Form-Data": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "f": {"type": "string", "format": "binary"}
                                        }
                                    }
                                }
                            }
                        },
                        "responses": { "200": { "description": "ok" } }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        // Octet-stream with mixed-case key still becomes `{}`.
        let octet = &value["paths"]["/upload"]["post"]["requestBody"]["content"]["Application/Octet-Stream"]
            ["schema"];
        assert!(octet.is_object());
        assert!(octet.as_object().unwrap().is_empty());
        // Multipart with mixed-case key still rewrites the binary field.
        let f = &value["paths"]["/upload"]["post"]["requestBody"]["content"]["Multipart/Form-Data"]
            ["schema"]["properties"]["f"];
        assert_eq!(f["type"], "string");
        assert_eq!(f["contentMediaType"], "application/octet-stream");
        assert!(f.get("format").is_none());
    }

    #[test]
    fn octet_stream_with_media_type_parameters_is_rewritten() {
        // RFC 7231 lets a media-type carry parameters
        // (`application/octet-stream; charset=binary`). The rewrite
        // must compare against the `type/subtype` head, not the full
        // string, so parameterised keys still get the `{}` migration.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/upload": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "application/octet-stream; charset=binary": {
                                    "schema": {"type": "string", "format": "binary"}
                                }
                            }
                        },
                        "responses": { "200": { "description": "ok" } }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let schema = &value["paths"]["/upload"]["post"]["requestBody"]["content"]["application/octet-stream; charset=binary"]
            ["schema"];
        assert!(schema.is_object());
        assert!(schema.as_object().unwrap().is_empty());
    }

    #[test]
    fn multipart_with_media_type_parameters_rewrites_binary_props() {
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/upload": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "multipart/form-data; boundary=ABCD": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "file": {"type": "string", "format": "binary"}
                                        }
                                    }
                                }
                            }
                        },
                        "responses": { "200": { "description": "ok" } }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let file = &value["paths"]["/upload"]["post"]["requestBody"]["content"]["multipart/form-data; boundary=ABCD"]
            ["schema"]["properties"]["file"];
        assert_eq!(file["type"], "string");
        assert_eq!(file["contentMediaType"], "application/octet-stream");
        assert!(file.get("format").is_none());
    }

    #[test]
    fn octet_stream_binary_with_extra_fields_is_preserved() {
        // A schema like `{type: string, format: binary, description}`
        // expresses more than a bare byte-stream — preserve it as-is
        // instead of dropping the description by replacing with `{}`.
        // The v3.0 form is still valid v3.1 (`format: binary` is a
        // JSON Schema annotation, not a constraint).
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/upload": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "application/octet-stream": {
                                    "schema": {
                                        "type": "string",
                                        "format": "binary",
                                        "description": "the document bytes"
                                    }
                                }
                            }
                        },
                        "responses": { "200": { "description": "ok" } }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let schema = &value["paths"]["/upload"]["post"]["requestBody"]["content"]["application/octet-stream"]
            ["schema"];
        assert_eq!(schema["type"], "string");
        assert_eq!(schema["format"], "binary");
        assert_eq!(schema["description"], "the document bytes");
    }

    #[test]
    fn link_parameters_and_request_body_are_opaque_to_walkers() {
        // `Link.parameters` is a `BTreeMap<String, Value>` and
        // `Link.requestBody` is a `Value` — both hold arbitrary JSON
        // (runtime expressions, free-form payloads). Neither walker
        // should rewrite their contents even if those contents
        // contain schema-shaped or content-shaped JSON.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {
                "/x": {
                    "get": {
                        "operationId": "getX",
                        "responses": {
                            "200": {
                                "description": "ok",
                                "links": {
                                    "trap": {
                                        "operationId": "getX",
                                        "parameters": {
                                            "p": {
                                                "schema": {
                                                    "type": "string",
                                                    "nullable": true,
                                                    "format": "base64"
                                                }
                                            }
                                        },
                                        "requestBody": {
                                            "content": {
                                                "application/octet-stream": {
                                                    "schema": {"type": "string", "format": "binary"}
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let link = &value["paths"]["/x"]["get"]["responses"]["200"]["links"]["trap"];
        // Link.parameters payload is preserved verbatim (no
        // nullable→type-array, no format→contentEncoding).
        let p_schema = &link["parameters"]["p"]["schema"];
        assert_eq!(p_schema["type"], "string");
        assert_eq!(p_schema["nullable"], true);
        assert_eq!(p_schema["format"], "base64");
        assert!(p_schema.get("contentEncoding").is_none());
        // Link.requestBody payload is preserved verbatim (no
        // octet-stream binary→{}, no contentMediaType rewrite).
        let body_schema = &link["requestBody"]["content"]["application/octet-stream"]["schema"];
        assert_eq!(body_schema["type"], "string");
        assert_eq!(body_schema["format"], "binary");
    }

    #[test]
    fn x_extension_payloads_are_opaque_to_walkers() {
        // `x-*` Specification Extensions must round-trip byte-for-byte
        // even when they carry schema- or content-shaped sub-objects.
        // Both the schema rewrites (nullable, exclusive*, base64,
        // example→examples) and the content-map rewrite (octet-stream,
        // multipart binary) must skip recursion through extensions.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Cfg": {
                        "type": "object",
                        "x-json-schema": {
                            "type": "string",
                            "nullable": true,
                            "format": "base64",
                            "example": "abc"
                        },
                        "x-vendor-content": {
                            "content": {
                                "application/octet-stream": {
                                    "schema": {"type": "string", "format": "binary"}
                                },
                                "multipart/form-data": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "f": {"type": "string", "format": "binary"}
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let cfg = &value["components"]["schemas"]["Cfg"];
        // x-json-schema's payload survives intact.
        let xjs = &cfg["x-json-schema"];
        assert_eq!(xjs["type"], "string");
        assert_eq!(xjs["nullable"], true);
        assert_eq!(xjs["format"], "base64");
        assert_eq!(xjs["example"], "abc");
        assert!(xjs.get("contentEncoding").is_none());
        assert!(xjs.get("examples").is_none());
        // x-vendor-content's nested binary schemas survive intact too.
        let xvc = &cfg["x-vendor-content"];
        let octet = &xvc["content"]["application/octet-stream"]["schema"];
        assert_eq!(octet["type"], "string");
        assert_eq!(octet["format"], "binary");
        let multipart_field = &xvc["content"]["multipart/form-data"]["schema"]["properties"]["f"];
        assert_eq!(multipart_field["type"], "string");
        assert_eq!(multipart_field["format"], "binary");
        assert!(multipart_field.get("contentMediaType").is_none());
    }

    #[test]
    fn nullable_string_with_base64_format_gets_content_encoding() {
        // `nullable: true` lifts `type: "string"` to `["string", "null"]`
        // before the base64 rewrite runs. The base64 rewrite must
        // accept that array form so the `format: base64` →
        // `contentEncoding: base64` migration applies consistently.
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Token": {
                        "type": "string",
                        "format": "base64",
                        "nullable": true
                    }
                }
            }
        }"##;
        let value = convert(raw);
        let token = &value["components"]["schemas"]["Token"];
        assert_eq!(token["type"], serde_json::json!(["string", "null"]));
        assert_eq!(token["contentEncoding"], "base64");
        assert!(token.get("format").is_none());
    }

    #[test]
    fn base64_format_becomes_content_encoding() {
        let raw = r##"{
            "openapi": "3.0.4",
            "info": { "title": "t", "version": "1" },
            "paths": {},
            "components": {
                "schemas": {
                    "Token": {"type": "string", "format": "base64"}
                }
            }
        }"##;
        let value = convert(raw);
        let token = &value["components"]["schemas"]["Token"];
        assert_eq!(token["type"], "string");
        assert_eq!(token["contentEncoding"], "base64");
        assert!(token.get("format").is_none());
    }

    /// Sweep every checked-in v3.0 fixture; each should convert and
    /// validate clean as v3.1 with the lenient validator options used
    /// by the v2→v3.0 fixture sweep.
    #[test]
    fn all_v3_0_fixtures_convert_to_valid_v3_1() {
        let fixtures: &[(&str, &str)] = &[
            (
                "petstore",
                include_str!("../../tests/v3_0_data/petstore.json"),
            ),
            (
                "petstore-expanded",
                include_str!("../../tests/v3_0_data/petstore-expanded.json"),
            ),
            (
                "api-with-examples",
                include_str!("../../tests/v3_0_data/api-with-examples.json"),
            ),
            (
                "callback-example",
                include_str!("../../tests/v3_0_data/callback-example.json"),
            ),
            (
                "link-example",
                include_str!("../../tests/v3_0_data/link-example.json"),
            ),
        ];
        let opts = Options::new() | Options::IgnoreMissingTags | IGNORE_UNUSED;
        for (name, raw) in fixtures {
            let v30: V30Spec =
                serde_json::from_str(raw).unwrap_or_else(|e| panic!("{name}: parse: {e}"));
            let v31: V31Spec = v30.into();
            assert_eq!(v31.openapi.as_str(), "3.1.2", "{name} openapi version");
            if let Err(e) = v31.validate(opts) {
                panic!("{name}: converted spec did not validate cleanly:\n{e}");
            }
        }
    }
}