rsdo 0.1.20251018

A Rust client library for the DigitalOcean API v2
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
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
//! # DigitalOcean OpenAPI Client Generator
//!
//! This build script generates a type-safe Rust client for the DigitalOcean API by processing
//! their OpenAPI specification. The generation process involves four complex stages:
//!
//! ## 1. Download & Extract (download_openapi_spec)
//! Downloads the latest OpenAPI spec from GitHub and extracts it to the build output directory.
//!
//! ## 2. YAML Reference Resolution (RefResolver)
//! The DigitalOcean OpenAPI spec is split across 100+ YAML files with references like:
//! - `$ref: "#/definitions/droplet"` (internal references within a file)
//! - `$ref: "shared/droplet.yml#/properties/id"` (external file references)
//! - `$ref: "../../../shared/attributes.yml"` (relative paths that may be incorrect)
//!
//! This stage recursively resolves all references into a single, self-contained spec.
//!
//! ## 3. Specification Fixups
//! The DigitalOcean spec has several issues that prevent successful code generation:
//!
//! ### a) Missing Definitions (add_missing_definitions)
//! Common types referenced but not defined (pagination links, K8s taints, etc.)
//!
//! ### b) Unresolved References (clean_unresolved_refs)
//! References that couldn't be resolved are replaced with fallback schemas.
//!
//! ### c) Documentation Sanitization (sanitize_documentation)
//! **CRITICAL FOR DOCTESTS**: OpenAPI descriptions contain shell commands, kubectl examples,
//! and HTTP responses that Rust's documentation system tries to compile as Rust code.
//! This causes doctest failures. We mark all non-Rust code blocks as ```text```.
//!
//! ### d) Response Type Deduplication (deduplicate_response_types)
//! **CRITICAL FOR PROGENITOR**: Progenitor 0.11.0 has an assertion that fails when an API
//! operation has multiple success responses (e.g., 200, 201, 204). We simplify each operation
//! to have only one response to avoid this limitation.
//!
//! ## 4. Code Generation (generate_client_code)
//! Uses the progenitor library to generate Rust code from the processed OpenAPI spec.
//! The workflow: YAML → JSON → OpenAPI struct → proc-macro tokens → syn AST → formatted code
//!
//! ## Fallback Strategy
//! If any stage fails, a minimal stub client is generated instead of failing the build.
//! This allows the crate to compile even if the OpenAPI spec is temporarily unavailable.

use serde_yaml::Value;
use std::{
    collections::HashMap,
    env, fs,
    path::{Path, PathBuf},
};

/// Build script entry point: orchestrates the client generation pipeline.
///
/// ## Process Flow:
/// 1. Download OpenAPI spec from GitHub (if not already cached)
/// 2. Process spec with full reference resolution and fixups
/// 3. Generate Rust client code using progenitor
/// 4. Write generated code to OUT_DIR/codegen.rs
///
/// ## Error Handling:
/// If any stage fails, writes a fallback stub client instead of failing the build.
/// This ensures the crate can still compile, though with limited functionality.
fn main() {
    let out_dir = env::var("OUT_DIR").unwrap();
    let spec_dir = Path::new(&out_dir).join("digitalocean-openapi");
    let output_path = Path::new(&out_dir).join("codegen.rs");

    // Download and extract OpenAPI specification
    if !spec_dir.exists() {
        if let Err(e) = download_openapi_spec(&spec_dir) {
            eprintln!("Failed to download OpenAPI spec: {}", e);
            println!("cargo:warning=Failed to download OpenAPI spec, using fallback stub");
            write_stub_client(&output_path);
            return;
        }
    }

    // Process the OpenAPI specification with full reference resolution
    let spec_path = spec_dir.join("specification/DigitalOcean-public.v2.yaml");
    match process_openapi_spec(&spec_path) {
        Ok(resolved_spec) => {
            // Generate client using progenitor
            match generate_client_code(&resolved_spec) {
                Ok(generated_code) => {
                    fs::write(&output_path, generated_code)
                        .unwrap_or_else(|e| panic!("Failed to write generated client code: {}", e));
                    println!(
                        "Generated DigitalOcean client code at: {}",
                        output_path.display()
                    );
                }
                Err(e) => {
                    eprintln!("Failed to generate client code: {}", e);
                    println!(
                        "cargo:warning=Failed to generate client code: {}, using fallback stub",
                        e
                    );
                    write_stub_client(&output_path);
                }
            }
        }
        Err(e) => {
            eprintln!("Failed to process OpenAPI spec: {}", e);
            println!(
                "cargo:warning=Failed to process OpenAPI spec: {}, using fallback stub",
                e
            );
            write_stub_client(&output_path);
        }
    }
}

/// Downloads and extracts the latest DigitalOcean OpenAPI specification from GitHub.
///
/// ## Source
/// Downloads from: https://github.com/digitalocean/openapi (main branch)
///
/// ## Process:
/// 1. Downloads ZIP archive of the entire repo
/// 2. Extracts all files to `spec_dir`
/// 3. Strips the "openapi-main/" prefix from paths (GitHub ZIP artifact)
///
/// ## File Structure:
/// After extraction, the spec is located at:
/// ```text
/// spec_dir/
///   specification/
///     DigitalOcean-public.v2.yaml  (main spec file)
///     description.yml
///     resources/                    (100+ YAML files)
///     shared/                       (common definitions)
/// ```
///
/// ## Caching:
/// This function is only called if `spec_dir` doesn't exist, so the spec is
/// downloaded once per clean build.
fn download_openapi_spec(spec_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
    println!("Downloading DigitalOcean OpenAPI specification...");

    let url = "https://github.com/digitalocean/openapi/archive/refs/heads/main.zip";
    let response = reqwest::blocking::get(url)?;

    if !response.status().is_success() {
        return Err(format!("Failed to download: HTTP {}", response.status()).into());
    }

    let bytes = response.bytes()?;
    let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes))?;

    // Extract all files
    for i in 0..zip.len() {
        let mut file = zip.by_index(i)?;
        let enclosed_name = file.enclosed_name().ok_or("Invalid file path in zip")?;
        let outpath = spec_dir.join(
            enclosed_name
                .strip_prefix("openapi-main/")
                .unwrap_or(&enclosed_name),
        );

        if file.name().ends_with('/') {
            fs::create_dir_all(&outpath)?;
        } else {
            if let Some(p) = outpath.parent() {
                fs::create_dir_all(p)?;
            }
            let mut outfile = fs::File::create(&outpath)?;
            std::io::copy(&mut file, &mut outfile)?;
        }
    }

    println!("Downloaded OpenAPI specification successfully");
    Ok(())
}

/// Processes the OpenAPI specification by resolving all YAML references.
///
/// This is the entry point for the reference resolution pipeline. It creates
/// a `RefResolver` and runs the complete resolution process.
///
/// ## Input:
/// - `spec_path`: Path to DigitalOcean-public.v2.yaml
///
/// ## Output:
/// - A single, self-contained YAML Value with all references resolved
///
/// See `RefResolver::resolve_refs` for details on the multi-stage resolution process.
fn process_openapi_spec(spec_path: &Path) -> Result<Value, Box<dyn std::error::Error>> {
    println!("Processing OpenAPI specification with reference resolution...");

    let spec_dir = spec_path.parent().ok_or("Invalid spec path")?;
    let mut resolver = RefResolver::new(spec_dir);

    let root_spec = resolver.load_yaml_file(spec_path)?;
    let resolved_spec = resolver.resolve_refs(root_spec)?;

    println!("Successfully resolved all OpenAPI references");
    Ok(resolved_spec)
}

/// Resolves OpenAPI `$ref` directives across multiple YAML files.
///
/// ## The Problem
/// The DigitalOcean OpenAPI spec uses `$ref` extensively to reference definitions
/// across 100+ separate YAML files. References can be:
///
/// 1. **Internal** (within the same file):
///    ```yaml
///    $ref: "#/definitions/droplet"
///    ```
///
/// 2. **External** (to another file):
///    ```yaml
///    $ref: "shared/droplet.yml#/properties/id"
///    ```
///
/// 3. **Relative** (with complex paths):
///    ```yaml
///    $ref: "../../../shared/attributes/tags.yml"
///    ```
///
/// These must all be resolved to actual schema definitions for progenitor to work.
///
/// ## How It Works
/// - Traverses the entire YAML tree recursively
/// - Whenever it finds a `$ref` key, it:
///   1. Loads the referenced file (if external)
///   2. Navigates to the specific path using JSON Pointer (RFC 6901)
///   3. Replaces the `$ref` with the actual definition
/// - Uses caching to avoid re-loading files
/// - Detects circular references to prevent infinite loops
///
/// ## Fields
/// - `spec_dir`: Base directory containing the specification files
/// - `cache`: Loaded YAML files (path → parsed Value) to avoid re-reading
/// - `resolving`: Set of files currently being resolved (for cycle detection)
/// - `root_spec`: The main specification, used for resolving internal refs
struct RefResolver {
    spec_dir: PathBuf,
    cache: HashMap<PathBuf, Value>,
    resolving: std::collections::HashSet<PathBuf>,
    root_spec: Option<Value>,
}

impl RefResolver {
    fn new(spec_dir: &Path) -> Self {
        Self {
            spec_dir: spec_dir.to_path_buf(),
            cache: HashMap::new(),
            resolving: std::collections::HashSet::new(),
            root_spec: None,
        }
    }

    /// Loads and parses a YAML file, with caching and error handling.
    ///
    /// ## Special Handling:
    /// - Caches parsed files to avoid re-loading
    /// - Fixes the integer `18446744073709552000` which is out of i64 range
    ///   (found in some DigitalOcean specs) by replacing it with i64::MAX
    fn load_yaml_file(&mut self, path: &Path) -> Result<Value, Box<dyn std::error::Error>> {
        if let Some(cached) = self.cache.get(path) {
            return Ok(cached.clone());
        }

        let content = match fs::read_to_string(path) {
            Ok(content) => content,
            Err(e) => {
                return Err(format!("Failed to read file '{}': {}", path.display(), e).into());
            }
        };
        let value: Value = match serde_yaml::from_str(&content) {
            Ok(v) => v,
            Err(e) => {
                // Try to handle specific parsing issues
                if content.contains("18446744073709552000") {
                    // Replace the problematic large integer with a smaller one
                    let fixed_content =
                        content.replace("18446744073709552000", "18446744073709551615");
                    serde_yaml::from_str(&fixed_content)?
                } else {
                    return Err(e.into());
                }
            }
        };
        self.cache.insert(path.to_path_buf(), value.clone());
        Ok(value)
    }

    /// Main entry point for the multi-stage reference resolution and fixup process.
    ///
    /// ## Pipeline:
    ///
    /// ### Stage 1: Add Missing Definitions
    /// Injects commonly-referenced but missing type definitions (pagination links,
    /// tags arrays, K8s node taints, etc.) into the spec.
    ///
    /// ### Stage 2: Multi-Pass Reference Resolution (3 passes)
    /// Resolves `$ref` directives. Multiple passes are needed because:
    /// - Pass 1: Resolves external file references
    /// - Pass 2: Resolves newly-exposed internal references from Pass 1
    /// - Pass 3: Catches any remaining nested references
    ///
    /// ### Stage 3: Clean Unresolved References
    /// Any `$ref` that couldn't be resolved (broken links, missing files) gets
    /// replaced with a generic fallback schema to prevent build failures.
    ///
    /// ### Stage 4: Sanitize Documentation
    /// Fixes doctest issues by marking non-Rust code blocks as ```text```.
    /// This prevents Rust's doc system from trying to compile kubectl commands,
    /// curl examples, and HTTP responses as Rust code.
    ///
    /// ### Stage 5: Deduplicate Response Types
    /// **CRITICAL**: Progenitor 0.11.0 asserts that each operation has only one
    /// successful response type. Operations with multiple 2xx responses (e.g.,
    /// 200, 201, 204) cause panics. We simplify each operation to keep only the
    /// first response.
    ///
    /// ## Why This Complexity Is Necessary:
    /// The DigitalOcean OpenAPI spec has several quality issues that would prevent
    /// successful code generation without these workarounds. Each stage addresses
    /// specific incompatibilities with progenitor and Rust's tooling.
    fn resolve_refs(&mut self, mut value: Value) -> Result<Value, Box<dyn std::error::Error>> {
        // Store the root spec for internal reference resolution
        self.root_spec = Some(value.clone());

        // Add missing definitions for common DigitalOcean API patterns
        self.add_missing_definitions(&mut value)?;

        // Update root spec after adding definitions
        self.root_spec = Some(value.clone());

        // Run reference resolution multiple times to handle internal references
        for i in 0..3 {
            println!("Reference resolution pass {}", i + 1);
            self.resolve_refs_recursive(&mut value, &self.spec_dir.clone())?;
        }

        // Clean up any remaining unresolved references
        self.clean_unresolved_refs(&mut value)?;

        // Sanitize documentation to fix doctest and doc generation issues
        self.sanitize_documentation(&mut value)?;

        // Deduplicate response types to prevent progenitor 0.11.0 assertion failures
        self.deduplicate_response_types(&mut value)?;

        Ok(value)
    }

    fn resolve_refs_recursive(
        &mut self,
        value: &mut Value,
        current_dir: &Path,
    ) -> Result<(), Box<dyn std::error::Error>> {
        self.resolve_refs_in_context(value, current_dir, None)
    }

    fn resolve_refs_in_context(
        &mut self,
        value: &mut Value,
        current_dir: &Path,
        _context_value: Option<&Value>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        match value {
            Value::Mapping(map) => {
                // Check for $ref
                if let Some(ref_value) = map.get(&Value::String("$ref".to_string())) {
                    if let Some(ref_str) = ref_value.as_str() {
                        let resolved = self.resolve_single_ref(ref_str, current_dir)?;
                        *value = resolved;
                        return Ok(());
                    }
                }

                // Recursively process all values in the mapping
                for (_, v) in map.iter_mut() {
                    self.resolve_refs_in_context(v, current_dir, None)?;
                }
            }
            Value::Sequence(seq) => {
                for item in seq.iter_mut() {
                    self.resolve_refs_in_context(item, current_dir, None)?;
                }
            }
            _ => {}
        }
        Ok(())
    }

    fn resolve_single_ref(
        &mut self,
        ref_str: &str,
        current_dir: &Path,
    ) -> Result<Value, Box<dyn std::error::Error>> {
        self.resolve_single_ref_with_context(ref_str, current_dir, None)
    }

    /// Resolves a single `$ref` directive to its target definition.
    ///
    /// ## Reference Format:
    /// OpenAPI references follow the format: `[file_path]#[json_pointer]`
    ///
    /// ### Examples:
    /// 1. **Internal reference** (within same file):
    ///    ```yaml
    ///    $ref: "#/definitions/droplet"
    ///    ```
    ///    Looks up `/definitions/droplet` in the current file or root spec.
    ///
    /// 2. **External file reference**:
    ///    ```yaml
    ///    $ref: "shared/droplet.yml#/properties/id"
    ///    ```
    ///    Loads `shared/droplet.yml` then navigates to `/properties/id`.
    ///
    /// 3. **File reference without pointer**:
    ///    ```yaml
    ///    $ref: "shared/droplet.yml"
    ///    ```
    ///    Loads entire file as the definition.
    ///
    /// ## Path Resolution Strategy:
    /// 1. If ref starts with `#`, it's internal (use context or root spec)
    /// 2. Otherwise, resolve path relative to `current_dir`
    /// 3. If path doesn't exist and starts with `../../../shared/`, try `shared/` instead
    ///    (workaround for incorrect relative paths in DigitalOcean spec)
    /// 4. If file still doesn't exist, return a generic fallback schema
    ///
    /// ## Circular Reference Detection:
    /// The `resolving` set tracks files currently being processed. If we encounter
    /// a file already in this set, we've found a circular dependency and error out.
    fn resolve_single_ref_with_context(
        &mut self,
        ref_str: &str,
        current_dir: &Path,
        context_value: Option<&Value>,
    ) -> Result<Value, Box<dyn std::error::Error>> {
        if ref_str.starts_with('#') {
            // Internal reference - first try context, then root document
            let pointer = &ref_str[1..]; // Remove the '#'

            // Try context first for file-local references
            if let Some(context) = context_value {
                if let Ok(result) = self.apply_json_pointer(context, pointer) {
                    return Ok(result);
                }
            }

            // Fall back to root document
            if let Some(root) = &self.root_spec {
                return self.apply_json_pointer(root, pointer);
            } else {
                return Err("Internal reference found but no root spec available".into());
            }
        }

        // Parse file path and optional JSON pointer
        let (file_part, pointer_part) = if let Some(hash_pos) = ref_str.find('#') {
            (&ref_str[..hash_pos], Some(&ref_str[hash_pos + 1..]))
        } else {
            (ref_str, None)
        };

        // Handle empty file part (internal reference only)
        if file_part.is_empty() {
            let pointer = pointer_part.unwrap_or("");

            // Try context first for file-local references
            if let Some(context) = context_value {
                if let Ok(result) = self.apply_json_pointer(context, pointer) {
                    return Ok(result);
                }
            }

            // Fall back to root document
            if let Some(root) = &self.root_spec {
                return self.apply_json_pointer(root, pointer);
            } else {
                return Err("Internal reference found but no root spec available".into());
            }
        }

        // Resolve file path relative to current directory
        let mut file_path = current_dir.join(file_part);

        // Handle problematic relative paths that go too far up the directory tree
        if !file_path.exists() && file_part.starts_with("../../../shared/") {
            // Try the corrected path within the specification directory
            let corrected_part = file_part.replace("../../../shared/", "shared/");
            file_path = current_dir.join(&corrected_part);
            println!(
                "Corrected problematic path '{}' to '{}' -> {}",
                file_part,
                corrected_part,
                file_path.display()
            );
        }

        // Validate the file exists before trying to canonicalize
        let canonical_path = if file_path.exists() {
            match file_path.canonicalize() {
                Ok(path) => path,
                Err(e) => {
                    return Err(format!(
                        "Failed to canonicalize path '{}': {}",
                        file_path.display(),
                        e
                    )
                    .into());
                }
            }
        } else {
            // For any missing file reference, use a fallback approach
            // This is more robust than trying to enumerate all possible missing files
            println!(
                "Using fallback for missing file reference: {} -> {}",
                file_part,
                file_path.display()
            );
            // Return a simple object type as fallback
            return Ok(serde_yaml::from_str(
                r#"
type: object
description: "Fallback schema for missing file reference"
additionalProperties: true
"#,
            )?);
        };

        // Check for circular reference
        if self.resolving.contains(&canonical_path) {
            return Err(
                format!("Circular reference detected: {}", canonical_path.display()).into(),
            );
        }

        self.resolving.insert(canonical_path.clone());

        // Load and resolve the referenced file
        let mut referenced_value = match self.load_yaml_file(&canonical_path) {
            Ok(value) => value,
            Err(e) => {
                self.resolving.remove(&canonical_path);
                return Err(format!(
                    "Failed to load referenced file '{}': {}",
                    canonical_path.display(),
                    e
                )
                .into());
            }
        };

        // Resolve refs in the referenced file with its directory as context
        let referenced_dir = canonical_path.parent().unwrap_or(current_dir);
        self.resolve_refs_with_file_context(&mut referenced_value, referenced_dir)?;

        self.resolving.remove(&canonical_path);

        // Apply JSON pointer if present
        if let Some(pointer) = pointer_part {
            if !pointer.is_empty() {
                // Apply pointer to original value for local references
                referenced_value = self.apply_json_pointer(&referenced_value, pointer)?;
            }
        }

        Ok(referenced_value)
    }

    fn resolve_refs_with_file_context(
        &mut self,
        value: &mut Value,
        current_dir: &Path,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Store the original file value for resolving internal references
        let original_file_value = value.clone();
        self.resolve_refs_with_context_value(value, current_dir, &original_file_value)
    }

    fn resolve_refs_with_context_value(
        &mut self,
        value: &mut Value,
        current_dir: &Path,
        file_context: &Value,
    ) -> Result<(), Box<dyn std::error::Error>> {
        match value {
            Value::Mapping(map) => {
                // Check for $ref
                if let Some(ref_value) = map.get(&Value::String("$ref".to_string())) {
                    if let Some(ref_str) = ref_value.as_str() {
                        let resolved = if ref_str.starts_with('#') {
                            // Internal reference within this file
                            let pointer = &ref_str[1..]; // Remove the '#'
                            self.apply_json_pointer(file_context, pointer)?
                        } else {
                            // External reference
                            self.resolve_single_ref(ref_str, current_dir)?
                        };
                        *value = resolved;
                        return Ok(());
                    }
                }

                // Recursively process all values in the mapping
                for (_, v) in map.iter_mut() {
                    self.resolve_refs_with_context_value(v, current_dir, file_context)?;
                }
            }
            Value::Sequence(seq) => {
                for item in seq.iter_mut() {
                    self.resolve_refs_with_context_value(item, current_dir, file_context)?;
                }
            }
            _ => {}
        }
        Ok(())
    }

    /// Navigates through a YAML/JSON structure using JSON Pointer (RFC 6901).
    ///
    /// ## JSON Pointer Format:
    /// A JSON Pointer is a string of tokens separated by `/` characters. Each token
    /// is either a key name (for objects) or an index (for arrays).
    ///
    /// ### Examples:
    /// - `/definitions/droplet` → Navigate to `value["definitions"]["droplet"]`
    /// - `/paths/~1droplets/get` → `value["paths"]["/droplets"]["get"]` (~ escaping)
    /// - `/items/0/name` → `value["items"][0]["name"]`
    ///
    /// ## Implementation Details:
    /// 1. Empty pointer or `/` returns the root value
    /// 2. Split pointer by `/` and navigate step by step
    /// 3. For objects, look up the key
    /// 4. For arrays, parse the token as an integer index
    /// 5. If a key isn't found, try looking in `/definitions` as fallback
    /// 6. If still not found, return a generic fallback schema instead of erroring
    ///
    /// ## Why Fallbacks?
    /// The DigitalOcean spec has some broken references. Rather than failing the
    /// build, we return generic object types to allow code generation to continue.
    fn apply_json_pointer(
        &self,
        value: &Value,
        pointer: &str,
    ) -> Result<Value, Box<dyn std::error::Error>> {
        if pointer.is_empty() || pointer == "/" {
            return Ok(value.clone());
        }

        let parts: Vec<&str> = pointer.split('/').skip(1).collect(); // Skip first empty part
        let mut current = value;

        for part in parts {
            match current {
                Value::Mapping(map) => {
                    if let Some(found) = map.get(&Value::String(part.to_string())) {
                        current = found;
                    } else {
                        // Try definitions as fallback for root-level references
                        if let Some(defs) = map.get(&Value::String("definitions".to_string())) {
                            if let Some(def_map) = defs.as_mapping() {
                                if let Some(found) = def_map.get(&Value::String(part.to_string())) {
                                    current = found;
                                    continue;
                                }
                            }
                        }

                        // If the JSON pointer path is not found, return a fallback definition immediately
                        println!(
                            "Creating fallback definition for missing JSON pointer path: {}",
                            part
                        );
                        return Ok(serde_yaml::from_str(&format!(
                            r#"
type: object
description: "Auto-generated fallback definition for: {}"
additionalProperties: true
"#,
                            part
                        ))
                        .unwrap_or_else(|_| Value::Mapping(serde_yaml::Mapping::new())));
                    }
                }
                Value::Sequence(seq) => {
                    let index: usize = part
                        .parse()
                        .map_err(|_| format!("Invalid array index in JSON pointer: {}", part))?;
                    current = seq
                        .get(index)
                        .ok_or_else(|| format!("Array index out of bounds: {}", index))?;
                }
                _ => {
                    return Err(
                        format!("Cannot apply JSON pointer to non-object/array: {}", part).into(),
                    );
                }
            }
        }

        Ok(current.clone())
    }

    /// Injects commonly-referenced but missing type definitions into the spec.
    ///
    /// ## The Problem:
    /// The DigitalOcean OpenAPI spec references several types that are never defined:
    /// - `forward_links`, `backward_links` (pagination)
    /// - `existing_tags_array` (resource tagging)
    /// - `kubernetes_node_pool_taint` (K8s node configuration)
    /// - `region_state` (datacenter availability)
    /// - `apiChatbot` (AI assistant features)
    ///
    /// These references would cause progenitor to fail with "undefined type" errors.
    ///
    /// ## The Solution:
    /// Before resolving references, we inject reasonable definitions for these
    /// missing types directly into the `/definitions` section. This allows the
    /// reference resolution to succeed and generates working Rust types.
    ///
    /// ## Why Not Fix Upstream?
    /// These issues exist in DigitalOcean's official spec. While we could report
    /// them, we need the build to work today, so we patch them here.
    fn add_missing_definitions(
        &mut self,
        value: &mut Value,
    ) -> Result<(), Box<dyn std::error::Error>> {
        if let Some(obj) = value.as_mapping_mut() {
            // Create the definitions/components section if it doesn't exist
            if !obj.contains_key(&Value::String("definitions".to_string())) {
                obj.insert(
                    Value::String("definitions".to_string()),
                    Value::Mapping(serde_yaml::Mapping::new()),
                );
            }

            if let Some(definitions) = obj.get_mut(&Value::String("definitions".to_string())) {
                if let Some(def_map) = definitions.as_mapping_mut() {
                    self.add_pagination_link_definitions(def_map)?;
                    self.add_common_attribute_definitions(def_map)?;
                }
            }
        }
        Ok(())
    }

    /// Adds `forward_links` and `backward_links` definitions for API pagination.
    ///
    /// These types are used throughout the API for paginated list responses.
    /// - `forward_links`: Contains `first`, `last`, `next` URLs
    /// - `backward_links`: Contains `first`, `last`, `prev` URLs
    fn add_pagination_link_definitions(
        &self,
        definitions: &mut serde_yaml::Mapping,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Add forward_links definition for pagination
        let forward_links = serde_yaml::from_str(
            r#"
type: object
properties:
  first:
    type: string
    format: uri
    example: "https://api.digitalocean.com/v2/images?page=1"
  last:
    type: string
    format: uri
    example: "https://api.digitalocean.com/v2/images?page=3"
  next:
    type: string
    format: uri
    example: "https://api.digitalocean.com/v2/images?page=2"
"#,
        )?;

        // Add backward_links definition for pagination
        let backward_links = serde_yaml::from_str(
            r#"
type: object
properties:
  first:
    type: string
    format: uri
    example: "https://api.digitalocean.com/v2/images?page=1"
  last:
    type: string
    format: uri  
    example: "https://api.digitalocean.com/v2/images?page=3"
  prev:
    type: string
    format: uri
    example: "https://api.digitalocean.com/v2/images?page=1"
"#,
        )?;

        definitions.insert(Value::String("forward_links".to_string()), forward_links);
        definitions.insert(Value::String("backward_links".to_string()), backward_links);

        println!("Added pagination link definitions (forward_links, backward_links)");
        Ok(())
    }

    /// Adds definitions for common attributes used across multiple resources.
    ///
    /// Injects:
    /// - `existing_tags_array`: For resource tagging (tags attached to droplets, etc.)
    /// - `error_response`: Standard API error format
    /// - `kubernetes_node_pool_taint`: K8s node scheduling constraints
    /// - `region_state`: Datacenter availability (available/unavailable)
    /// - `apiChatbot`: AI assistant configuration
    fn add_common_attribute_definitions(
        &self,
        definitions: &mut serde_yaml::Mapping,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Add common tag array definition often missing from shared attributes
        let existing_tags_array = serde_yaml::from_str(
            r#"
type: array
items:
  type: object
  properties:
    name:
      type: string
      minLength: 1
      maxLength: 255
      example: "web"
    resources:
      type: object
      properties:
        count:
          type: integer
          example: 0
        last_tagged_uri:
          type: string
          example: ""
  required:
    - name
    - resources
"#,
        )?;

        definitions.insert(
            Value::String("existing_tags_array".to_string()),
            existing_tags_array,
        );

        // Add basic error response definition
        let error_response = serde_yaml::from_str(
            r#"
type: object
properties:
  id:
    type: string
    example: "bad_request"
  message:
    type: string
    example: "The request was invalid."
  request_id: 
    type: string
    example: "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
required:
  - id
  - message
"#,
        )?;

        definitions.insert(Value::String("error_response".to_string()), error_response);

        // Add kubernetes node pool taint definition (commonly referenced but missing)
        let kubernetes_node_pool_taint = serde_yaml::from_str(
            r#"
type: object
properties:
  key:
    type: string
    example: "node.kubernetes.io/example-key"
    description: "The taint key"
  value:
    type: string
    example: "example-value"
    description: "The taint value"  
  effect:
    type: string
    enum:
      - NoSchedule
      - PreferNoSchedule
      - NoExecute
    example: "NoSchedule"
    description: "The taint effect"
required:
  - key
  - effect
"#,
        )?;

        definitions.insert(
            Value::String("kubernetes_node_pool_taint".to_string()),
            kubernetes_node_pool_taint,
        );

        // Add region state definition
        let region_state = serde_yaml::from_str(
            r#"
type: string
enum:
  - available
  - unavailable
example: "available"
description: "The availability state of the region"
"#,
        )?;

        definitions.insert(Value::String("region_state".to_string()), region_state);

        // Add API chatbot definition
        let api_chatbot = serde_yaml::from_str(
            r#"
type: object
properties:
  id:
    type: string
    example: "chatbot-123"
  name:
    type: string
    example: "Customer Support Bot"
  enabled:
    type: boolean
    example: true
  settings:
    type: object
    additionalProperties: true
"#,
        )?;

        definitions.insert(Value::String("apiChatbot".to_string()), api_chatbot);

        println!("Added common attribute definitions (existing_tags_array, error_response, kubernetes_node_pool_taint, region_state, apiChatbot)");
        Ok(())
    }

    /// Replaces any remaining unresolved `$ref` directives with fallback schemas.
    ///
    /// ## Why This Is Needed:
    /// After 3 passes of reference resolution, some `$ref`s may still be unresolved due to:
    /// - Broken relative paths (e.g., `../../../shared/missing.yml`)
    /// - Non-existent files referenced in the spec
    /// - Malformed references
    ///
    /// ## What Gets Cleaned:
    /// Any `$ref` matching these patterns is replaced with a generic string type:
    /// - `../../../shared/...` (broken relative paths)
    /// - `#/api...` (invalid internal refs)
    /// - `node.yml` (missing file)
    /// - `shared/attributes/...` (missing shared attributes)
    /// - `*.yml` without `#` (file-only refs that failed to load)
    ///
    /// ## Fallback Schema:
    /// Unresolved refs become: `{ type: "string", description: "Fallback for..." }`
    ///
    /// This allows code generation to continue even with a broken spec, which is
    /// better than failing the build entirely.
    fn clean_unresolved_refs(&self, value: &mut Value) -> Result<(), Box<dyn std::error::Error>> {
        match value {
            Value::Mapping(map) => {
                // Check for unresolved $ref patterns
                let ref_to_replace =
                    if let Some(ref_value) = map.get(&Value::String("$ref".to_string())) {
                        if let Some(ref_str) = ref_value.as_str() {
                            // Handle common unresolved patterns
                            if ref_str.contains("../../../shared/")
                                || ref_str.starts_with("#/api")
                                || ref_str.contains("node.yml")
                                || ref_str.contains("shared/attributes/")
                                || ref_str.ends_with(".yml") && !ref_str.contains("#")
                            {
                                Some(ref_str.to_string())
                            } else {
                                None
                            }
                        } else {
                            None
                        }
                    } else {
                        None
                    };

                if let Some(ref_str) = ref_to_replace {
                    // Replace with a generic string type
                    println!(
                        "Replacing unresolved reference '{}' with fallback schema",
                        ref_str
                    );
                    map.clear();
                    map.insert(
                        Value::String("type".to_string()),
                        Value::String("string".to_string()),
                    );
                    map.insert(
                        Value::String("description".to_string()),
                        Value::String(format!("Fallback for unresolved reference: {}", ref_str)),
                    );
                    return Ok(());
                }

                // Recursively clean all values in the mapping
                for (_, v) in map.iter_mut() {
                    self.clean_unresolved_refs(v)?;
                }
            }
            Value::Sequence(seq) => {
                for item in seq.iter_mut() {
                    self.clean_unresolved_refs(item)?;
                }
            }
            _ => {}
        }
        Ok(())
    }

    /// Simplifies API operations to have only one response per operation.
    ///
    /// ## CRITICAL FOR PROGENITOR 0.11.0
    ///
    /// ### The Problem:
    /// Progenitor 0.11.0 has an internal assertion that each API operation can only
    /// have ONE successful response type. Many DigitalOcean endpoints return multiple
    /// success codes:
    ///
    /// ```yaml
    /// responses:
    ///   200:
    ///     description: Success
    ///     content:
    ///       application/json: { schema: ... }
    ///   201:
    ///     description: Created
    ///     content:
    ///       application/json: { schema: ... }
    ///   204:
    ///     description: No Content
    /// ```
    ///
    /// When progenitor encounters this, it panics with:
    /// ```text
    /// thread 'main' panicked at 'assertion failed: success_responses.len() <= 1'
    /// ```
    ///
    /// ### The Solution:
    /// For any operation with multiple 2xx responses (or >2 total responses), we:
    /// 1. Keep ONLY the first success response (200, 201, etc.)
    /// 2. Remove all other success responses
    /// 3. Also simplify content-types (keep only first, e.g., `application/json`)
    ///
    /// ### Impact:
    /// This means generated Rust functions will only return the first response type.
    /// Users won't get distinct types for 200 vs 201 vs 204. This is a limitation
    /// of progenitor 0.11.0, not the DigitalOcean API.
    ///
    /// ### Example Transformation:
    /// ```yaml
    /// # Before:
    /// responses:
    ///   200: { ... }
    ///   201: { ... }
    ///   204: { ... }
    ///
    /// # After:
    /// responses:
    ///   200: { ... }  # Only first success response kept
    /// ```
    fn deduplicate_response_types(
        &self,
        value: &mut Value,
    ) -> Result<(), Box<dyn std::error::Error>> {
        println!("Deduplicating response types to prevent progenitor assertion failures...");
        let mut operations_modified = 0;
        let mut total_responses_removed = 0;

        if let Some(obj) = value.as_mapping_mut() {
            if let Some(paths) = obj.get_mut(&Value::String("paths".to_string())) {
                if let Some(paths_map) = paths.as_mapping_mut() {
                    for (path_key, path_value) in paths_map.iter_mut() {
                        if let Some(path_obj) = path_value.as_mapping_mut() {
                            // Check each HTTP method in this path
                            for (method_key, method_value) in path_obj.iter_mut() {
                                if let Some(method_str) = method_key.as_str() {
                                    // Skip non-HTTP method keys like "parameters"
                                    if ![
                                        "get", "post", "put", "patch", "delete", "head", "options",
                                        "trace",
                                    ]
                                    .contains(&method_str)
                                    {
                                        continue;
                                    }

                                    if let Some(operation) = method_value.as_mapping_mut() {
                                        // Get operation_id before mutable borrow
                                        let operation_id = operation
                                            .get(&Value::String("operationId".to_string()))
                                            .and_then(|v| v.as_str())
                                            .unwrap_or("unknown")
                                            .to_string();

                                        if let Some(responses) = operation
                                            .get_mut(&Value::String("responses".to_string()))
                                        {
                                            if let Some(responses_map) = responses.as_mapping_mut()
                                            {
                                                let original_count = responses_map.len();
                                                let mut success_responses = Vec::new();
                                                let mut other_responses = Vec::new();

                                                // Separate success (2xx) responses from others
                                                for (status_key, response_value) in
                                                    responses_map.iter()
                                                {
                                                    if let Some(status_str) = status_key.as_str() {
                                                        if status_str.starts_with('2')
                                                            && status_str.len() == 3
                                                        {
                                                            success_responses.push((
                                                                status_key.clone(),
                                                                response_value.clone(),
                                                            ));
                                                        } else {
                                                            other_responses.push((
                                                                status_key.clone(),
                                                                response_value.clone(),
                                                            ));
                                                        }
                                                    } else {
                                                        other_responses.push((
                                                            status_key.clone(),
                                                            response_value.clone(),
                                                        ));
                                                    }
                                                }

                                                // Use a more aggressive approach: if there are multiple success responses,
                                                // or if there's any response complexity, simplify to just one response
                                                if success_responses.len() > 1 || original_count > 2
                                                {
                                                    println!("Operation '{}' ({} {}) has {} responses ({}), simplifying to prevent assertion failure", 
                                                            operation_id, method_str.to_uppercase(),
                                                            path_key.as_str().unwrap_or("unknown"),
                                                            success_responses.len(),
                                                            original_count);

                                                    responses_map.clear();

                                                    // Keep only one response: prefer first success, then first error, then default
                                                    if let Some((first_status, first_response)) =
                                                        success_responses.into_iter().next()
                                                    {
                                                        // Also simplify the response content to avoid multiple content types
                                                        let mut simplified_response =
                                                            first_response;
                                                        if let Some(response_obj) =
                                                            simplified_response.as_mapping_mut()
                                                        {
                                                            if let Some(content) = response_obj
                                                                .get_mut(&Value::String(
                                                                    "content".to_string(),
                                                                ))
                                                            {
                                                                if let Some(content_map) =
                                                                    content.as_mapping_mut()
                                                                {
                                                                    // Keep only the first content type to avoid multiple response types
                                                                    if content_map.len() > 1 {
                                                                        let first_content_type =
                                                                            content_map
                                                                                .keys()
                                                                                .next()
                                                                                .cloned();
                                                                        if let Some(first_key) =
                                                                            first_content_type
                                                                        {
                                                                            let first_value =
                                                                                content_map
                                                                                    .get(&first_key)
                                                                                    .cloned();
                                                                            content_map.clear();
                                                                            if let Some(value) =
                                                                                first_value
                                                                            {
                                                                                content_map.insert(
                                                                                    first_key,
                                                                                    value,
                                                                                );
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        responses_map.insert(
                                                            first_status,
                                                            simplified_response,
                                                        );
                                                    } else if let Some((
                                                        first_status,
                                                        first_response,
                                                    )) = other_responses
                                                        .iter()
                                                        .find(|(status_key, _)| {
                                                            if let Some(status_str) =
                                                                status_key.as_str()
                                                            {
                                                                status_str != "default"
                                                            } else {
                                                                true
                                                            }
                                                        })
                                                        .map(|(k, v)| (k.clone(), v.clone()))
                                                    {
                                                        responses_map
                                                            .insert(first_status, first_response);
                                                    } else if let Some((
                                                        default_status,
                                                        default_response,
                                                    )) = other_responses
                                                        .iter()
                                                        .find(|(status_key, _)| {
                                                            if let Some(status_str) =
                                                                status_key.as_str()
                                                            {
                                                                status_str == "default"
                                                            } else {
                                                                false
                                                            }
                                                        })
                                                        .map(|(k, v)| (k.clone(), v.clone()))
                                                    {
                                                        responses_map.insert(
                                                            default_status,
                                                            default_response,
                                                        );
                                                    }

                                                    operations_modified += 1;
                                                    total_responses_removed +=
                                                        original_count - responses_map.len();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if operations_modified > 0 {
            println!(
                "Modified {} operations, removed {} duplicate responses",
                operations_modified, total_responses_removed
            );
        } else {
            println!("No operations with multiple response types found");
        }

        Ok(())
    }

    /// Fixes documentation strings to prevent Rust doctest failures.
    ///
    /// ## The Problem:
    /// OpenAPI `description` fields often contain code examples like:
    /// - Shell commands: `curl -X POST ...`
    /// - Kubernetes commands: `kubectl create secret ...`
    /// - HTTP responses: `HTTP/1.1 403 Forbidden`
    ///
    /// When progenitor generates Rust doc comments from these, Rust's `cargo doc`
    /// treats them as Rust doctests and tries to compile them, causing failures like:
    /// ```text
    /// error[E0425]: cannot find value `curl` in this scope
    /// error[E0425]: cannot find value `kubectl` in this scope
    /// ```
    ///
    /// ## The Solution:
    /// Mark all non-Rust code blocks with the `text` language tag:
    /// - Before: ` ```\ncurl ...` ` (Rust tries to compile this)
    /// - After: ` ```text\ncurl ...` ` (Rust skips compilation)
    ///
    /// This function applies hundreds of targeted string replacements to catch
    /// all variations of non-Rust code blocks in the OpenAPI descriptions.
    ///
    /// ## What Gets Fixed:
    /// - Code blocks: ` ``` ` → ` ```text`
    /// - URLs in text: `https://...` → `<https://...>` (prevents doc link errors)
    /// - Template syntax: `<host>` → `\<host\>` (escapes angle brackets)
    /// - Special characters: `[V2]` → `\[V2\]` (escapes square brackets)
    fn sanitize_documentation(&self, value: &mut Value) -> Result<(), Box<dyn std::error::Error>> {
        println!("Sanitizing documentation to fix doctest and doc generation issues...");
        let mut fixes_applied = 0;

        // Simple targeted fixes for known problematic patterns
        fixes_applied += self.apply_targeted_fixes(value)?;

        if fixes_applied > 0 {
            println!("Applied {} targeted documentation fixes", fixes_applied);
        } else {
            println!("No documentation fixes needed");
        }

        Ok(())
    }

    /// Recursively applies documentation fixes to all description/example fields.
    ///
    /// Walks the entire YAML tree and applies string replacements to any
    /// `description` or `example` field found. See `sanitize_documentation`
    /// for details on what gets fixed and why.
    fn apply_targeted_fixes(&self, value: &mut Value) -> Result<usize, Box<dyn std::error::Error>> {
        let mut fixes_count = 0;

        match value {
            Value::Mapping(map) => {
                // Fix description fields with known problematic content
                if let Some(description) = map.get_mut(&Value::String("description".to_string())) {
                    if let Some(desc_str) = description.as_str() {
                        let mut fixed = desc_str.to_string();
                        let original = fixed.clone();

                        // Apply specific known fixes
                        fixed = fixed.replace(
                            "https://github.com/google/re2/wiki/Syntax",
                            "<https://github.com/google/re2/wiki/Syntax>",
                        );
                        fixed = fixed.replace(
                            "https://www.digitalocean.com/legal/terms-of-service-agreement/",
                            "<https://www.digitalocean.com/legal/terms-of-service-agreement/>",
                        );
                        fixed = fixed.replace("[V2]", r"\[V2\]");
                        fixed = fixed.replace("<host>", r"\<host\>");
                        fixed = fixed.replace("<port>", r"\<port\>");
                        fixed = fixed.replace("<resource>", r"\<resource\>");

                        // Fix code blocks - use text instead of ignore for non-Rust content
                        fixed = fixed.replace("```\nDD_KEY <%pri%>", "```text\nDD_KEY <%pri%>");
                        fixed = fixed.replace("```\ncurl ", "```text\ncurl ");
                        fixed = fixed.replace("```\n     curl ", "```text\n     curl ");
                        fixed = fixed.replace("```\n      curl ", "```text\n      curl ");
                        fixed = fixed.replace("```\n    curl ", "```text\n    curl ");
                        // Handle kubectl commands with various indentation levels
                        for spaces in 0..=8 {
                            let indent = if spaces == 0 {
                                String::new()
                            } else {
                                " ".repeat(spaces)
                            };
                            fixed = fixed.replace(
                                &format!("```\n{}kubectl ", indent),
                                &format!("```text\n{}kubectl ", indent),
                            );
                        }
                        fixed = fixed.replace("```\nHTTP/", "```text\nHTTP/");
                        fixed = fixed.replace("```\nexport ", "```text\nexport ");
                        fixed = fixed.replace("```\n    . . .", "```text\n    . . .");
                        fixed = fixed.replace(
                            "```\n429 Too Many Requests",
                            "```text\n429 Too Many Requests",
                        );
                        fixed = fixed.replace(
                            "```\n    429 Too Many Requests",
                            "```text\n    429 Too Many Requests",
                        );

                        // Catch ALL remaining code blocks and mark them as text
                        // This is the safest approach since OpenAPI examples aren't meant to be Rust doctests

                        // First handle code blocks that start with just ```
                        let mut temp_fixed = fixed.clone();
                        let mut found_blocks = Vec::new();

                        // Find all code block starts
                        let mut pos = 0;
                        while let Some(start) = temp_fixed[pos..].find("```") {
                            let abs_pos = pos + start;

                            // Look for the end of this line to see what language marker (if any) is there
                            if let Some(newline_pos) = temp_fixed[abs_pos..].find('\n') {
                                let line_end = abs_pos + newline_pos;
                                let code_block_start = &temp_fixed[abs_pos..line_end];

                                // If it's just ``` or ```\s*, mark it for replacement
                                if code_block_start.trim() == "```" {
                                    found_blocks.push(abs_pos);
                                }
                            }
                            pos = abs_pos + 3;
                        }

                        // Replace from the end to avoid position shifts
                        for &block_pos in found_blocks.iter().rev() {
                            if let Some(newline_pos) = temp_fixed[block_pos..].find('\n') {
                                let end_pos = block_pos + newline_pos;
                                temp_fixed.replace_range(block_pos..end_pos, "```text");
                            }
                        }

                        fixed = temp_fixed;

                        // More comprehensive approach: look for any remaining code blocks that contain kubectl
                        // and mark them as text regardless of spacing
                        if fixed.contains("kubectl") {
                            // Find any ```\n followed by kubectl (with any amount of whitespace)
                            let lines: Vec<&str> = fixed.split('\n').collect();
                            let mut new_lines = Vec::new();
                            let mut i = 0;

                            while i < lines.len() {
                                let line = lines[i];
                                if line.trim() == "```" && i + 1 < lines.len() {
                                    let next_line = lines[i + 1];
                                    if next_line.trim_start().starts_with("kubectl") {
                                        new_lines.push("```text");
                                    } else {
                                        new_lines.push(line);
                                    }
                                } else {
                                    new_lines.push(line);
                                }
                                i += 1;
                            }

                            fixed = new_lines.join("\n");
                        }

                        // Additional pattern to catch any remaining code blocks with the specific pattern
                        // that's still slipping through (indented kubectl with backslashes)
                        if fixed.contains("kubectl create secret generic docr") {
                            // Handle various indentation levels for kubectl commands
                            for spaces in [2, 3, 4, 5, 6, 7, 8] {
                                let indent = " ".repeat(spaces);
                                fixed = fixed.replace(
                                    &format!("```\n{}kubectl create secret generic docr", indent),
                                    &format!(
                                        "```text\n{}kubectl create secret generic docr",
                                        indent
                                    ),
                                );
                                fixed = fixed.replace(
                                    &format!("```\n{}kubectl create secret", indent),
                                    &format!("```text\n{}kubectl create secret", indent),
                                );
                            }
                        }

                        // Handle indented kubectl commands that aren't in explicit code blocks
                        // These appear as plain indented text but get treated as Rust code examples
                        if fixed.contains("kubectl create secret generic docr") {
                            // Look for lines that start with whitespace followed by kubectl
                            let lines: Vec<&str> = fixed.split('\n').collect();
                            let mut new_lines = Vec::new();
                            let mut in_kubectl_block = false;
                            let mut kubectl_lines = Vec::new();

                            for line in lines {
                                // Detect start of kubectl command block (indented kubectl line)
                                if line
                                    .trim_start()
                                    .starts_with("kubectl create secret generic docr")
                                    && line.starts_with("    ")
                                {
                                    in_kubectl_block = true;
                                    kubectl_lines.clear();
                                    kubectl_lines.push(line);
                                } else if in_kubectl_block {
                                    // Continue collecting kubectl-related lines
                                    if line.trim().is_empty()
                                        || (line.starts_with("      ")
                                            && (line.contains("--from-file")
                                                || line.contains("--type")))
                                    {
                                        kubectl_lines.push(line);

                                        // If this line doesn't end with \, it's the end of the command
                                        if !line.trim().ends_with('\\') && !line.trim().is_empty() {
                                            // Convert the collected kubectl lines to a proper text code block
                                            new_lines.push("```text");
                                            for kubectl_line in &kubectl_lines {
                                                new_lines.push(kubectl_line);
                                            }
                                            new_lines.push("```");
                                            in_kubectl_block = false;
                                            kubectl_lines.clear();
                                            continue;
                                        }
                                    } else {
                                        // End of kubectl block, flush what we have
                                        if !kubectl_lines.is_empty() {
                                            new_lines.push("```text");
                                            for kubectl_line in &kubectl_lines {
                                                new_lines.push(kubectl_line);
                                            }
                                            new_lines.push("```");
                                            kubectl_lines.clear();
                                        }
                                        in_kubectl_block = false;
                                        new_lines.push(line);
                                    }
                                } else {
                                    new_lines.push(line);
                                }
                            }

                            // Handle any remaining kubectl lines
                            if !kubectl_lines.is_empty() {
                                new_lines.push("```text");
                                for kubectl_line in &kubectl_lines {
                                    new_lines.push(kubectl_line);
                                }
                                new_lines.push("```");
                            }

                            fixed = new_lines.join("\n");
                        }

                        // Handle specific problematic patterns from the failed tests
                        if fixed.contains("HTTP/1.1 403 Forbidden") {
                            fixed = fixed.replace(
                                "```\nHTTP/1.1 403 Forbidden",
                                "```text\nHTTP/1.1 403 Forbidden",
                            );
                        }

                        // Handle indented curl commands that start with spaces
                        if fixed.contains("curl -H \"Authorization:") {
                            fixed = fixed.replace(
                                "```\n    curl -H \"Authorization:",
                                "```text\n    curl -H \"Authorization:",
                            );
                        }

                        if fixed != original {
                            *description = Value::String(fixed);
                            fixes_count += 1;
                        }
                    }
                }

                // Fix example fields
                if let Some(example) = map.get_mut(&Value::String("example".to_string())) {
                    if let Some(example_str) = example.as_str() {
                        let mut fixed = example_str.to_string();
                        let original = fixed.clone();

                        // Escape problematic template characters
                        if fixed.contains("<%pri%>") || fixed.contains("DD_KEY") {
                            fixed = fixed.replace("<%", "\\<%").replace("%>", "%\\>");
                        }

                        if fixed != original {
                            *example = Value::String(fixed);
                            fixes_count += 1;
                        }
                    }
                }

                // Recursively process all values in the mapping
                for (_, v) in map.iter_mut() {
                    fixes_count += self.apply_targeted_fixes(v)?;
                }
            }
            Value::Sequence(seq) => {
                for item in seq.iter_mut() {
                    fixes_count += self.apply_targeted_fixes(item)?;
                }
            }
            _ => {}
        }

        Ok(fixes_count)
    }
}

/// Generates Rust client code from the processed OpenAPI specification.
///
/// ## Code Generation Pipeline:
///
/// ### 1. YAML → JSON Conversion
/// Progenitor requires JSON input, so we convert the YAML Value to JSON.
/// Also saves a debug copy to `OUT_DIR/resolved_spec.json`.
///
/// ### 2. Parse to OpenAPI Struct
/// Deserialize JSON into `openapiv3::OpenAPI` struct for type-safe access.
/// This validates the spec structure and extracts metadata.
///
/// ### 3. Generate Proc-Macro Tokens
/// `progenitor::Generator::generate_tokens()` is the core code generator.
/// It produces a `TokenStream` containing Rust code as proc-macro tokens.
///
/// This is where progenitor can fail if the spec has issues like:
/// - Multiple success responses (caught by our deduplication)
/// - Invalid type references (caught by our reference resolution)
/// - Malformed schemas (caught by our fallbacks)
///
/// ### 4. Parse Tokens to Syn AST
/// Convert the raw token stream into a `syn::File` abstract syntax tree.
/// This allows for potential manipulation before code generation.
///
/// ### 5. Format with Prettyplease
/// `prettyplease::unparse()` converts the AST into nicely-formatted Rust code.
///
/// ### 6. Add Lint Suppressions
/// Prepends `#[allow(...)]` attributes to silence warnings in generated code.
/// Generated code often triggers clippy lints that aren't worth fixing.
///
/// ### 7. Fix Renamed Lints
/// Progenitor generates `#[allow(elided_named_lifetimes)]` which was renamed
/// to `#[allow(mismatched_lifetime_syntaxes)]` in newer Rust versions.
///
/// ## Output:
/// Returns a String containing ~700,000 lines of Rust code defining:
/// - `Client` struct with 500+ async methods
/// - `types` module with schema definitions
/// - Error types and response wrappers
fn generate_client_code(spec: &Value) -> Result<String, Box<dyn std::error::Error>> {
    println!("Generating Rust client code using progenitor...");

    // Convert YAML to JSON for progenitor
    let json_spec = serde_json::to_string_pretty(spec)?;

    // Debug: Save resolved spec to file for inspection
    if let Ok(out_dir) = std::env::var("OUT_DIR") {
        let debug_path = std::path::Path::new(&out_dir).join("resolved_spec.json");
        if let Err(e) = std::fs::write(&debug_path, &json_spec) {
            eprintln!("Warning: Failed to save debug spec: {}", e);
        } else {
            println!("Debug: Saved resolved spec to {}", debug_path.display());
        }
    }

    // Parse the OpenAPI spec
    println!("Parsing OpenAPI specification...");
    let openapi_spec: openapiv3::OpenAPI =
        match serde_json::from_str::<openapiv3::OpenAPI>(&json_spec) {
            Ok(spec) => {
                println!("Successfully parsed OpenAPI spec");
                println!("API Info: {} v{}", spec.info.title, spec.info.version);
                if let Some(components) = &spec.components {
                    println!("Found {} component schemas", components.schemas.len());
                }
                println!("Found {} API paths", spec.paths.paths.len());
                spec
            }
            Err(e) => {
                eprintln!("Failed to parse resolved OpenAPI spec: {}", e);
                return Err(format!("Failed to parse resolved OpenAPI spec: {}", e).into());
            }
        };

    // Generate the client using progenitor
    println!("Creating progenitor generator...");
    let mut generator = progenitor::Generator::default();

    println!("Starting token generation with progenitor...");
    let tokens = match generator.generate_tokens(&openapi_spec) {
        Ok(t) => {
            println!("Successfully generated tokens from OpenAPI spec");
            t
        }
        Err(e) => {
            eprintln!("Failed to generate tokens: {}", e);
            eprintln!("Error details: {:#?}", e);

            // Save additional debug information
            if let Ok(out_dir) = std::env::var("OUT_DIR") {
                let error_path = std::path::Path::new(&out_dir).join("generation_error.txt");
                let error_info = format!("Generation Error:\n{:#?}\n\nCaused by:\n{}", e, e);
                if let Err(write_err) = std::fs::write(&error_path, error_info) {
                    eprintln!("Warning: Failed to save error info: {}", write_err);
                } else {
                    println!("Debug: Saved error info to {}", error_path.display());
                }
            }

            return Err(e.into());
        }
    };

    println!("Parsing generated tokens into syntax tree...");
    let syntax_tree = match syn::parse2(tokens) {
        Ok(tree) => {
            println!("Successfully parsed generated tokens");
            tree
        }
        Err(e) => {
            eprintln!("Failed to parse generated tokens: {}", e);
            return Err(format!("Failed to parse generated tokens: {}", e).into());
        }
    };

    println!("Converting syntax tree to formatted code...");
    let mut code = prettyplease::unparse(&syntax_tree);

    // Add comprehensive lint suppressions at the top for generated code
    let lint_suppressions = r#"// Generated code - comprehensive lint suppressions
#[allow(warnings)]
#[allow(clippy::all)]
#[allow(rustdoc::all)]
#[allow(unused)]
#[allow(dead_code)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
#[allow(missing_docs)]
#[allow(missing_debug_implementations)]
#[allow(missing_copy_implementations)]
#[allow(trivial_casts)]
#[allow(trivial_numeric_casts)]
#[allow(unknown_lints)]
#[allow(unsafe_code)]
#[allow(unstable_features)]
#[allow(unused_import_braces)]
#[allow(unused_qualifications)]
#[allow(renamed_and_removed_lints)]
#[allow(mismatched_lifetime_syntaxes)]

"#;

    // Prepend the lint suppressions to the generated code
    code = format!("{}{}", lint_suppressions, code);

    // Fix renamed lint warnings in progenitor-generated code
    code = code.replace(
        "#[allow(elided_named_lifetimes)]",
        "#[allow(mismatched_lifetime_syntaxes)]",
    );

    println!(
        "Successfully generated {} characters of Rust client code (with lint suppressions)",
        code.len()
    );
    Ok(code)
}

/// Writes a minimal fallback client stub when code generation fails.
///
/// ## Why This Exists:
/// If the OpenAPI download, reference resolution, or progenitor code generation
/// fails for any reason, we don't want to completely break the build. Instead,
/// we generate a minimal stub that:
///
/// 1. Allows the crate to compile (preventing build failures)
/// 2. Provides basic `Client` structure
/// 3. Includes type definitions for common patterns
/// 4. Documents what went wrong in the comments
///
/// ## What's Included:
/// - `Client` struct with `new_with_client()` constructor
/// - `types` module with common types (Response, Links, ErrorResponse)
/// - `Error` enum with basic error variants
/// - `ResponseValue<T>` wrapper
///
/// ## Limitations:
/// The stub doesn't include any actual API methods. Users will get compile
/// errors if they try to call methods like `client.droplets_list()`.
///
/// ## When This Runs:
/// Only when one of these stages fails:
/// - OpenAPI spec download (network error, GitHub down)
/// - Reference resolution (spec corruption, file missing)
/// - Progenitor code generation (incompatible spec changes)
fn write_stub_client(output_path: &Path) {
    let stub_content = r#"
// Generated DigitalOcean API client (enhanced stub)
//
// Reference resolution: ✅ SUCCESS - All OpenAPI $ref directives resolved
// Code generation: ❌ FAILED - Progenitor encountered issues with the resolved spec
//
// This enhanced stub provides the basic client structure. The OpenAPI reference
// resolution is working correctly, downloading and processing the full DigitalOcean
// specification. However, the resolved spec (19MB JSON) appears to contain constructs
// that the current version of progenitor cannot handle.

/// Enhanced client implementation with authentication support
#[derive(Debug, Clone)]
pub struct Client {
    base_url: String,
    client: reqwest::Client,
}

impl Client {
    /// Create a new client with the specified base URL and HTTP client
    pub fn new_with_client(base_url: impl Into<String>, client: reqwest::Client) -> Self {
        Self {
            base_url: base_url.into(),
            client,
        }
    }
    
    /// Get the base URL
    pub fn baseurl(&self) -> &str {
        &self.base_url
    }
    
    /// Get a reference to the underlying HTTP client
    pub fn client(&self) -> &reqwest::Client {
        &self.client
    }
}

/// Types module with common DigitalOcean API types
pub mod types {
    use serde::{Deserialize, Serialize};
    
    /// Generic response wrapper used by DigitalOcean API
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct Response<T> {
        pub data: T,
    }
    
    /// Pagination links structure
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct Links {
        pub pages: Option<Pages>,
    }
    
    /// Page navigation links  
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct Pages {
        pub first: Option<String>,
        pub prev: Option<String>,
        pub next: Option<String>,
        pub last: Option<String>,
    }
    
    /// Standard error response from DigitalOcean API
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct ErrorResponse {
        pub id: String,
        pub message: String,
        pub request_id: Option<String>,
    }
}

/// Comprehensive error types for DigitalOcean API
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Request error: {0}")]
    RequestError(#[from] reqwest::Error),
    
    #[error("Response error: {status} - {content}")]
    ResponseError {
        status: reqwest::StatusCode,
        content: String,
    },
    
    #[error("Authentication error: {0}")]
    AuthError(String),
    
    #[error("Rate limit exceeded: {0}")]
    RateLimitError(String),
    
    #[error("Other error: {0}")]
    Other(String),
}

/// Response value wrapper with additional metadata
pub struct ResponseValue<T> {
    inner: T,
    status: reqwest::StatusCode,
    headers: reqwest::header::HeaderMap,
}

impl<T> ResponseValue<T> {
    pub fn into_inner(self) -> T {
        self.inner
    }
    
    pub fn status(&self) -> reqwest::StatusCode {
        self.status
    }
    
    pub fn headers(&self) -> &reqwest::header::HeaderMap {
        &self.headers
    }
}
"#;

    fs::write(output_path, stub_content)
        .unwrap_or_else(|e| panic!("Failed to write stub client code: {}", e));
}