crozier 0.0.126

Ultra-fast OpenAPI-to-SDK code generator that matches Fern's output, byte for byte.
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
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
//! Rendering the IR to Python source files with minijinja.
//!
//! Three layers: Rust computes the semantics (resolved type expressions, the
//! exact import block, per-field annotations and defaults); minijinja templates
//! (`templates/`) lay out the file shape — e.g. `class_body.py.j2` arranges a
//! pydantic model's docstring, fields, and config block, applying the Fern
//! blank-line rules `ruff` does not impose; and `ruff format` (see [`crate::pyfmt`])
//! handles the line wrapping. The template layout is covered directly by render
//! tests (see this module's `tests`), so it iterates without the full pipeline.

use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;

use minijinja::{context, Environment};
use serde::Serialize;

use crate::error::{Error, Result};
use crate::ir::{
    is_json_like_media_type, Auth, BodyField, BodySchemaShape, ClientPathParameter, Credential,
    Endpoint, EndpointPagination, ErrorClass, Field, GlobalHeader, HeaderType, Ir, ObjectType,
    PageAdvance, Prim, QueryParam, RequestBody, StreamProtocol, TagTypeDecl, TypeDecl, TypeRef,
};
use crate::naming;
use crate::settings::{EnumType, ExtraFields, Layout};
use crate::wrap::Doc;

mod xml;

/// The header comment crozier writes atop every generated file. It differs from
/// Fern's by design; the comment-stripping normalizer removes it before any
/// comparison, so the wording is free.
const HEADER: &str = "# This file was auto-generated by crozier from an OpenAPI document.";

/// Whether a string a YAML document supplied as an example would have been
/// resolved to a *timestamp* rather than to a string, had it been written
/// unquoted. Fern's YAML parser follows YAML 1.1 here, so an unquoted
/// `2022-01-23T19:00:00.000Z` reaches its example generator as a date and is not
/// a value a plain `str` field can take — VTEX writes its `dateRange` example
/// that way and its golden falls back to the field-name placeholder, while the
/// same document written as JSON keeps the string (probed at Fern 5.20.0 both
/// ways; not re-measured at the certified pair).
/// The quoting itself does not survive deserialization, so this reads the value.
fn yaml_resolves_as_timestamp(value: &str) -> bool {
    let bytes = value.as_bytes();
    // `yyyy-mm-dd`, then optionally `T`/space and at least `hh:mm`.
    if bytes.len() < 10 {
        return false;
    }
    let digits = |range: std::ops::Range<usize>| bytes[range].iter().all(u8::is_ascii_digit);
    if !(digits(0..4) && bytes[4] == b'-' && digits(5..7) && bytes[7] == b'-' && digits(8..10)) {
        return false;
    }
    match bytes.len() {
        10 => true,
        _ => {
            matches!(bytes[10], b'T' | b't' | b' ')
                && bytes.len() >= 16
                && digits(11..13)
                && bytes[13] == b':'
                && digits(14..16)
        }
    }
}

/// Escape a spec-controlled value for embedding inside a double-quoted Python
/// string literal, so a `"`/`\`/newline in the input cannot break out of (or
/// inject into) the generated source. Ordinary values (URLs, header names) pass
/// through unchanged, so byte-exact output is unaffected.
fn escape_py_str(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
}

/// A generated file: a repo-relative path and its contents.
#[derive(Debug, Clone)]
pub struct GeneratedFile {
    /// Path relative to the output root.
    pub path: PathBuf,
    /// File contents.
    pub contents: String,
}

/// Python standard-library top-level modules crozier may import. Used to split
/// imports into Fern's two ordered groups (stdlib first, everything else second).
const STDLIB_MODULES: &[&str] = &[
    "typing",
    "datetime",
    "uuid",
    "importlib",
    "enum",
    "os",
    "sys",
    "json",
    "base64",
    "collections",
    "decimal",
    "abc",
    "contextlib",
    "logging",
];

/// The location of the file whose imports are being collected, relative to the
/// package root — it decides the relative path to a referenced generated type
/// (how many leading dots, and whether via a tag's `types/` subpackage).
#[derive(Clone, Default)]
enum RefLoc {
    /// A generated file at the package root (`src/{pkg}/client.py` or
    /// `src/{pkg}/raw_client.py`).
    PackageRoot,
    /// A module in the package-root `types/` package (`src/{pkg}/types/`).
    #[default]
    RootTypes,
    /// A module in a tag's `types/` package (`src/{pkg}/{tag}/types/`).
    TagTypes(String),
    /// A tag client file (`src/{pkg}/{tag}/raw_client.py` or `client.py`).
    Client(String),
    /// The `errors/` package (`src/{pkg}/errors/`).
    Errors,
}

/// How many directory segments a client module name holds. A module is normally
/// one (`videos`), but an `x-crozier-sdk-group-name` list nests it
/// (`catalogs/mcp_servers`), and the package root is zero.
fn module_depth(module: &str) -> usize {
    if module.is_empty() {
        0
    } else {
        module.split('/').count()
    }
}

/// A module name as a dotted Python path (`catalogs/mcp_servers` ->
/// `catalogs.mcp_servers`).
fn module_path(module: &str) -> String {
    module.replace('/', ".")
}

/// The last directory segment of a client module — the one its class names are
/// built from. Fern names a nested client after its own segment
/// (`catalogs/mcp_servers` -> `McpServersClient`), not after the whole path.
fn module_stem(module: &str) -> &str {
    module.rsplit('/').next().unwrap_or(module)
}

/// Every client package the module list implies, child lists included: the modules
/// that own endpoints plus the *intermediate* packages a nested
/// `x-crozier-sdk-group-name` creates, which own none but still carry a client
/// exposing their children.
///
/// A package's children are ordered the way Fern writes them: those that own
/// endpoints first, in the order the document declares them, then the pure parents
/// sorted by name. `catalogs`, `internal` and `settings` land after `agents` …
/// `skills` in TrueForge's root client for exactly that reason.
fn module_children(endpoint_modules: &[String]) -> BTreeMap<String, Vec<String>> {
    let owns_endpoints: BTreeSet<&str> = endpoint_modules.iter().map(String::as_str).collect();
    let mut owning: BTreeMap<String, Vec<String>> = BTreeMap::new();
    let mut parents: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
    for module in endpoint_modules {
        let mut path = String::new();
        for segment in module.split('/') {
            let parent = path.clone();
            if !path.is_empty() {
                path.push('/');
            }
            path.push_str(segment);
            if owns_endpoints.contains(path.as_str()) {
                let siblings = owning.entry(parent).or_default();
                if !siblings.contains(&path) {
                    siblings.push(path.clone());
                }
            } else {
                parents.entry(parent).or_default().insert(path.clone());
            }
        }
    }
    let mut children: BTreeMap<String, Vec<String>> = owning;
    for (parent, pure) in parents {
        children.entry(parent).or_default().extend(pure);
    }
    children
}

/// Parse an example literal as JSON, reading the Python spellings `True`, `False`
/// and `None` outside strings as JSON's: a field's example literal is rendered
/// for Python, and Zulip's `subscription_data` example carries `True` inside a
/// list of objects.
fn parse_example_value(example: &str) -> Option<serde_json::Value> {
    if let Ok(value) = serde_json::from_str(example) {
        return Some(value);
    }
    let mut json = String::with_capacity(example.len());
    let mut in_string = false;
    let mut escaped = false;
    let mut rest = example;
    while let Some(c) = rest.chars().next() {
        if in_string {
            json.push(c);
            if escaped {
                escaped = false;
            } else if c == '\\' {
                escaped = true;
            } else if c == '"' {
                in_string = false;
            }
            rest = &rest[c.len_utf8()..];
            continue;
        }
        let word = [("True", "true"), ("False", "false"), ("None", "null")]
            .into_iter()
            .find(|(python, _)| rest.starts_with(python));
        if let Some((python, spelled)) = word {
            json.push_str(spelled);
            rest = &rest[python.len()..];
            continue;
        }
        if c == '"' {
            in_string = true;
        }
        json.push(c);
        rest = &rest[c.len_utf8()..];
    }
    serde_json::from_str(&json).ok()
}

/// The value Fern substitutes for a `date-time` example it cannot read.
const FERN_DEFAULT_DATETIME: &str = "2024-01-15T09:30:00+00:00";

/// A declared `date-time` example as a worked call writes it: the instant it
/// names, in UTC, as `YYYY-MM-DDTHH:MM:SS+00:00` (a fraction of a second is
/// dropped). Measured against `fernapi/fern-python-sdk` 5.20.0 (at the certified
/// pair, Fergus's golden and the hand-written `unread-date-time-examples`
/// fixture re-verify an offset, a zone name and `Z`), Fern reads the value after
/// appending `Z` unless it already ends in `Z` or holds a `+`, so Fergus's
/// `2025-12-02T00:00:00+13:00` passes `2025-12-01 11:00:00+00:00`, a value with
/// no zone (`2023-03-04 05:06:07`, `2023-03-04`) is read as UTC and a day past
/// the month's end rolls into the next month. A value that reading rejects is
/// replaced by [`FERN_DEFAULT_DATETIME`]: a zone name (`… CDT`), but equally a
/// negative offset or a lower-case `z`, which the appended `Z` breaks. Every
/// value returned parses with `datetime.datetime.fromisoformat`.
fn datetime_example(value: &str) -> String {
    let rewritten = if value.ends_with('Z') || value.contains('+') {
        value.to_string()
    } else {
        format!("{value}Z")
    };
    let Some(seconds) = read_datetime(&rewritten) else {
        return FERN_DEFAULT_DATETIME.to_string();
    };
    let (days, second_of_day) = (
        seconds.div_euclid(86_400) + 719_468,
        seconds.rem_euclid(86_400),
    );
    // `civil_from_days` (Howard Hinnant), from days since 0000-03-01.
    let era = days.div_euclid(146_097);
    let day_of_era = days - era * 146_097;
    let year_of_era =
        (day_of_era - day_of_era / 1460 + day_of_era / 36524 - day_of_era / 146_096) / 365;
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
    let month_index = (5 * day_of_year + 2) / 153;
    let day = day_of_year - (153 * month_index + 2) / 5 + 1;
    let month = if month_index < 10 {
        month_index + 3
    } else {
        month_index - 9
    };
    let year = year_of_era + era * 400 + i64::from(month <= 2);
    format!(
        "{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}+00:00",
        second_of_day / 3600,
        second_of_day / 60 % 60,
        second_of_day % 60
    )
}

/// The instant Fern reads from its rewritten `date-time` value, in whole seconds
/// since the Unix epoch, or `None` where it reads none. The grammar is the one
/// measured: a date alone or a date and a time (`T`, `t` or a space between, seconds
/// and a fraction optional, the fraction dropped), then `Z` or a `±HH:MM` or
/// `±HHMM` offset; any day up to 31, rolling past the month's end.
fn read_datetime(value: &str) -> Option<i64> {
    let bytes = value.as_bytes();
    let number = |range: std::ops::Range<usize>| value.get(range).and_then(number_of);
    let (year, month, day) = (number(0..4)?, number(5..7)?, number(8..10)?);
    if bytes.get(4) != Some(&b'-') || bytes.get(7) != Some(&b'-') {
        return None;
    }
    if !(1..=12).contains(&month) || !(1..=31).contains(&day) {
        return None;
    }
    let mut at = 10;
    let (mut hour, mut minute, mut second) = (0, 0, 0);
    if matches!(bytes.get(at), Some(b'T' | b't' | b' ')) {
        hour = number(at + 1..at + 3)?;
        minute = number(at + 4..at + 6)?;
        if bytes.get(at + 3) != Some(&b':') {
            return None;
        }
        at += 6;
        if bytes.get(at) == Some(&b':') {
            second = number(at + 1..at + 3)?;
            at += 3;
            if bytes.get(at) == Some(&b'.') {
                let digits = bytes[at + 1..]
                    .iter()
                    .take_while(|byte| byte.is_ascii_digit())
                    .count();
                if digits == 0 {
                    return None;
                }
                at += 1 + digits;
            }
        }
    }
    if hour > 23 || minute > 59 || second > 59 {
        return None;
    }
    let zone = value.get(at..)?;
    let offset_minutes = if zone == "Z" {
        0
    } else {
        let sign = match zone.as_bytes().first()? {
            b'+' => 1,
            b'-' => -1,
            _ => return None,
        };
        let (hours, minutes) = match zone.len() {
            6 if zone.as_bytes()[3] == b':' => (zone.get(1..3)?, zone.get(4..6)?),
            5 => (zone.get(1..3)?, zone.get(3..5)?),
            _ => return None,
        };
        let (hours, minutes) = (number_of(hours)?, number_of(minutes)?);
        if hours > 23 || minutes > 59 {
            return None;
        }
        sign * (hours * 60 + minutes)
    };
    // `days_from_civil` (Howard Hinnant); a day past the month's end simply
    // counts on into the next month.
    let shifted_year = if month <= 2 { year - 1 } else { year };
    let era = shifted_year.div_euclid(400);
    let year_of_era = shifted_year - era * 400;
    let day_of_year = (153 * (month + if month > 2 { -3 } else { 9 }) + 2) / 5 + day - 1;
    let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
    let days = era * 146_097 + day_of_era - 719_468;
    Some(days * 86_400 + hour * 3600 + (minute - offset_minutes) * 60 + second)
}

/// `digits` as a number, when it is nothing but ASCII digits.
fn number_of(digits: &str) -> Option<i64> {
    (!digits.is_empty() && digits.bytes().all(|byte| byte.is_ascii_digit()))
        .then(|| digits.parse().ok())
        .flatten()
}

/// Collects imports and renders them in Fern's order: group 1 is stdlib
/// (`import`s then `from`s), group 2 is everything else (`import`s then `from`s),
/// separated by a blank line. Names within a `from` and the statements within a
/// group are sorted.
#[derive(Default)]
struct Imports {
    /// Plain `import module [as alias]`, keyed by module for sorting.
    plain: BTreeMap<String, Option<String>>,
    /// A multipart argument can shadow `json` in one method while another
    /// method still needs the unaliased module in the same generated file.
    json_module_alias: bool,
    /// `from module import a, b`, module -> sorted names.
    from: BTreeMap<String, BTreeSet<String>>,
    /// This file's location, for resolving relative imports of generated types.
    loc: RefLoc,
    /// Names of tag-scoped (hoisted) types → their owning tag directory. A name
    /// absent from this map is a package-root type.
    tag_types: BTreeMap<String, String>,
    /// Referenced type names that must render as string forward references
    /// (`"TreeNode"`) because importing them eagerly would close an import cycle
    /// (issue #84). A same-file self-reference needs no import at all; a
    /// cross-module cycle import is deferred to [`Imports::deferred`].
    forward: std::collections::HashSet<String>,
    /// The module (file stem) of the type being emitted, so a forward reference to
    /// a type in *this* file is recognised and left un-imported.
    cur_module: String,
    /// `from … import …` lines for forward references in *other* modules, emitted
    /// after the class body (a deferred import) so the module loads without the
    /// cycle firing at import time.
    deferred: BTreeSet<String>,
    /// First-registration order for deferred imports. Fern keeps direct field
    /// references ahead of the additional namespace used to repair a cycle;
    /// aliases still use [`Self::deferred`]'s sorted order under `TYPE_CHECKING`.
    deferred_order: Vec<String>,
    /// `(module, name)` → the local alias to import it under and to write at every
    /// occurrence. Populated from [`Self::collision_aliases`] on a throwaway
    /// first pass: two modules can supply the same simple name — Eozilla declares
    /// a component schema called `ApiError` beside crozier's own core one — and
    /// Python has no way to hold both, so Fern imports *both* under a
    /// module-qualified alias (`core_api_error_ApiError`,
    /// `types_api_error_ApiError`) rather than picking a winner.
    aliases: BTreeMap<(String, String), String>,
}

impl Imports {
    /// A fresh collector for a file at `loc`, aware of the tag-scoped type map so
    /// references resolve to the right relative path.
    fn at(loc: RefLoc, tag_types: &BTreeMap<String, String>) -> Self {
        Imports {
            loc,
            tag_types: tag_types.clone(),
            ..Default::default()
        }
    }

    /// The dotted prefix that reaches the generated package root from this file.
    /// A module name may be a *nested* client path (`catalogs/mcp_servers`, from an
    /// `x-crozier-sdk-group-name` list), so the dot count is measured from the
    /// module's own depth rather than assumed to be one.
    fn root_prefix(&self) -> String {
        let depth = match &self.loc {
            RefLoc::PackageRoot => 0,
            RefLoc::RootTypes | RefLoc::Errors => 1,
            RefLoc::Client(module) => module_depth(module),
            RefLoc::TagTypes(module) => module_depth(module) + 1,
        };
        ".".repeat(depth + 1)
    }

    /// The package-relative dotted module a generated type lives in —
    /// `types.config`, or `{tag}.types.{module}` for a tag-scoped one — whatever
    /// file imports it.
    fn package_module_path(&self, class: &str) -> String {
        let m = naming::module_name(class);
        match self.tag_types.get(class) {
            Some(tag) if !tag.is_empty() => format!("{}.types.{m}", module_path(tag)),
            _ => format!("types.{m}"),
        }
    }

    /// The relative module path a referenced generated type is imported from,
    /// chosen from this file's [`RefLoc`] and whether the type is tag-scoped.
    fn type_import_path(&self, class: &str) -> String {
        let m = naming::module_name(class);
        let root = self.root_prefix();
        match self.tag_types.get(class) {
            // A package-root type lives at `{pkg}.types.{m}`; so does a tag type
            // whose owning tag is the package root itself.
            Some(tag) if tag.is_empty() => match &self.loc {
                RefLoc::RootTypes => format!(".{m}"),
                _ => format!("{root}types.{m}"),
            },
            // A tag-scoped type lives at `{pkg}.{tag}.types.{m}`.
            Some(tag) => match &self.loc {
                RefLoc::TagTypes(cur) if cur == tag => format!(".{m}"),
                RefLoc::Client(cur) if cur == tag => format!(".types.{m}"),
                _ => format!("{root}{}.types.{m}", module_path(tag)),
            },
            // A package-root type lives at `{pkg}.types.{m}`.
            None => match &self.loc {
                RefLoc::RootTypes => format!(".{m}"),
                _ => format!("{root}types.{m}"),
            },
        }
    }

    /// Register the import for a referenced generated type. A type flagged as a
    /// forward reference (issue #84) is *not* imported eagerly: a same-file
    /// reference needs no import, and a cross-module one is deferred to after the
    /// class body so its cycle does not fire at import time.
    fn add_type(&mut self, class: &str) -> String {
        if self.forward.contains(class) {
            if naming::module_name(class) != self.cur_module {
                let path = self.type_import_path(class);
                let line = format!("from {path} import {class}");
                if self.deferred.insert(line.clone()) {
                    self.deferred_order.push(line);
                }
            }
            return class.to_string();
        }
        let path = self.type_import_path(class);
        self.add_from(&path, class);
        self.local(&path, class)
    }

    /// The name `name` is written as in this file: itself, or the alias a
    /// same-name collision forced on it.
    fn local(&self, module: &str, name: &str) -> String {
        self.aliases
            .get(&(module.to_string(), name.to_string()))
            .cloned()
            .unwrap_or_else(|| name.to_string())
    }

    /// The name a `core` helper is written as, without importing it: the raw
    /// clients import the response wrappers file by file (see
    /// `raw_client_file`), so a method naming one only reads its alias.
    fn core_local(&self, submodule: &str, name: &str) -> String {
        self.local(&format!("{}.{submodule}", self.core_prefix()), name)
    }

    /// Register a `core` helper and return the name to write for it.
    fn add_core_local(&mut self, submodule: &str, name: &str) -> String {
        self.add_core(submodule, name);
        self.local(&format!("{}.{submodule}", self.core_prefix()), name)
    }

    /// The aliases a second pass must use: every simple name this file would
    /// import from more than one module, aliased in *all* of them (Fern renames
    /// both sides, not just the later one).
    fn collision_aliases(&self) -> BTreeMap<(String, String), String> {
        let mut modules_by_name: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
        for (module, names) in &self.from {
            for name in names {
                modules_by_name
                    .entry(name.as_str())
                    .or_default()
                    .push(module.as_str());
            }
        }
        let mut aliases = BTreeMap::new();
        for (name, modules) in modules_by_name {
            if modules.len() < 2 {
                continue;
            }
            for module in modules {
                let stem = module.trim_start_matches('.').replace('.', "_");
                aliases.insert(
                    (module.to_string(), name.to_string()),
                    format!("{stem}_{name}"),
                );
            }
        }
        aliases
    }

    /// The dotted prefix that reaches the package-root `core` package from this
    /// file's location. Most generated files sit one package below the root
    /// (`{pkg}/types/`, `{pkg}/{tag}/`, `{pkg}/errors/`); a tag's `types/` is two
    /// deep, and a nested client path (`{pkg}/catalogs/mcp_servers/`) is as deep as
    /// its own segments, so the count comes from [`Imports::root_prefix`].
    fn core_prefix(&self) -> String {
        format!("{}core", self.root_prefix())
    }

    fn error_import_path(&self, module: &str) -> String {
        format!("{}errors.{module}", self.root_prefix())
    }

    /// Register a `from {..}core.{submodule} import {name}` at the depth this
    /// file's location requires — so `core.*` helpers resolve uniformly rather
    /// than with a hardcoded dot count that only holds at the root nesting level
    /// (issue #85).
    fn add_core(&mut self, submodule: &str, name: &str) {
        let module = format!("{}.{submodule}", self.core_prefix());
        self.add_from(&module, name);
    }

    fn add_core_package(&mut self) {
        let prefix = self.core_prefix();
        let parent = prefix.trim_end_matches("core");
        self.add_from(parent, "core");
    }

    fn add_plain(&mut self, module: &str) {
        self.plain.entry(module.to_string()).or_insert(None);
    }

    fn add_plain_as(&mut self, module: &str, alias: &str) {
        self.plain
            .insert(module.to_string(), Some(alias.to_string()));
    }

    fn json_module(&mut self, shadowed: bool) -> &'static str {
        if shadowed {
            self.json_module_alias = true;
            "_json"
        } else {
            self.add_plain("json");
            "json"
        }
    }

    fn add_from(&mut self, module: &str, name: &str) {
        self.from
            .entry(module.to_string())
            .or_default()
            .insert(name.to_string());
    }

    /// Register a `from module import name` and return the name to write for it.
    fn add_from_local(&mut self, module: &str, name: &str) -> String {
        self.add_from(module, name);
        self.local(module, name)
    }

    /// Is a module (or a `from` path's leading segment) part of the stdlib group?
    /// Relative imports (leading `.`) are never stdlib.
    fn is_stdlib(module: &str) -> bool {
        if module.starts_with('.') {
            return false;
        }
        let top = module.split('.').next().unwrap_or(module);
        STDLIB_MODULES.contains(&top)
    }

    fn render(&self) -> String {
        let (mut g1, mut g2): (Vec<String>, Vec<String>) = (Vec::new(), Vec::new());

        // Fern orders generated modules naturally (`type9` before `type10`), while
        // the maps provide deterministic collection and de-duplication.
        let mut plain: Vec<_> = self
            .plain
            .iter()
            .map(|(module, alias)| (module.as_str(), alias.as_deref()))
            .collect();
        if self.json_module_alias {
            plain.push(("json", Some("_json")));
        }
        plain.sort_by(|(a, alias_a), (b, alias_b)| {
            natural_cmp(a, b).then_with(|| alias_a.cmp(alias_b))
        });
        plain.dedup();
        for (module, alias) in plain {
            let line = match alias {
                Some(a) => format!("import {module} as {a}"),
                None => format!("import {module}"),
            };
            if Self::is_stdlib(module) {
                g1.push(line);
            } else {
                g2.push(line);
            }
        }
        // Then `from` imports within each group.
        let mut from: Vec<_> = self.from.iter().collect();
        from.sort_by(|(a, _), (b, _)| natural_cmp(a, b));
        for (module, names) in from {
            // An aliased name takes a line of its own after the module's plain
            // names, as ruff's isort writes it (`combine-as-imports` off):
            // MockServer's raw clients import `AsyncHttpResponse`, then
            // `HttpResponse as core_http_response_HttpResponse`.
            let (aliased, plain): (Vec<&String>, Vec<&String>) = names.iter().partition(|name| {
                self.aliases
                    .contains_key(&((*module).clone(), (*name).clone()))
            });
            let mut lines = Vec::new();
            if !plain.is_empty() {
                let joined = plain
                    .iter()
                    .map(|name| name.as_str())
                    .collect::<Vec<_>>()
                    .join(", ");
                lines.push(format!("from {module} import {joined}"));
            }
            for name in aliased {
                let alias = &self.aliases[&(module.clone(), name.clone())];
                lines.push(format!("from {module} import {name} as {alias}"));
            }
            for line in lines {
                if Self::is_stdlib(module) {
                    g1.push(line);
                } else {
                    g2.push(line);
                }
            }
        }

        let groups: Vec<String> = [g1, g2]
            .into_iter()
            .filter(|g| !g.is_empty())
            .map(|g| g.join("\n"))
            .collect();
        groups.join("\n\n")
    }
}

/// Compare strings with digit runs treated as integers, matching Fern's import
/// ordering for generated names with numeric suffixes (`item9`, `item10`).
fn natural_cmp(left: &str, right: &str) -> Ordering {
    let (mut left_index, mut right_index) = (0, 0);
    let (left_bytes, right_bytes) = (left.as_bytes(), right.as_bytes());
    while left_index < left_bytes.len() && right_index < right_bytes.len() {
        let left_digit = left_bytes[left_index].is_ascii_digit();
        let right_digit = right_bytes[right_index].is_ascii_digit();
        if left_digit && right_digit {
            let left_end = left_index
                + left_bytes[left_index..]
                    .iter()
                    .take_while(|byte| byte.is_ascii_digit())
                    .count();
            let right_end = right_index
                + right_bytes[right_index..]
                    .iter()
                    .take_while(|byte| byte.is_ascii_digit())
                    .count();
            let left_digits = &left[left_index..left_end];
            let right_digits = &right[right_index..right_end];
            let left_value = left_digits.trim_start_matches('0');
            let right_value = right_digits.trim_start_matches('0');
            let ordering = left_value
                .len()
                .cmp(&right_value.len())
                .then_with(|| left_value.cmp(right_value))
                .then_with(|| left_digits.len().cmp(&right_digits.len()));
            if ordering != Ordering::Equal {
                return ordering;
            }
            left_index = left_end;
            right_index = right_end;
        } else {
            let ordering = left_bytes[left_index].cmp(&right_bytes[right_index]);
            if ordering != Ordering::Equal {
                return ordering;
            }
            left_index += 1;
            right_index += 1;
        }
    }
    left_bytes.len().cmp(&right_bytes.len())
}

fn example_import_cmp(left: &str, right: &str) -> Ordering {
    let left_prefix = left.split_once('_').map(|(prefix, _)| prefix);
    let right_prefix = right.split_once('_').map(|(prefix, _)| prefix);
    // Two wrappers of one union (`PartsItem_Text`, `PartsItem_Data`) are
    // ordered by isort below, as LangChain's `post_a2a` docstring imports them.
    if left_prefix.is_some() && left_prefix == right_prefix {
        return left
            .to_ascii_lowercase()
            .cmp(&right.to_ascii_lowercase())
            .then_with(|| left.cmp(right));
    }
    if left_prefix.is_some_and(|prefix| right.starts_with(prefix)) {
        return Ordering::Less;
    }
    if right_prefix.is_some_and(|prefix| left.starts_with(prefix)) {
        return Ordering::Greater;
    }
    // isort orders the names case-insensitively, as Fern's examples do:
    // `OcmGroupsCluster` comes before `OcmGroupUser`.
    left.to_ascii_lowercase()
        .cmp(&right.to_ascii_lowercase())
        .then_with(|| left.cmp(right))
}

/// Render a resolved type to a [`Doc`] expression, registering needed imports.
/// The `Doc` renders flat; `ruff format` handles any line wrapping downstream.
fn render_type(t: &TypeRef, imports: &mut Imports) -> Doc {
    match t {
        TypeRef::Primitive(p) => match p {
            Prim::Str => Doc::atom("str"),
            Prim::Bytes => Doc::atom("bytes"),
            Prim::Int | Prim::Long => Doc::atom("int"),
            Prim::Float => Doc::atom("float"),
            Prim::Bool => Doc::atom("bool"),
            Prim::LiteralBool(value) => {
                imports.add_plain("typing");
                Doc::atom(format!(
                    "typing.Literal[{}]",
                    if *value { "True" } else { "False" }
                ))
            }
            Prim::Any => {
                imports.add_plain("typing");
                Doc::atom("typing.Any")
            }
            Prim::Datetime => {
                imports.add_plain_as("datetime", "dt");
                Doc::atom("dt.datetime")
            }
            Prim::Date => {
                imports.add_plain_as("datetime", "dt");
                Doc::atom("dt.date")
            }
        },
        TypeRef::Named(class) => {
            let local = imports.add_type(class);
            // A forward reference (issue #84) renders as a quoted string so the
            // annotation needs no eager import of a not-yet-defined name.
            if imports.forward.contains(class) {
                Doc::atom(format!("\"{class}\""))
            } else {
                Doc::atom(local)
            }
        }
        TypeRef::Optional(inner) => {
            imports.add_plain("typing");
            Doc::group("typing.Optional[", vec![render_type(inner, imports)], "]")
        }
        TypeRef::Literal(values) => {
            imports.add_plain("typing");
            let items = values
                .iter()
                .map(|v| Doc::atom(format!("\"{v}\"")))
                .collect();
            Doc::group("typing.Literal[", items, "]")
        }
        TypeRef::List(inner) => {
            imports.add_plain("typing");
            Doc::group("typing.List[", vec![render_type(inner, imports)], "]")
        }
        TypeRef::Set(inner) => {
            imports.add_plain("typing");
            Doc::group("typing.Set[", vec![render_type(inner, imports)], "]")
        }
        TypeRef::Dict(k, v) => {
            imports.add_plain("typing");
            Doc::group(
                "typing.Dict[",
                vec![render_type(k, imports), render_type(v, imports)],
                "]",
            )
        }
        TypeRef::Union(variants) => {
            imports.add_plain("typing");
            let items = variants.iter().map(|v| render_type(v, imports)).collect();
            Doc::group("typing.Union[", items, "]")
        }
    }
}

/// A field prepared for emission: its Python name, resolved annotation, the
/// `= <default>` suffix, and an optional docstring.
struct RenderedField {
    py_name: String,
    annotation: Doc,
    default: String,
    docstring: Option<String>,
}

/// The view model handed to `object.py.j2`. Rust computes the semantics (imports,
/// per-field annotation strings, defaults); the template lays out the class body
/// (field order, docstrings, the blank-line rules `ruff` does not impose) and
/// `ruff format` handles the line wrapping. This split keeps the byte-sensitive
/// logic in testable Rust while the file's *shape* lives in the template.
#[derive(Serialize)]
struct ObjectView {
    header: &'static str,
    imports: String,
    class_name: String,
    bases: String,
    /// The class docstring's raw text (the template applies the `docstring` filter).
    docstring: Option<String>,
    fields: Vec<FieldView>,
    /// The `pydantic.ConfigDict(...)` argument list for the v2 `model_config`
    /// (`extra="allow", frozen=True`, or just `frozen=True` for `ignore`).
    config_dict_args: String,
    /// The v1 `Config`'s `pydantic.Extra.<extra_kind>` member (`allow`/`ignore`/
    /// `forbid`).
    extra_kind: &'static str,
    /// Whether the module opens with `from __future__ import annotations` (an
    /// XML model names its own class in its method signatures).
    future: bool,
    /// Methods rendered after the fields (an XML model's), class-body indented.
    members: Option<String>,
    /// Whether the v1 `Config` sets `copy_on_model_validation = "none"`.
    copy_on_model_validation: bool,
}

/// One field as the object template consumes it: the flat declaration line
/// (`name: annotation = default`, wrapped later by `ruff`) and the raw docstring.
#[derive(Serialize)]
struct FieldView {
    decl: String,
    docstring: Option<String>,
}

/// Compute a field's annotation + default, registering imports.
fn render_field(field: &Field, imports: &mut Imports) -> RenderedField {
    let inner = render_type(&field.type_ref, imports);
    let typ = if field.optional && !matches!(field.type_ref, TypeRef::Optional(_)) {
        imports.add_plain("typing");
        Doc::group("typing.Optional[", vec![inner], "]")
    } else {
        inner
    };

    // Fern carries the wire alias in both its serialization metadata and a
    // pydantic field marker. The former drives crozier's write-side converter;
    // the latter makes direct pydantic construction/deserialization honor it.
    let annotation = if field.needs_alias() {
        imports.add_plain("pydantic");
        imports.add_plain("typing_extensions");
        imports.add_core("serialization", "FieldMetadata");
        let pydantic_field = field.docstring.as_ref().map_or_else(
            || Doc::atom(format!("pydantic.Field(alias=\"{}\")", field.wire_name)),
            |description| {
                Doc::group(
                    "pydantic.Field(",
                    vec![
                        Doc::atom(format!("alias=\"{}\"", field.wire_name)),
                        Doc::atom(format!("description=\"{}\"", escape_py_str(description))),
                    ],
                    ")",
                )
            },
        );
        Doc::group(
            "typing_extensions.Annotated[",
            vec![
                typ,
                Doc::atom(format!("FieldMetadata(alias=\"{}\")", field.wire_name)),
                pydantic_field,
            ],
            "]",
        )
    } else {
        typ
    };

    RenderedField {
        py_name: field.py_name.clone(),
        annotation,
        default: field_default(field, imports),
        docstring: field.docstring.clone(),
    }
}

/// The `= <default>` suffix for a field. A documented field carries a
/// `pydantic.Field` (with `default=None` when optional); an undocumented
/// optional field defaults to `None`; a required undocumented field has none.
fn field_default(field: &Field, imports: &mut Imports) -> String {
    // An aliased field already carries its pydantic.Field in Annotated. Adding a
    // second Field as the default changes pydantic's metadata merge semantics and
    // diverges from Fern; optional aliases use the ordinary None default.
    if field.needs_alias() {
        return if field.optional {
            " = None".to_string()
        } else {
            String::new()
        };
    }
    if field.docstring.is_some() {
        imports.add_plain("pydantic");
        if field.optional {
            " = pydantic.Field(default=None)".to_string()
        } else {
            " = pydantic.Field()".to_string()
        }
    } else if field.optional {
        " = None".to_string()
    } else {
        String::new()
    }
}

/// Build the minijinja environment with the embedded templates.
fn environment() -> Environment<'static> {
    let mut env = Environment::new();
    env.set_keep_trailing_newline(true);
    env.set_trim_blocks(true);
    env.set_lstrip_blocks(true);
    // Render a raw docstring into an indented, backslash-escaped triple-quoted
    // block; templates apply it with `{{ text | docstring }}`.
    env.add_filter("docstring", |text: String| render_docstring(&text, 4));
    env.add_template("version.py", include_str!("../templates/version.py.j2"))
        .expect("version template compiles");
    env.add_template("object.py", include_str!("../templates/object.py.j2"))
        .expect("object template compiles");
    env.add_template(
        "class_body.py",
        include_str!("../templates/class_body.py.j2"),
    )
    .expect("class_body template compiles");
    env.add_template("alias.py", include_str!("../templates/alias.py.j2"))
        .expect("alias template compiles");
    env.add_template(
        "raw_client.py",
        include_str!("../templates/raw_client.py.j2"),
    )
    .expect("raw_client template compiles");
    env.add_template("error.py", include_str!("../templates/error.py.j2"))
        .expect("error template compiles");
    env.add_template("lazy_init.py", include_str!("../templates/lazy_init.py.j2"))
        .expect("lazy_init template compiles");
    env.add_template(
        "reference_entry.md",
        include_str!("../templates/reference_entry.md.j2"),
    )
    .expect("reference_entry template compiles");
    env.add_template(
        "root_client.py",
        include_str!("../templates/root_client.py.j2"),
    )
    .expect("root_client template compiles");
    env.add_template(
        "client_class.py",
        include_str!("../templates/client_class.py.j2"),
    )
    .expect("client_class template compiles");
    env.add_template("file.py", include_str!("../templates/file.py.j2"))
        .expect("file template compiles");
    env
}

/// Generate every file crozier currently emits for an IR.
/// Collect every named-type reference reachable within a resolved type.
fn collect_named_refs(t: &TypeRef, out: &mut Vec<String>) {
    match t {
        TypeRef::Named(n) => out.push(n.clone()),
        TypeRef::Optional(i) | TypeRef::List(i) | TypeRef::Set(i) => collect_named_refs(i, out),
        TypeRef::Dict(k, v) => {
            collect_named_refs(k, out);
            collect_named_refs(v, out);
        }
        TypeRef::Union(vs) => vs.iter().for_each(|v| collect_named_refs(v, out)),
        TypeRef::Primitive(_) | TypeRef::Literal(_) => {}
    }
}

/// The named types a declaration references (base classes plus every field's
/// type), for building the type-reference graph that finds recursive cycles.
pub(crate) fn decl_refs(decl: &TypeDecl) -> Vec<String> {
    let mut out = Vec::new();
    match decl {
        TypeDecl::Object(o) => {
            out.extend(o.bases.iter().cloned());
            out.extend(o.reach_refs.iter().cloned());
            for f in &o.fields {
                collect_named_refs(&f.type_ref, &mut out);
            }
        }
        TypeDecl::Alias(a) => {
            collect_named_refs(&a.target, &mut out);
            for reach in &a.reach_refs {
                collect_named_refs(reach, &mut out);
            }
        }
        TypeDecl::DiscriminatedUnion(u) => {
            // A union can be any of its mapped variant schemas — that edge is what
            // exposes a variant that recurses back through the union (issue #84).
            out.extend(u.variant_targets.iter().cloned());
            for m in &u.members {
                for f in &m.fields {
                    collect_named_refs(&f.type_ref, &mut out);
                }
            }
        }
        TypeDecl::Enum(_) => {}
    }
    out
}

/// Named types that occur in a declaration's emitted annotations. Unlike the
/// cycle-detection graph, a discriminated union excludes its source variant
/// targets: Fern emits wrapper classes, so those source models are not names
/// `update_forward_refs` must resolve for the union module.
fn decl_annotation_refs(decl: &TypeDecl) -> Vec<String> {
    let mut out = Vec::new();
    match decl {
        TypeDecl::Object(object) => {
            out.extend(object.bases.iter().cloned());
            out.extend(object.reach_refs.iter().cloned());
            for field in &object.fields {
                collect_named_refs(&field.type_ref, &mut out);
            }
        }
        TypeDecl::Alias(alias) => collect_named_refs(&alias.target, &mut out),
        TypeDecl::DiscriminatedUnion(union) => {
            for member in &union.members {
                for field in &member.fields {
                    collect_named_refs(&field.type_ref, &mut out);
                }
            }
        }
        TypeDecl::Enum(_) => {}
    }
    out
}

/// A declaration's references excluding an object's base classes — what the
/// declaration itself annotates.
fn decl_own_refs(decl: &TypeDecl) -> Vec<String> {
    match decl {
        TypeDecl::Object(object) => {
            let mut out = Vec::new();
            for field in &object.fields {
                collect_named_refs(&field.type_ref, &mut out);
            }
            out
        }
        _ => decl_refs(decl),
    }
}

/// Whether an alias's target is a union expression, looking through `Optional`.
fn is_union_target(t: &TypeRef) -> bool {
    match t {
        TypeRef::Union(_) => true,
        TypeRef::Optional(inner) => is_union_target(inner),
        _ => false,
    }
}

/// For every generated type, the subset of its own references that close a cycle
/// back to it — the references crozier must emit as string forward references
/// with a deferred/omitted import so the module graph loads (issue #84).
///
/// A reference `A → B` is a back-edge when `B` can reach `A` again through the
/// type graph (a self-reference is the degenerate `A → A`). Fern renders exactly
/// these as `"B"` and repairs them at class-definition time with
/// `update_forward_refs`.
fn forward_ref_map(
    types: &[TypeDecl],
    tag_types: &[crate::ir::TagTypeDecl],
) -> std::collections::HashMap<String, std::collections::HashSet<String>> {
    use std::collections::{HashMap, HashSet};

    // Adjacency: type name → the names it references.
    let mut edges: HashMap<String, Vec<String>> = HashMap::new();
    for decl in types.iter().chain(tag_types.iter().map(|t| &t.decl)) {
        edges
            .entry(decl.name().to_string())
            .or_default()
            .extend(decl_refs(decl));
    }

    // Whether `target` is reachable from `start` following ≥1 edge.
    let reaches = |start: &str, target: &str| -> bool {
        let mut stack: Vec<&str> = edges
            .get(start)
            .into_iter()
            .flatten()
            .map(String::as_str)
            .collect();
        let mut seen: HashSet<&str> = HashSet::new();
        while let Some(node) = stack.pop() {
            if node == target {
                return true;
            }
            if seen.insert(node) {
                stack.extend(edges.get(node).into_iter().flatten().map(String::as_str));
            }
        }
        false
    };

    let mut map: HashMap<String, HashSet<String>> = HashMap::new();
    for decl in types.iter().chain(tag_types.iter().map(|t| &t.decl)) {
        let name = decl.name();
        let defer_recursive_target =
            !matches!(decl, TypeDecl::Alias(_) | TypeDecl::DiscriminatedUnion(_));
        let mut forward = HashSet::new();
        for r in decl_refs(decl) {
            // A superclass must already be bound when Python executes the class
            // statement; its own module repairs its recursive annotations.
            if matches!(decl, TypeDecl::Object(object) if object.bases.contains(&r)) {
                continue;
            }
            // Render `r` as a string forward reference (deferred import) when it
            // closes a cycle back to this type, OR when `r` is itself recursive (in a
            // cycle of its own). Importing a recursive type eagerly can trip its
            // module's own import cycle even from an acyclic referrer, so Fern defers
            // every reference *into* a cycle, not only the back-edges of one.
            // An alias that merely *wraps* its target imports it eagerly even when
            // the target points back at the alias — Fern emits such an alias with
            // no forward-reference machinery at all, not even
            // `from __future__ import annotations`: Apideck Accounting's
            // `ProfitAndLossRecords = Optional[List[ProfitAndLossRecordsItem]]`
            // over a discriminated union, and MockServer's
            // `SchemaArray = List[Schema]` over the draft-04 `Schema` model. An
            // alias whose target is itself a `Union[..]` is a declaration in its
            // own right and defers into the cycle like any other member, which is
            // how EN 18222's `MultiValuedDataElementValueItem` quotes
            // `DataElement`.
            let wrapper_alias = match decl {
                TypeDecl::Alias(alias) => !is_union_target(&alias.target),
                _ => false,
            };
            if !wrapper_alias && (reaches(&r, name) || (defer_recursive_target && reaches(&r, &r)))
            {
                forward.insert(r);
            }
        }
        if !forward.is_empty() {
            map.insert(name.to_string(), forward);
        }
    }
    map
}

/// For each non-alias declaration that reaches a recursive type, collect every
/// cyclic name in its forward-reference closure. Fern passes that complete set to
/// `update_forward_refs`, including names reached transitively through aliases.
/// An object excludes its own class (already the call's first argument), while a
/// discriminated union retains its alias name because the first argument is a
/// generated variant wrapper.
#[derive(Default)]
struct ForwardRepair {
    names: std::collections::HashSet<String>,
    /// For a discriminated union, the exact `update_forward_refs` arguments each
    /// wrapper class takes, keyed by wrapper class name. Wrappers differ: each
    /// resolves what its own fields reach, minus the model it flattened.
    member_names: std::collections::HashMap<String, Vec<String>>,
    triggers: std::collections::HashSet<String>,
    import_order: Vec<String>,
    /// For an object, the order Fern writes its trailing deferred imports in:
    /// field by field, each named type contributing first the cycle it closes
    /// (sorted, minus the model itself) and then every non-union cyclic type it
    /// reaches (sorted), each name kept at its first position. Fern registers
    /// those as unused ghost imports and its lint pass prunes the ones nothing
    /// names, so only the order survives: a model whose `wharf` field closes
    /// one cycle and whose later `buoy` field closes another imports `Wharf`
    /// ahead of `Buoy`.
    deferred_order: Vec<String>,
    /// Whether the declaration reaches a cycle through its *own* annotations
    /// rather than only through a base class. Eozilla's `QualifiedValue` extends
    /// `Format`, whose `schema` field closes the `Schema` map-of-self cycle, and
    /// Fern gives the subclass `from __future__ import annotations` but no
    /// `update_forward_refs` call — the base's own module already repaired it.
    own_reach: bool,
}

/// Each node of the type-reference graph mapped to the id of its strongly
/// connected component (Tarjan's algorithm, iterative so a deep reference chain
/// cannot exhaust the stack). Two names share an id exactly when each reaches
/// the other, so a cycle is one id however many cycles the graph chains.
fn cycle_components(
    edges: &std::collections::HashMap<String, Vec<String>>,
) -> std::collections::HashMap<String, usize> {
    use std::collections::HashMap;

    let mut names: Vec<&str> = edges.keys().map(String::as_str).collect();
    names.sort_unstable();
    let mut index: HashMap<&str, usize> = HashMap::new();
    let mut low: HashMap<&str, usize> = HashMap::new();
    let mut on_stack: std::collections::HashSet<&str> = std::collections::HashSet::new();
    let mut stack: Vec<&str> = Vec::new();
    let mut component: HashMap<String, usize> = HashMap::new();
    let mut next_index = 0;
    let mut next_component = 0;
    let neighbours = |node: &str| -> Vec<&str> {
        edges
            .get(node)
            .into_iter()
            .flatten()
            .map(String::as_str)
            .filter(|name| edges.contains_key(*name))
            .collect()
    };
    for root in names {
        if index.contains_key(root) {
            continue;
        }
        // Each frame is a node and the neighbours it has still to visit.
        let mut frames: Vec<(&str, Vec<&str>)> = Vec::new();
        index.insert(root, next_index);
        low.insert(root, next_index);
        next_index += 1;
        stack.push(root);
        on_stack.insert(root);
        frames.push((root, neighbours(root)));
        while let Some((node, pending)) = frames.last_mut() {
            let node = *node;
            if let Some(next) = pending.pop() {
                if !index.contains_key(next) {
                    index.insert(next, next_index);
                    low.insert(next, next_index);
                    next_index += 1;
                    stack.push(next);
                    on_stack.insert(next);
                    frames.push((next, neighbours(next)));
                } else if on_stack.contains(next) {
                    let reached = index[next];
                    let entry = low.get_mut(node).expect("visited node has a low-link");
                    *entry = (*entry).min(reached);
                }
                continue;
            }
            frames.pop();
            let node_low = low[node];
            if let Some((parent, _)) = frames.last() {
                let entry = low.get_mut(*parent).expect("visited node has a low-link");
                *entry = (*entry).min(node_low);
            }
            if node_low == index[node] {
                while let Some(member) = stack.pop() {
                    on_stack.remove(member);
                    component.insert(member.to_string(), next_component);
                    if member == node {
                        break;
                    }
                }
                next_component += 1;
            }
        }
    }
    component
}

/// [`ForwardRepair::deferred_order`] for one object: the names its trailing
/// deferred imports follow, field by field.
fn object_deferred_order(
    object: &ObjectType,
    edges: &std::collections::HashMap<String, Vec<String>>,
    cyclic: &std::collections::HashSet<&str>,
    unions: &std::collections::HashSet<&str>,
    component: &std::collections::HashMap<String, usize>,
) -> Vec<String> {
    let mut order: Vec<String> = Vec::new();
    let push = |order: &mut Vec<String>, name: &str| {
        if !order.iter().any(|known| known == name) {
            order.push(name.to_string());
        }
    };
    for base in &object.bases {
        push(&mut order, base);
    }
    let mut annotated = Vec::new();
    for field in &object.fields {
        let mut refs = Vec::new();
        collect_named_refs(&field.type_ref, &mut refs);
        for name in &refs {
            // The cycle this name closes, without the model being written.
            let mut cycle: Vec<&str> = match component.get(name.as_str()) {
                Some(id) if cyclic.contains(name.as_str()) => component
                    .iter()
                    .filter(|(member, member_id)| {
                        *member_id == id && member.as_str() != object.name
                    })
                    .map(|(member, _)| member.as_str())
                    .collect(),
                _ => Vec::new(),
            };
            cycle.sort_by(|left, right| natural_cmp(left, right));
            // Every non-union cyclic declaration the name reaches.
            let mut reached: Vec<&str> = Vec::new();
            let mut stack = vec![name.as_str()];
            let mut seen = std::collections::HashSet::new();
            while let Some(node) = stack.pop() {
                if seen.insert(node) {
                    if cyclic.contains(node) && !unions.contains(node) && node != object.name {
                        reached.push(node);
                    }
                    stack.extend(edges.get(node).into_iter().flatten().map(String::as_str));
                }
            }
            reached.sort_by(|left, right| natural_cmp(left, right));
            for member in cycle.into_iter().chain(reached) {
                push(&mut order, member);
            }
        }
        annotated.extend(refs);
    }
    // The field annotations themselves follow the ghost references.
    for name in &annotated {
        push(&mut order, name);
    }
    order
}

fn forward_repair_map(
    types: &[TypeDecl],
    tag_types: &[crate::ir::TagTypeDecl],
) -> std::collections::HashMap<String, ForwardRepair> {
    use std::collections::{HashMap, HashSet};

    let mut edges: HashMap<String, Vec<String>> = HashMap::new();
    let mut repair_edges: HashMap<String, Vec<String>> = HashMap::new();
    let mut walk_edges: HashMap<String, Vec<String>> = HashMap::new();
    for decl in types.iter().chain(tag_types.iter().map(|t| &t.decl)) {
        edges
            .entry(decl.name().to_string())
            .or_default()
            .extend(decl_refs(decl));
        repair_edges
            .entry(decl.name().to_string())
            .or_default()
            .extend(decl_annotation_refs(decl));
        walk_edges
            .entry(decl.name().to_string())
            .or_default()
            .extend(match decl {
                TypeDecl::DiscriminatedUnion(union) => union.variant_targets.clone(),
                other => decl_refs(other),
            });
    }
    let reaches = |start: &str, target: &str| -> bool {
        let mut stack: Vec<&str> = edges
            .get(start)
            .into_iter()
            .flatten()
            .map(String::as_str)
            .collect();
        let mut seen: HashSet<&str> = HashSet::new();
        while let Some(node) = stack.pop() {
            if node == target {
                return true;
            }
            if seen.insert(node) {
                stack.extend(edges.get(node).into_iter().flatten().map(String::as_str));
            }
        }
        false
    };

    let cyclic: HashSet<&str> = edges
        .keys()
        .map(String::as_str)
        .filter(|n| reaches(n, n))
        .collect();
    let aliases: HashSet<&str> = types
        .iter()
        .chain(tag_types.iter().map(|tag| &tag.decl))
        .filter_map(|decl| matches!(decl, TypeDecl::Alias(_)).then_some(decl.name()))
        .collect();
    let unions: HashSet<&str> = types
        .iter()
        .chain(tag_types.iter().map(|tag| &tag.decl))
        .filter_map(|decl| {
            let union = match decl {
                TypeDecl::DiscriminatedUnion(_) => true,
                TypeDecl::Alias(alias) => is_union_target(&alias.target),
                _ => false,
            };
            union.then_some(decl.name())
        })
        .collect();
    let component = cycle_components(&edges);
    let same_cycle = |left: &str, right: &str| {
        component
            .get(left)
            .is_some_and(|id| component.get(right) == Some(id))
    };
    let classes: HashSet<&str> = types
        .iter()
        .chain(tag_types.iter().map(|tag| &tag.decl))
        .filter_map(|decl| matches!(decl, TypeDecl::Object(_)).then_some(decl.name()))
        .collect();
    // Whether a declaration needs `from __future__ import annotations` at all.
    // Naming a cyclic type directly always does. Reaching one only through other
    // declarations does when the cyclic type is a *class*: a class in a cycle is
    // still incomplete when its referrer is defined, so the referrer is rebuilt
    // too — dnd5eapi's `Monster` names `MonsterActionsItem`, which names the
    // cyclic `Choice`. A cyclic *alias* is not: it is a finished object by the
    // time the module that names it repairs it, so nothing beyond that module is
    // affected — NDW's `Feature` repairs the `FeatureProperties` union and its own
    // referrer `FeatureCollection` needs no repair of its own.
    let reaches_repairable = |starts: &[String]| -> bool {
        if starts.iter().any(|name| cyclic.contains(name.as_str())) {
            return true;
        }
        let mut stack: Vec<&str> = starts.iter().map(String::as_str).collect();
        let mut seen: HashSet<&str> = HashSet::new();
        while let Some(node) = stack.pop() {
            if cyclic.contains(node) && classes.contains(node) {
                return true;
            }
            if seen.insert(node) {
                stack.extend(edges.get(node).into_iter().flatten().map(String::as_str));
            }
        }
        false
    };

    let mut out = HashMap::new();
    for decl in types.iter().chain(tag_types.iter().map(|tag| &tag.decl)) {
        if matches!(decl, TypeDecl::Alias(_)) {
            continue;
        }
        let reaches_cycle =
            reaches_repairable(edges.get(decl.name()).map_or(&[][..], Vec::as_slice));
        // The same question asked of this declaration's own annotations only: a
        // base class is imported eagerly and repaired in its own module, so
        // inheriting a cyclic field does not make the subclass call
        // `update_forward_refs`.
        let own_reach = reaches_repairable(&decl_own_refs(decl));

        // The names one declaration's `update_forward_refs` call must resolve: for
        // each cyclic name its own annotations reach, the cycle that name closes,
        // walking on **through that cycle's members only**. A non-cyclic node is
        // a leaf — whatever it references is repaired in its own module, not
        // here — which is why NDW's `FeatureCollection` names nothing (it
        // annotates `Feature`, which is not cyclic) while its `Feature` names
        // `FeatureProperties` directly. So is a node of a *different* cycle the
        // walk could reach: a model naming `Captain`, whose cycle with `Vessel`
        // points into a self-recursive `Harbor`, resolves `Captain` and `Vessel`
        // and leaves `Harbor` to the modules of the cycle that closes it.
        //
        // A union is walked by its *variant targets*, not by its wrappers' fields.
        // The wrappers are flattened views of those models, so a field they
        // flattened is only a name to resolve when the model it came from is
        // itself cyclic — the walk reaches it through that model. EN 18222's
        // `DataElement` reaches `MultiValuedDataElementValueItem` through the
        // cyclic `MultiValuedDataElement`, while NDW's `FeatureProperties` does not
        // reach `Reason`, because the only wrapper carrying it flattened the
        // acyclic `DestinationFeatureProperties`.
        let closure = |starts: Vec<&str>| -> HashSet<String> {
            let mut found = HashSet::new();
            for start in starts.into_iter().filter(|name| cyclic.contains(name)) {
                let mut stack = vec![start];
                let mut seen = HashSet::new();
                while let Some(node) = stack.pop() {
                    if seen.insert(node) {
                        found.insert(node.to_string());
                        stack.extend(
                            walk_edges
                                .get(node)
                                .into_iter()
                                .flatten()
                                .map(String::as_str)
                                .filter(|name| same_cycle(start, name)),
                        );
                    }
                }
            }
            found
        };
        let mut member_names: HashMap<String, Vec<String>> = HashMap::new();
        let mut repair: HashSet<String> = if let TypeDecl::DiscriminatedUnion(union) = decl {
            // Each wrapper resolves its own reach, minus the model it flattened —
            // a wrapper cannot be asked to resolve its own source.
            let mut all = HashSet::new();
            for (index, member) in union.members.iter().enumerate() {
                let mut refs = Vec::new();
                for field in &member.fields {
                    collect_named_refs(&field.type_ref, &mut refs);
                }
                let mut names = closure(refs.iter().map(String::as_str).collect());
                if let Some(source) = member
                    .source
                    .as_ref()
                    .or_else(|| union.variant_targets.get(index).filter(|_| !member.wrapped))
                {
                    names.remove(source);
                }
                let mut ordered: Vec<String> = names.iter().cloned().collect();
                ordered.sort_by(|left, right| natural_cmp(left, right));
                all.extend(names);
                member_names.insert(member.class_name.clone(), ordered);
            }
            all.extend(
                union
                    .variant_targets
                    .iter()
                    .filter(|target| cyclic.contains(target.as_str()))
                    .cloned(),
            );
            all
        } else {
            let mut names = closure(
                repair_edges
                    .get(decl.name())
                    .into_iter()
                    .flatten()
                    .map(String::as_str)
                    .collect(),
            );
            names.remove(decl.name());
            names
        };
        if matches!(decl, TypeDecl::Object(_)) {
            repair.remove(decl.name());
        }
        if reaches_cycle {
            let triggers = edges
                .get(decl.name())
                .into_iter()
                .flatten()
                .filter(|name| {
                    cyclic.contains(name.as_str())
                        || cyclic.iter().any(|target| reaches(name.as_str(), target))
                })
                .cloned()
                .collect();
            let mut import_order: Vec<String> = repair.iter().cloned().collect();
            import_order.sort_by(|left, right| {
                aliases
                    .contains(left.as_str())
                    .cmp(&aliases.contains(right.as_str()))
                    .then_with(|| natural_cmp(left, right))
            });
            let deferred_order = match decl {
                TypeDecl::Object(object) => {
                    object_deferred_order(object, &edges, &cyclic, &unions, &component)
                }
                _ => Vec::new(),
            };
            out.insert(
                decl.name().to_string(),
                ForwardRepair {
                    names: repair,
                    member_names,
                    triggers,
                    import_order,
                    deferred_order,
                    own_reach,
                },
            );
        }
    }
    out
}

pub fn generate(ir: &Ir) -> Result<Vec<GeneratedFile>> {
    let env = environment();
    let mut files = Vec::new();
    let pkg = &ir.package_name;

    // Map each tag-scoped (hoisted) type name to its owning tag directory, so
    // references from anywhere resolve to the right relative import path.
    let tag_map: BTreeMap<String, String> = ir
        .tag_types
        .iter()
        .map(|t| (t.decl.name().to_string(), t.module.clone()))
        .collect();

    // Recursive types: per type, the references that must render as string forward
    // references (issue #84). Empty for the common acyclic case.
    let forward_map = forward_ref_map(&ir.types, &ir.tag_types);
    let repair_map = forward_repair_map(&ir.types, &ir.tag_types);
    let empty_forward = std::collections::HashSet::new();

    // version.py and the py.typed marker are packaging: Fern's flat module tree
    // carries neither.
    let packaged = ir.layout == Layout::Packaged;
    if packaged {
        let version = render(
            &env,
            "version.py",
            "version.py",
            context! { project_name => ir.project_name },
        )?;
        files.push(GeneratedFile {
            path: PathBuf::from(format!("src/{pkg}/version.py")),
            contents: version,
        });

        // py.typed (an empty marker file).
        files.push(GeneratedFile {
            path: PathBuf::from(format!("src/{pkg}/py.typed")),
            contents: String::new(),
        });
    }

    // The client package tree the module names imply. A module is normally one
    // directory (`videos`); an `x-crozier-sdk-group-name` list nests it
    // (`catalogs/mcp_servers`), which adds intermediate packages that own no
    // endpoints and exist only to carry their children.
    let children = module_children(&ir.endpoint_modules);
    let parent_modules: Vec<String> = children
        .keys()
        .filter(|module| !module.is_empty() && !ir.endpoint_modules.contains(module))
        .cloned()
        .collect();

    // One file per generated type.
    let decl_settings = DeclSettings {
        extra: ir.extra_fields,
        enum_type: ir.enum_type,
        xml_types: xml::XmlTypes::new(&ir.types, &ir.tag_types),
    };
    for decl in &ir.types {
        let forward = forward_map.get(decl.name()).unwrap_or(&empty_forward);
        let repair = repair_map.get(decl.name());
        let file = render_type_decl(
            &env,
            decl,
            RefLoc::RootTypes,
            &tag_map,
            decl_settings,
            forward,
            repair,
        )?;
        files.push(GeneratedFile {
            path: PathBuf::from(format!("src/{pkg}/types/{}.py", decl.module())),
            contents: file,
        });
    }

    // Tag-scoped hoisted types (inline request/response bodies), each in its tag's
    // own `types/` package, plus a lazy-loader `__init__.py` per such package.
    let mut tag_type_modules: BTreeMap<&str, Vec<&TypeDecl>> = BTreeMap::new();
    for tt in &ir.tag_types {
        tag_type_modules
            .entry(&tt.module)
            .or_default()
            .push(&tt.decl);
    }
    for (module, decls) in &tag_type_modules {
        for decl in decls {
            let forward = forward_map.get(decl.name()).unwrap_or(&empty_forward);
            let repair = repair_map.get(decl.name());
            let location = if module.is_empty() {
                RefLoc::RootTypes
            } else {
                RefLoc::TagTypes((*module).to_string())
            };
            let file = render_type_decl(
                &env,
                decl,
                location,
                &tag_map,
                decl_settings,
                forward,
                repair,
            )?;
            files.push(GeneratedFile {
                path: PathBuf::from(format!("src/{pkg}/{module}/types/{}.py", decl.module())),
                contents: file,
            });
        }
        // The root `types/` package has one aggregator, written below over the
        // component types *and* these hoisted ones; a second one here would land
        // on the same path and lose whichever was written first.
        if !module.is_empty() {
            files.push(tag_types_init_file(&env, pkg, module, decls)?);
        }
    }

    // Fern's static core runtime, emitted verbatim (see assets/README.md), plus
    // the auth-shaped `client_wrapper.py`.
    files.extend(core_files(
        pkg,
        ir.endpoints
            .iter()
            .any(|endpoint| endpoint.pagination.is_some()),
        ir.enum_type,
        &ir.auth,
    ));
    // An XML model reads and writes itself through `core/xml_utilities.py`, which
    // Fern ships (unexported) only with an SDK that has one, together with a
    // round-trip test module per model.
    if decl_settings.xml_types.any() {
        files.push(GeneratedFile {
            path: PathBuf::from(format!("src/{pkg}/core/xml_utilities.py")),
            contents: xml::XML_UTILITIES.to_string(),
        });
        files.extend(xml::test_files(
            pkg,
            decl_settings.xml_types,
            ir.layout == Layout::Packaged || pkg == "fern",
        ));
    }
    // Fern's flat tree has no publishing identity, so its wrapper sends no
    // `SDK-Name`/`SDK-Version` headers.
    files.push(client_wrapper_file(
        pkg,
        packaged.then_some(ir.project_name.as_str()),
        &ir.auth,
        &ir.global_headers,
        &ir.client_path_parameters,
        ir.default_max_retries,
        ir.environment.as_ref(),
    ));

    // `environment.py`: the server-environment enum, when the document declares
    // `servers`.
    if let Some(environment) = &ir.environment {
        files.push(environment_file(&env, pkg, environment)?);
    }

    // One package marker per endpoint client module. Fern's `__init__.py` here is
    // a comment-only header (four blank lines once stripped) — unless the tag owns
    // hoisted inline types, in which case it is a lazy loader re-exporting them,
    // or it has nested sub-packages, in which case it is a lazy loader over those.
    for module in ir.endpoint_modules.iter().chain(parent_modules.iter()) {
        let child_types = |names: &[String]| -> Vec<(String, Vec<String>)> {
            names
                .iter()
                .filter_map(|child| {
                    let decls = tag_type_modules.get(child.as_str())?;
                    let mut exported: Vec<String> = decls
                        .iter()
                        .flat_map(|decl| decl.exported_names().into_iter().map(str::to_string))
                        .collect();
                    exported.sort();
                    Some((module_stem(child).to_string(), exported))
                })
                .collect()
        };
        if let Some(decls) = tag_type_modules.get(module.as_str()) {
            let names = children.get(module.as_str()).cloned().unwrap_or_default();
            files.push(tag_pkg_init_file(
                &env,
                pkg,
                module,
                decls,
                &names,
                &child_types(&names),
            )?);
        } else if let Some(names) = children.get(module.as_str()) {
            let child_types = child_types(names);
            files.push(module_pkg_init_file(
                &env,
                pkg,
                module,
                names,
                &child_types,
            )?);
        } else {
            files.push(GeneratedFile {
                path: PathBuf::from(format!("src/{pkg}/{module}/__init__.py")),
                contents: "\n\n\n\n".to_string(),
            });
        }
    }

    // A package that only holds types (a component placed by its SDK group or
    // `x-tags`) has no client, so no module above wrote its `__init__.py`.
    let type_only_modules: Vec<String> = tag_type_modules
        .keys()
        .filter(|module| {
            !module.is_empty()
                && !ir.endpoint_modules.iter().any(|m| m == *module)
                && !parent_modules.iter().any(|m| m == *module)
        })
        .map(|module| (*module).to_string())
        .collect();
    for module in &type_only_modules {
        files.push(tag_pkg_init_file(
            &env,
            pkg,
            module,
            &tag_type_modules[module.as_str()],
            &[],
            &[],
        )?);
    }

    let root_eps: Vec<&Endpoint> = ir
        .endpoints
        .iter()
        .filter(|e| e.module.is_empty() && e.emittable)
        .collect();
    let root_emittable = !root_eps.is_empty();
    if root_emittable {
        files.push(raw_client_file(
            &env,
            pkg,
            &ir.client_name,
            "",
            &root_eps,
            &tag_map,
        )?);
    }

    // Per-tag `raw_client.py` and `client.py`, but only for modules whose every
    // operation is within the subset crozier emits today (see
    // `Endpoint::emittable`); the rest await wider endpoint support. The modules
    // that clear this bar (in sorted order) also drive the root client below.
    let mut emittable_modules: Vec<&String> = Vec::new();
    for module in &ir.endpoint_modules {
        let eps: Vec<&Endpoint> = ir
            .endpoints
            .iter()
            .filter(|e| &e.module == module)
            .collect();
        if !eps.is_empty() && eps.iter().all(|e| e.emittable) {
            files.push(raw_client_file(
                &env,
                pkg,
                &ir.client_name,
                module,
                &eps,
                &tag_map,
            )?);
            let cx = ClientCtx {
                yaml_unquoted_timestamps: ir.yaml_unquoted_timestamps.as_ref(),
                pkg,
                client_name: &ir.client_name,
                module,
                types: &ir.types,
                enum_type: ir.enum_type,
                tag_decls: &ir.tag_types,
                auth: &ir.auth,
                has_environment: ir.environment.is_some(),
                tag_types: &tag_map,
                global_headers: &ir.global_headers,
                client_path_parameters: &ir.client_path_parameters,
                sdk_first_party: packaged || pkg == "fern",
                children: children.get(module.as_str()).map_or(&[][..], Vec::as_slice),
            };
            files.push(client_file(&env, &cx, &eps)?);
            emittable_modules.push(module);
        }
    }
    // A nested `x-crozier-sdk-group-name` creates *intermediate* packages that own
    // no operation of their own (`catalogs` over `catalogs/mcp_servers`). Fern
    // still emits a client for each: an empty raw client, and a high-level one
    // whose whole body is the lazy sub-client properties.
    for module in &parent_modules {
        files.push(raw_client_file(
            &env,
            pkg,
            &ir.client_name,
            module,
            &[],
            &tag_map,
        )?);
        let cx = ClientCtx {
            yaml_unquoted_timestamps: ir.yaml_unquoted_timestamps.as_ref(),
            pkg,
            client_name: &ir.client_name,
            module,
            types: &ir.types,
            enum_type: ir.enum_type,
            tag_decls: &ir.tag_types,
            auth: &ir.auth,
            has_environment: ir.environment.is_some(),
            tag_types: &tag_map,
            global_headers: &ir.global_headers,
            client_path_parameters: &ir.client_path_parameters,
            sdk_first_party: packaged || pkg == "fern",
            children: children.get(module.as_str()).map_or(&[][..], Vec::as_slice),
        };
        files.push(client_file(&env, &cx, &[])?);
    }
    // The root client aggregates only the *top-level* packages; a nested one is
    // reached through its parent's property instead.
    let root_modules: Vec<&String> = children.get("").map_or(Vec::new(), |names| {
        names
            .iter()
            .filter(|name| emittable_modules.contains(name) || parent_modules.contains(*name))
            .collect()
    });
    // `emittable_modules` stays in first-appearance (declaration) order — the order
    // `ir.endpoint_modules` yields — because that is the order Fern lists sub-clients
    // in the root client and `reference.md` (e.g. `widgets` before `gadgets` when
    // `/widgets` is declared first, not alphabetically). The root `__init__.py`
    // re-sorts its own dynamic-import/`__all__` maps, so its output is unaffected.

    // Root client: `FernApi`/`AsyncFernApi` aggregating the tag clients. Fern emits
    // it unconditionally — an API whose operations it all discarded still gets a
    // client carrying the credential constructor and no methods
    // (`cyberark-conjur-api`) — so there is no emptiness guard here.
    {
        files.push(root_client_file(
            &env,
            RootClientFileCtx {
                yaml_unquoted_timestamps: ir.yaml_unquoted_timestamps.as_ref(),
                pkg,
                client_name: &ir.client_name,
                modules: &root_modules,
                root_endpoints: if root_emittable { &root_eps } else { &[] },
                types: &ir.types,
                enum_type: ir.enum_type,
                tag_decls: &ir.tag_types,
                tag_map: &tag_map,
                auth: &ir.auth,
                environment: ir.environment.as_ref(),
                global_headers: &ir.global_headers,
                client_path_parameters: &ir.client_path_parameters,
                default_max_retries: ir.default_max_retries,
            },
        )?);
    }

    // The `errors/` package: one exception class per distinct declared error,
    // plus the lazy-loading `__init__.py` aggregator. Emitted only when there are
    // errors to declare.
    files.extend(error_files(&env, pkg, &ir.errors, &tag_map)?);

    // Package aggregators: `types/__init__.py` over the type layer, and the
    // package-root `__init__.py` re-exporting types, errors, the endpoint
    // submodules, the root client, and `__version__`.
    let root_tag_types: &[&TypeDecl] = tag_type_modules.get("").map_or(&[], Vec::as_slice);
    if !ir.types.is_empty() || !root_tag_types.is_empty() {
        files.push(types_init_file(&env, pkg, &ir.types, root_tag_types)?);
    }
    // The root package lists a types-only package beside the client ones.
    let mut init_modules = root_modules.clone();
    init_modules.extend(
        type_only_modules
            .iter()
            .filter(|module| !module.contains('/')),
    );
    files.push(root_init_file(&env, pkg, ir, &init_modules)?);

    // Generated `README.md` (usage examples from the first endpoint) and the
    // per-endpoint `reference.md`.
    // Fern's packaged tree has `README.md` whether or not it has a call to
    // demonstrate: an API whose every operation it discarded gets the file empty
    // (`cyberark-conjur-api`). Its flat module tree has no such file at all.
    match readme_file(ir) {
        Some(readme) => files.push(readme),
        None if ir.layout == Layout::Packaged => files.push(GeneratedFile {
            path: PathBuf::from("README.md"),
            contents: String::new(),
        }),
        None => {}
    }
    files.push(reference_file(&env, ir, &reference_modules(ir), &tag_map)?);

    // Project-root scaffolding (pyproject.toml, requirements.txt, metadata).
    files.extend(scaffolding_files(pkg, &ir.project_name, ir.layout));

    // Final wrapping is delegated to `ruff format` (the tool Fern runs), so the
    // emitters above produce content-correct Python without reproducing ruff's
    // line layout. `.py` files are formatted in place; non-Python outputs
    // (Markdown, TOML, JSON, the `py.typed` marker) are left untouched.
    format_python_files(pkg, &mut files)?;

    if ir.layout == Layout::Flat {
        flatten_package_paths(pkg, &mut files);
    }

    Ok(files)
}

/// Move the package's modules from `src/<pkg>/` to the output root, which is
/// where Fern's flat tree (a `local-file-system` output) puts them. Everything
/// else — `README.md`, `reference.md`, `.fern/`, `tests/` — is already at the root
/// in both layouts.
fn flatten_package_paths(pkg: &str, files: &mut [GeneratedFile]) {
    let package_root = PathBuf::from(format!("src/{pkg}"));
    for file in files.iter_mut() {
        if let Ok(rest) = file.path.strip_prefix(&package_root) {
            file.path = rest.to_path_buf();
        }
    }
}

/// Generate `environment.py`: an `enum.Enum` of the SDK's server environments.
/// Fern's OpenAPI importer emits a member per named server (see
/// [`ir::Environment`]).
fn environment_file(
    env: &Environment<'static>,
    pkg: &str,
    environment: &crate::ir::Environment,
) -> Result<GeneratedFile> {
    let mut body = String::new();
    if environment.urls.is_empty() {
        body.push_str(&format!(
            "import enum\n\n\nclass {}(enum.Enum):\n",
            environment.enum_name
        ));
        for (member, url) in environment.members() {
            body.push_str(&format!("    {member} = \"{}\"\n", escape_py_str(url)));
        }
    } else {
        // A multi-URL environment: each member an instance whose `base` is its
        // server and whose other fields are the operation servers' URLs.
        let name = &environment.enum_name;
        let fields: Vec<&str> = std::iter::once("base")
            .chain(environment.urls.iter().map(|(field, _)| field.as_str()))
            .collect();
        body.push_str(&format!(
            "from __future__ import annotations\n\n\nclass {name}:\n"
        ));
        for (member, _) in environment.members() {
            body.push_str(&format!("    {member}: {name}\n"));
        }
        body.push_str(&format!(
            "\n    def __init__(self, *, {}):\n",
            fields
                .iter()
                .map(|field| format!("{field}: str"))
                .collect::<Vec<_>>()
                .join(", ")
        ));
        for field in &fields {
            body.push_str(&format!("        self.{field} = {field}\n"));
        }
        for (member, url) in environment.members() {
            let arguments: Vec<String> =
                std::iter::once(format!("base=\"{}\"", escape_py_str(url)))
                    .chain(
                        environment
                            .urls
                            .iter()
                            .map(|(field, url)| format!("{field}=\"{}\"", escape_py_str(url))),
                    )
                    .collect();
            body.push_str(&format!(
                "\n\n{name}.{member} = {name}({})\n",
                arguments.join(", ")
            ));
        }
    }
    let contents = render(
        env,
        "file.py",
        "environment.py",
        context! { header => HEADER, body => body },
    )?;
    Ok(GeneratedFile {
        path: PathBuf::from(format!("src/{pkg}/environment.py")),
        contents,
    })
}

/// Generate the `errors/` package: an exception module per class and the
/// lazy-loading `__init__.py` re-exporting them. Empty when there are no errors.
fn error_files(
    env: &Environment<'static>,
    pkg: &str,
    errors: &[ErrorClass],
    tag_types: &BTreeMap<String, String>,
) -> Result<Vec<GeneratedFile>> {
    if errors.is_empty() {
        return Ok(Vec::new());
    }
    let mut files = Vec::new();
    for err in errors {
        // Register once to learn whether the body type's name collides with the
        // core `ApiError` this module subclasses (Eozilla declares a component
        // schema of that name), then render for real with the aliases that forces.
        let mut probe = Imports::at(RefLoc::Errors, tag_types);
        probe.add_plain("typing");
        probe.add_from("..core.api_error", "ApiError");
        raw_type_str(&err.body_type, &mut probe);

        let mut imports = Imports::at(RefLoc::Errors, tag_types);
        imports.aliases = probe.collision_aliases();
        imports.add_plain("typing");
        let base = imports.add_from_local("..core.api_error", "ApiError");
        // Registers the body type's `..types.<module>` import.
        let body_type = raw_type_str(&err.body_type, &mut imports);
        let module = naming::module_name(&err.class_name);
        let contents = render(
            env,
            "error.py",
            &err.class_name,
            context! {
                header => HEADER,
                imports => imports.render(),
                class_name => err.class_name,
                base => base,
                body_type => body_type,
                status_code => err.status.code(),
                wildcard => err.status.is_range(),
            },
        )?;
        files.push(GeneratedFile {
            path: PathBuf::from(format!("src/{pkg}/errors/{module}.py")),
            contents,
        });
    }
    files.push(GeneratedFile {
        path: PathBuf::from(format!("src/{pkg}/errors/__init__.py")),
        contents: lazy_loader_init(env, errors)?,
    });
    Ok(files)
}

/// Fern's package-`__init__` lazy loader for the `errors/` package: a
/// `TYPE_CHECKING` import block, a `_dynamic_imports` map, the static
/// `__getattr__`/`__dir__` machinery, and `__all__`. The error classes arrive
/// pre-sorted, so all three sections are alphabetical.
fn lazy_loader_init(env: &Environment<'static>, errors: &[ErrorClass]) -> Result<String> {
    let type_checking: String = errors
        .iter()
        .map(|e| {
            format!(
                "    from .{} import {}\n",
                naming::module_name(&e.class_name),
                e.class_name
            )
        })
        .collect();
    let pairs: Vec<(String, String)> = errors
        .iter()
        .map(|e| {
            (
                e.class_name.clone(),
                format!(".{}", naming::module_name(&e.class_name)),
            )
        })
        .collect();
    let names: Vec<String> = errors.iter().map(|e| e.class_name.clone()).collect();
    render_lazy_loader(env, &type_checking, &pairs, &names)
}

/// Assemble a lazy-loader `__init__.py` from its `TYPE_CHECKING` block, the
/// `_dynamic_imports` pairs `(exported name, ".module")`, and the `__all__`
/// names. The `_dynamic_imports` map and `__all__` are always alphabetical
/// (sorted here); only the `TYPE_CHECKING` block's order is caller-controlled.
/// The header collapses to the four leading blank lines Fern's comment header
/// strips to.
fn render_lazy_loader(
    env: &Environment<'static>,
    type_checking: &str,
    pairs: &[(String, String)],
    names: &[String],
) -> Result<String> {
    let mut pairs = pairs.to_vec();
    pairs.sort();
    let mut names = names.to_vec();
    names.sort();

    // `_dynamic_imports` and `__all__` are emitted flat; `ruff format` wraps them
    // (one entry per line with a trailing comma) when they overflow.
    let entries: Vec<String> = pairs
        .iter()
        .map(|(n, m)| format!("\"{n}\": \"{m}\""))
        .collect();
    let dynamic = format!(
        "_dynamic_imports: typing.Dict[str, str] = {{{}}}",
        entries.join(", ")
    );
    let all_entries: Vec<String> = names.iter().map(|n| format!("\"{n}\"")).collect();
    let all = format!("__all__ = [{}]", all_entries.join(", "));

    render(
        env,
        "lazy_init.py",
        "__init__.py",
        context! {
            header => HEADER,
            type_checking => type_checking,
            dynamic => dynamic,
            all => all,
        },
    )
}

/// The `types/__init__.py` lazy loader. `_dynamic_imports`, `__all__`, and the
/// `TYPE_CHECKING` block are all emitted alphabetically.
fn types_init_file(
    env: &Environment<'static>,
    pkg: &str,
    types: &[TypeDecl],
    hoisted: &[&TypeDecl],
) -> Result<GeneratedFile> {
    // Each declaration may export more than its primary name (a discriminated
    // union also exports its per-variant wrappers), all sharing the decl module.
    let mut module_of: std::collections::HashMap<String, String> = std::collections::HashMap::new();
    let mut names: Vec<String> = Vec::new();
    for decl in types.iter().chain(hoisted.iter().copied()) {
        for n in decl.exported_names() {
            names.push(n.to_string());
            module_of.insert(n.to_string(), decl.module().to_string());
        }
    }

    // TYPE_CHECKING imports are emitted alphabetically. Their order is never
    // executed, so crozier sorts straightforwardly rather than reproducing Fern's
    // traversal order: the `init-type-checking-import-order` departure.
    let mut ordered = names.clone();
    ordered.sort();

    // Consecutive names sharing a module collapse into one `from .mod import a, b`
    // line (Fern groups a discriminated union's names on a single import).
    let module = |n: &str| {
        module_of
            .get(n)
            .cloned()
            .unwrap_or_else(|| naming::module_name(n))
    };
    let mut type_checking = String::new();
    let mut i = 0;
    while i < ordered.len() {
        let m = module(&ordered[i]);
        let mut group = vec![ordered[i].clone()];
        while i + 1 < ordered.len() && module(&ordered[i + 1]) == m {
            i += 1;
            group.push(ordered[i].clone());
        }
        type_checking.push_str(&format!("    from .{m} import {}\n", group.join(", ")));
        i += 1;
    }

    let pairs: Vec<(String, String)> = names
        .iter()
        .map(|n| (n.clone(), format!(".{}", module(n))))
        .collect();
    Ok(GeneratedFile {
        path: PathBuf::from(format!("src/{pkg}/types/__init__.py")),
        contents: render_lazy_loader(env, &type_checking, &pairs, &names)?,
    })
}

/// The `{tag}/types/__init__.py` lazy loader over a tag's hoisted inline types.
/// Each type sits in its own module (`.inlined_search_response`); the block is
/// alphabetical (the `init-type-checking-import-order` departure).
fn tag_types_init_file(
    env: &Environment<'static>,
    pkg: &str,
    module: &str,
    decls: &[&TypeDecl],
) -> Result<GeneratedFile> {
    let mut named: Vec<(String, String)> = decls
        .iter()
        .flat_map(|d| {
            d.exported_names()
                .into_iter()
                .map(|n| (n.to_string(), d.module().to_string()))
        })
        .collect();
    named.sort();
    let type_checking: String = named
        .iter()
        .map(|(n, m)| format!("    from .{m} import {n}\n"))
        .collect();
    let pairs: Vec<(String, String)> = named
        .iter()
        .map(|(n, m)| (n.clone(), format!(".{m}")))
        .collect();
    let names: Vec<String> = named.iter().map(|(n, _)| n.clone()).collect();
    Ok(GeneratedFile {
        path: PathBuf::from(format!("src/{pkg}/{module}/types/__init__.py")),
        contents: render_lazy_loader(env, &type_checking, &pairs, &names)?,
    })
}

/// A client package's `__init__.py` when it holds nested sub-packages: the lazy
/// loader over those sub-packages, the way the package-root `__init__.py` lists
/// the top-level ones. `catalogs/` over `catalogs/mcp_servers/` is the shape.
fn module_pkg_init_file(
    env: &Environment<'static>,
    pkg: &str,
    module: &str,
    children: &[String],
    child_types: &[(String, Vec<String>)],
) -> Result<GeneratedFile> {
    let mut names: Vec<String> = children
        .iter()
        .map(|child| module_stem(child).to_string())
        .collect();
    let mut type_checking = from_import_block(".", &names, 4);
    let mut pairs: Vec<(String, String)> = names
        .iter()
        .map(|name| (name.clone(), format!(".{name}")))
        .collect();
    // A sub-package's hoisted types are re-exported here, one level up, and no
    // further: CloudPDF's `doc/__init__.py` lists `from .pages import
    // DocPagesSetScaleRequestMeasure…`, and the package root does not.
    for (child, exported) in child_types {
        type_checking.push_str(&from_import_block(&format!(".{child}"), exported, 4));
        for name in exported {
            pairs.push((name.clone(), format!(".{child}")));
            names.push(name.clone());
        }
    }
    Ok(GeneratedFile {
        path: PathBuf::from(format!("src/{pkg}/{module}/__init__.py")),
        contents: render_lazy_loader(env, &type_checking, &pairs, &names)?,
    })
}

/// The tag package's `__init__.py` when it owns hoisted types: a lazy loader
/// re-exporting them from the tag's `.types` subpackage. A tag with no hoisted
/// types keeps the plain comment-only marker instead.
fn tag_pkg_init_file(
    env: &Environment<'static>,
    pkg: &str,
    module: &str,
    decls: &[&TypeDecl],
    children: &[String],
    child_types: &[(String, Vec<String>)],
) -> Result<GeneratedFile> {
    let mut names: Vec<String> = decls
        .iter()
        .flat_map(|d| d.exported_names().into_iter().map(str::to_string))
        .collect();
    names.sort();
    let mut type_checking = from_import_block(".types", &names, 4);
    let mut pairs: Vec<(String, String)> = names
        .iter()
        .map(|n| (n.clone(), ".types".to_string()))
        .collect();
    // A group with a child group exports the child too, after its own types
    // (Fern's `yard/__init__.py` lists `from . import cranes` beside the
    // `.types` it hoisted), and re-exports the child's hoisted types.
    let child_names: Vec<String> = children
        .iter()
        .map(|child| module_stem(child).to_string())
        .collect();
    type_checking.push_str(&from_import_block(".", &child_names, 4));
    for name in &child_names {
        pairs.push((name.clone(), format!(".{name}")));
        names.push(name.clone());
    }
    for (child, exported) in child_types {
        type_checking.push_str(&from_import_block(&format!(".{child}"), exported, 4));
        for name in exported {
            pairs.push((name.clone(), format!(".{child}")));
            names.push(name.clone());
        }
    }
    Ok(GeneratedFile {
        path: PathBuf::from(format!("src/{pkg}/{module}/__init__.py")),
        contents: render_lazy_loader(env, &type_checking, &pairs, &names)?,
    })
}

/// The package-root `__init__.py` lazy loader, re-exporting the type layer, the
/// errors, the endpoint submodules, the root client, and `__version__`. Every
/// section is alphabetical; the `TYPE_CHECKING` block groups the imports by
/// source (`.types`, `.errors`, submodules, `.client`, `.version`).
fn root_init_file(
    env: &Environment<'static>,
    pkg: &str,
    ir: &Ir,
    modules: &[&String],
) -> Result<GeneratedFile> {
    let client_name = &ir.client_name;
    let async_name = format!("Async{client_name}");

    let mut type_names: Vec<String> = ir
        .types
        .iter()
        .flat_map(|t| t.exported_names())
        .map(str::to_string)
        .collect();
    type_names.sort();
    // Hoisted tag types are re-exported at the package root through their tag
    // submodule (`from .inlined import InlinedSearchResponse`), grouped per tag.
    let mut hoisted_by_tag: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for tt in &ir.tag_types {
        let names = tt.decl.exported_names().into_iter().map(str::to_string);
        if tt.module.is_empty() {
            type_names.extend(names);
        } else if tt.module.contains('/') {
            // A nested sub-package's types are re-exported by its parent
            // package instead (see `module_pkg_init_file`).
        } else {
            hoisted_by_tag
                .entry(tt.module.clone())
                .or_default()
                .extend(names);
        }
    }
    type_names.sort();
    let environment = ir.environment.as_ref();
    let mut error_names: Vec<String> = ir.errors.iter().map(|e| e.class_name.clone()).collect();
    error_names.sort();
    let mut module_names: Vec<String> = modules.iter().map(|m| (*m).clone()).collect();
    module_names.sort();

    // Grouped TYPE_CHECKING block (each group alphabetical, ruff-wrapped at 120).
    let mut tc = String::new();
    tc.push_str(&from_import_block(".types", &type_names, 4));
    tc.push_str(&from_import_block(".errors", &error_names, 4));
    tc.push_str(&from_import_block(".", &module_names, 4));
    tc.push_str(&from_import_block(
        "._default_clients",
        &[
            "DefaultAioHttpClient".to_string(),
            "DefaultAsyncHttpxClient".to_string(),
        ],
        4,
    ));
    tc.push_str(&from_import_block(
        ".client",
        &[async_name.clone(), client_name.to_string()],
        4,
    ));
    if let Some(e) = environment {
        tc.push_str(&from_import_block(
            ".environment",
            std::slice::from_ref(&e.enum_name),
            4,
        ));
    }
    for (tag, names) in &hoisted_by_tag {
        tc.push_str(&from_import_block(
            &format!(".{}", module_path(tag)),
            names,
            4,
        ));
    }
    // `__version__` reads the installed distribution's metadata, so only the
    // packaged form exports it; Fern's flat tree has no `version.py`.
    let packaged = ir.layout == Layout::Packaged;
    if packaged {
        tc.push_str(&from_import_block(
            ".version",
            &["__version__".to_string()],
            4,
        ));
    }

    // `_dynamic_imports` pairs: every symbol → its source module.
    let mut pairs: Vec<(String, String)> = Vec::new();
    for n in &type_names {
        pairs.push((n.clone(), ".types".to_string()));
    }
    for n in &error_names {
        pairs.push((n.clone(), ".errors".to_string()));
    }
    for m in &module_names {
        pairs.push((m.clone(), format!(".{m}")));
    }
    pairs.push((
        "DefaultAioHttpClient".to_string(),
        "._default_clients".to_string(),
    ));
    pairs.push((
        "DefaultAsyncHttpxClient".to_string(),
        "._default_clients".to_string(),
    ));
    pairs.push((client_name.to_string(), ".client".to_string()));
    pairs.push((async_name, ".client".to_string()));
    if let Some(e) = environment {
        pairs.push((e.enum_name.clone(), ".environment".to_string()));
    }
    for (tag, names) in &hoisted_by_tag {
        for n in names {
            pairs.push((n.clone(), format!(".{}", module_path(tag))));
        }
    }
    if packaged {
        pairs.push(("__version__".to_string(), ".version".to_string()));
    }

    let names: Vec<String> = pairs.iter().map(|(n, _)| n.clone()).collect();
    Ok(GeneratedFile {
        path: PathBuf::from(format!("src/{pkg}/__init__.py")),
        contents: render_lazy_loader(env, &tc, &pairs, &names)?,
    })
}

/// A lifted base-path parameter's constructor argument line: a required `str`,
/// or an `Optional[str]` defaulting to its declared default on the client and
/// to `None` in the wrapper beneath it.
fn client_path_parameter_param(parameter: &ClientPathParameter, on_client: bool) -> String {
    match &parameter.default {
        None => format!("        {}: str,\n", parameter.py_name),
        Some(value) if on_client => format!(
            "        {}: typing.Optional[str] = \"{}\",\n",
            parameter.py_name,
            escape_py_str(value)
        ),
        Some(_) => format!(
            "        {}: typing.Optional[str] = None,\n",
            parameter.py_name
        ),
    }
}

/// A lifted base-path parameter's example argument: its default by keyword, or
/// the placeholder a required constructor argument takes, as fern-python-sdk
/// 5.41.3's docstrings write it (`FernApi(edition="v2")`).
fn client_path_parameter_example(parameter: &ClientPathParameter) -> String {
    match &parameter.default {
        Some(value) => format!("{}=\"{}\"", parameter.py_name, escape_py_str(value)),
        None => format!(
            "{}=\"YOUR_{}\"",
            parameter.py_name,
            parameter.py_name.to_uppercase()
        ),
    }
}

/// A promoted header's constructor annotation, before any `Optional[...]`: its
/// scalar or list type, or the one-value `typing.Literal` a subset-promoted
/// string header with a `default` takes.
fn global_header_annotation(header: &crate::ir::GlobalHeader) -> String {
    match header.literal() {
        Some(value) => format!("typing.Literal[\"{}\"]", escape_py_str(value)),
        None => header.py_type().python().to_string(),
    }
}

/// A constructor example matching the header's type. Date fields use Fern's
/// measured date-example form instead of an invalid string placeholder.
fn global_header_example(header: &GlobalHeader) -> String {
    if header.py_type() == HeaderType::Date {
        format!(
            "{}=datetime.date.fromisoformat(\"2023-01-15\")",
            header.py_name
        )
    } else {
        format!(
            "{}=\"YOUR_{}\"",
            header.py_name,
            header.py_name.to_uppercase()
        )
    }
}

/// Render an abbreviated call `<prefix>(...)` for the README snippets: empty parens
/// when the body is not complex, else a literal `...` placeholder. Fern does
/// not wrap these advanced README calls,
/// even when a descriptive operation name makes the line unusually long.
fn abbrev_call(indent: usize, prefix: &str, complex: bool) -> String {
    let pad = " ".repeat(indent);
    if !complex {
        return format!("{pad}{prefix}()");
    }
    format!("{pad}{prefix}(...)")
}

/// Whether an endpoint can anchor the README's representative calls. Fern skips
/// referenced file-form declarations here even though their methods may still have
/// a deliberately argument-free worked example in `reference.md`.
fn readme_endpoint_eligible(ep: &Endpoint, types: &[TypeDecl], tag_decls: &[TagTypeDecl]) -> bool {
    endpoint_has_worked_example(ep)
        && !root_binary_download_undocumented(ep)
        && path_object_documented(ep, types, tag_decls)
        && !matches!(ep.request_body, Some(RequestBody::Bytes { .. }))
        && !matches!(&ep.request_body, Some(RequestBody::Form(form))
            if ep.body_schema_shape == BodySchemaShape::Ref
                && form.fields.iter().any(|field| field.is_file)
                && !form.fields.iter().any(|field| field.form_content_type.is_some()))
}

/// The endpoint Fern's README and `reference.md` anchor their worked example on:
/// the first eligible `POST`, else the first eligible operation, each tier read
/// in the README's own module order and over the top-level clients before any
/// nested one. Neither a request body nor an argument-free call promotes a
/// non-`POST` operation ahead of that order — corpus row 123 anchors its README
/// on the document's first `GET` even though a later `PUT` carries a flattened
/// JSON body, and an argument-free first `GET` wins over a later argument-free
/// `DELETE`. CloudPDF's first `POST` is `doc.annotations.create`, two levels
/// down, and its README demonstrates `shares.exchange`, the first `POST` of a
/// top-level client; a nested `POST` still wins over a top-level `GET`.
fn select_readme_endpoint<'a>(
    endpoints: impl Clone + Iterator<Item = &'a Endpoint>,
    types: &[TypeDecl],
    tag_decls: &[TagTypeDecl],
) -> Option<&'a Endpoint> {
    let tier = |accept: &dyn Fn(&Endpoint) -> bool| {
        endpoints
            .clone()
            .filter(|e| module_depth(&e.module) <= 1)
            .find(|e| accept(e))
            .or_else(|| endpoints.clone().find(|e| accept(e)))
    };
    tier(&|e| {
        e.emittable && e.http_method == "POST" && readme_endpoint_eligible(e, types, tag_decls)
    })
    .or_else(|| tier(&|e| e.emittable && readme_endpoint_eligible(e, types, tag_decls)))
}

/// The import block and the call block of an endpoint's worked documentation
/// example — the lines before and after the client constructor. The README's
/// streaming section writes its own constructor, but imports what the call
/// constructs (`from fern import FernApi, Sector`).
fn readme_example_parts(ir: &Ir, ep: &Endpoint, pkg: &str) -> Option<(String, String)> {
    let mut ctx = ExampleCtx {
        types: &ir.types,
        enum_type: ir.enum_type,
        yaml_unquoted_timestamps: ir.yaml_unquoted_timestamps.as_ref(),
        tag_decls: &ir.tag_types,
        referenced: BTreeSet::new(),
        referenced_doc_order: Vec::new(),
        referenced_tag: BTreeSet::new(),
        referenced_tag_doc_order: Vec::new(),
        uses_datetime: false,
        datetime_precedes_tag_import: false,
        auth: &ir.auth,
        has_environment: ir.environment.is_some(),
        global_headers: &ir.global_headers,
        client_path_parameters: &ir.client_path_parameters,
        building: Default::default(),
        expanding_aliases: Vec::new(),
        documentation: false,
        reference: false,
        field_examples_ignored: false,
        untyped_arguments: BTreeSet::new(),
        sdk_first_party: true,
    };
    let lines = build_documentation_example(
        ep,
        false,
        &ep.module,
        pkg,
        &ir.client_name,
        &mut ctx,
        None,
        false,
    )?;
    let start = lines.iter().position(|line| line.starts_with("client."))?;
    let constructor = lines
        .iter()
        .position(|line| line.starts_with("client = "))?;
    let imports = lines[..constructor].join("\n");
    Some((imports.trim_end().to_string(), lines[start..].join("\n")))
}

/// The client modules in the order the generated package nests them: each
/// package before its own children, siblings in the order
/// [`module_children`] yields. A flat SDK's order is unchanged by this; a nested
/// one (`catalogs/mcp_servers`) is walked through its parent rather than in the
/// document order its paths happened to appear in.
fn client_tree_modules(ir: &Ir) -> Vec<&str> {
    let children = module_children(&ir.endpoint_modules);
    let mut out: Vec<&str> = Vec::new();
    let mut stack: Vec<String> = children
        .get("")
        .map(|names| names.iter().rev().cloned().collect())
        .unwrap_or_default();
    while let Some(module) = stack.pop() {
        if let Some(name) = ir
            .endpoint_modules
            .iter()
            .find(|known| **known == module)
            .map(String::as_str)
        {
            out.push(name);
        }
        if let Some(names) = children.get(&module) {
            for child in names.iter().rev() {
                stack.push(child.clone());
            }
        }
    }
    out
}

/// The client modules in the order `reference.md` lists them. A package's own
/// endpoints come at its place among its siblings, but its children are listed
/// where a package that owns no endpoints would be — after every sibling that
/// owns endpoints, among the pure parents sorted by name. TrueForge's `internal`
/// owns endpoints and nests four sub-clients, and its golden lists `Internal`
/// first, then `Agents` … `Skills`, then the `Catalogs`, `Internal` and
/// `Settings` sub-clients in that order.
fn reference_modules(ir: &Ir) -> Vec<&str> {
    fn visit<'a>(
        parent: &str,
        children: &BTreeMap<String, Vec<String>>,
        ir: &'a Ir,
        out: &mut Vec<&'a str>,
    ) {
        let Some(names) = children.get(parent) else {
            return;
        };
        out.extend(names.iter().filter_map(|name| {
            ir.endpoint_modules
                .iter()
                .find(|known| *known == name)
                .map(String::as_str)
        }));
        let mut folders: Vec<&String> = names
            .iter()
            .filter(|name| children.contains_key(*name))
            .collect();
        folders.sort();
        for folder in folders {
            visit(folder, children, ir, out);
        }
    }
    let children = module_children(&ir.endpoint_modules);
    let mut out = Vec::new();
    visit("", &children, ir, &mut out);
    out
}

/// The order the README walks endpoints in. Fern builds client modules in
/// first-seen module order and reads the README's endpoints from that grouped
/// view. Keep operations within each module in source order, but do not let an
/// operation from a later module leapfrog one from the first module merely
/// because their paths were interleaved. The root client's own operations come
/// after every sub-client's: OpenCodeUI's `Session`-tagged pair offers no `POST`,
/// and its README anchors on the root's first one, `global_dispose`.
fn readme_endpoint_order(ir: &Ir) -> Vec<&Endpoint> {
    let grouped: Vec<&Endpoint> = client_tree_modules(ir)
        .into_iter()
        .chain(std::iter::once(""))
        .flat_map(|module| {
            ir.endpoints
                .iter()
                .filter(move |ep| ep.module.as_str() == module)
        })
        .collect();
    if grouped.is_empty() {
        ir.endpoints
            .iter()
            .filter(|e| e.module.is_empty())
            .collect()
    } else {
        grouped
    }
}

fn readme_endpoint(ir: &Ir) -> Option<&Endpoint> {
    let order = readme_endpoint_order(ir);
    select_readme_endpoint(order.into_iter(), &ir.types, &ir.tag_types)
}

/// The endpoint the README's streaming section demonstrates: the first emittable
/// streaming operation anywhere in the SDK. Fern documents streaming whenever the
/// SDK has it, so this is independent of the endpoint the usage example shows.
fn readme_streaming_endpoint(ir: &Ir) -> Option<&Endpoint> {
    readme_endpoint_order(ir)
        .into_iter()
        .find(|e| e.emittable && e.streaming)
}

fn client_call_prefix(ep: &Endpoint) -> String {
    let method = ep.method_name.trim_end_matches('_');
    if ep.module.is_empty() {
        format!("client.{method}")
    } else {
        format!("client.{}.{method}", module_path(&ep.module))
    }
}

fn raw_client_call_prefix(ep: &Endpoint) -> String {
    let method = ep.method_name.trim_end_matches('_');
    if ep.module.is_empty() {
        format!("response = client.with_raw_response.{method}")
    } else {
        format!(
            "response = client.{}.with_raw_response.{method}",
            module_path(&ep.module)
        )
    }
}

/// The generated `README.md`: mostly static prose with the SDK name/package
/// substituted and a worked usage example (sync + async) synthesized from the
/// first endpoint. Compared verbatim (README is not comment-stripped). Emitted
/// only when there is an endpoint to demonstrate; the caller writes the empty file
/// Fern leaves behind when there is not (`cyberark-conjur-api`).
fn readme_file(ir: &Ir) -> Option<GeneratedFile> {
    // Fern demonstrates the first emittable endpoint that has a request body (the
    // most illustrative call). APIs with no body-bearing operations use the first
    // no-argument non-GET operation before falling back to the first endpoint.
    let first = readme_endpoint(ir)?;
    let pkg = &ir.package_name;
    let org = naming::to_pascal_case(pkg);
    let async_name = format!("Async{}", ir.client_name);

    // The abbreviated calls in the error-handling and advanced sections show `...`
    // whenever the endpoint has a path or query argument or a request body; an
    // argument-free endpoint (and the `_` placeholder) shows empty parens. The
    // error/raw-response calls are ruff-wrapped at the snippet width 88. A golden
    // refresh that reintroduces body-shape-sensitive placeholders is a Fern behavior
    // change, not a regression here.
    // A header is no argument here: measured at Fern 5.20.0 (not re-measured at
    // the certified pair), Fern writes `client.probe()` for an endpoint whose only
    // parameter is a header, scalar or array, required or not.
    let has_arguments = !first.path_params.is_empty()
        || !first.query_params.is_empty()
        || first.request_body.is_some();
    let complex = first.method_name != "_" && has_arguments;
    let err_call = abbrev_call(4, &client_call_prefix(first), complex);
    // A paginated endpoint's raw-response snippet walks its pager, as Fern's
    // does; any other reads the raw client's response.
    let raw_block = if first.pagination.is_some() {
        format!(
            "client = @@CLIENT@@(\n    ...,\n)\npager = {}\nprint(pager.response)  # access the typed response for the first page\nfor item in pager:\n    print(item)  # access the underlying object(s)\nfor page in pager.iter_pages():\n    print(page.response)  # access the typed response for each page\n    for item in page:\n        print(item)  # access the underlying object(s)\n",
            abbrev_call(0, &client_call_prefix(first), complex)
        )
    } else {
        format!(
            "client = @@CLIENT@@(...)\n{}\nprint(response.headers)  # access the response headers\nprint(response.status_code)  # access the response status code\nprint(response.data)  # access the underlying object\n",
            abbrev_call(0, &raw_client_call_prefix(first), complex)
        )
    };
    let retry_prefix = client_call_prefix(first);
    let retry_call = format!(
        "{retry_prefix}({}request_options={{",
        if complex { "..., " } else { "" }
    );

    let sync_example = {
        let mut ctx = ExampleCtx {
            types: &ir.types,
            enum_type: ir.enum_type,
            yaml_unquoted_timestamps: ir.yaml_unquoted_timestamps.as_ref(),
            tag_decls: &ir.tag_types,
            referenced: BTreeSet::new(),
            referenced_doc_order: Vec::new(),
            referenced_tag: BTreeSet::new(),
            referenced_tag_doc_order: Vec::new(),
            uses_datetime: false,
            datetime_precedes_tag_import: false,
            auth: &ir.auth,
            has_environment: ir.environment.is_some(),
            global_headers: &ir.global_headers,
            client_path_parameters: &ir.client_path_parameters,
            building: Default::default(),
            expanding_aliases: Vec::new(),
            documentation: false,
            reference: false,
            field_examples_ignored: false,
            untyped_arguments: BTreeSet::new(),
            sdk_first_party: true,
        };
        build_documentation_example(
            first,
            false,
            &first.module,
            pkg,
            &ir.client_name,
            &mut ctx,
            None,
            false,
        )?
        .join("\n")
    };
    let async_example = {
        let mut ctx = ExampleCtx {
            types: &ir.types,
            enum_type: ir.enum_type,
            yaml_unquoted_timestamps: ir.yaml_unquoted_timestamps.as_ref(),
            tag_decls: &ir.tag_types,
            referenced: BTreeSet::new(),
            referenced_doc_order: Vec::new(),
            referenced_tag: BTreeSet::new(),
            referenced_tag_doc_order: Vec::new(),
            uses_datetime: false,
            datetime_precedes_tag_import: false,
            auth: &ir.auth,
            has_environment: ir.environment.is_some(),
            global_headers: &ir.global_headers,
            client_path_parameters: &ir.client_path_parameters,
            building: Default::default(),
            expanding_aliases: Vec::new(),
            documentation: false,
            reference: false,
            field_examples_ignored: false,
            untyped_arguments: BTreeSet::new(),
            sdk_first_party: true,
        };
        build_documentation_example(
            first,
            true,
            &first.module,
            pkg,
            &ir.client_name,
            &mut ctx,
            None,
            false,
        )?
        .join("\n")
    };

    let mut streaming_args =
        documentation_client_example_args(&ir.auth, &ir.global_headers, &ir.client_path_parameters)
            .into_iter()
            .map(|arg| format!("    {arg},\n"))
            .collect::<String>();
    if ir.environment.is_none() {
        streaming_args.push_str("    base_url=\"https://yourhost.com/path/to/api\",\n");
    }
    // A client taking no argument is constructed on one line: `dot-ai` declares
    // a server and no auth, and its streaming section reads `client = FernApi()`.
    let client_call = if streaming_args.is_empty() {
        format!("{}()", ir.client_name)
    } else {
        format!("{}(\n{streaming_args})", ir.client_name)
    };
    let stream_ep = readme_streaming_endpoint(ir);
    let streaming = stream_ep.map_or_else(String::new, |ep| {
        // The call carries its worked arguments here, exactly as the usage example
        // does; an argument-free streaming endpoint renders the same empty parens
        // it always did.
        let (imports, call) = readme_example_parts(ir, ep, pkg).unwrap_or_else(|| {
            (
                format!("from {pkg} import {}", ir.client_name),
                format!("{}()", client_call_prefix(ep)),
            )
        });
        format!(
            "## Streaming\n\nThe SDK supports streaming responses, as well, the response will be a generator that you can loop over.\n\n```python\n{imports}\n\nclient = {client_call}\n\n{call}\n```\n\n",
        )
    });
    // The pager section, on the first paginated endpoint in client order. Fern
    // shows the plain call and then the page-by-page form.
    let pager_ep = readme_endpoint_order(ir)
        .into_iter()
        .find(|ep| ep.emittable && ep.pagination.is_some());
    let pagination = pager_ep.map_or_else(String::new, |ep| {
        let prefix = client_call_prefix(ep);
        format!(
            "## Pagination\n\nPaginated requests will return a `SyncPager` or `AsyncPager`, which can be used as generators for the underlying object.\n\n```python\nfrom {pkg} import {}\n\nclient = {client_call}\n\n{prefix}()\n```\n\n```python\n# You can also iterate through pages and access the typed response per page\npager = {prefix}(...)\nfor page in pager.iter_pages():\n    print(page.response)  # access the typed response for each page\n    for item in page:\n        print(item)\n```\n\n",
            ir.client_name,
        )
    });
    let contents = include_str!("../assets/scaffolding/README.md.tmpl")
        .replace(
            "@@STREAMING_TOC@@",
            if stream_ep.is_some() {
                "- [Streaming](#streaming)\n"
            } else {
                ""
            },
        )
        .replace("@@STREAMING@@", &streaming)
        .replace(
            "@@PAGINATION_TOC@@\n",
            if pager_ep.is_some() {
                "- [Pagination](#pagination)\n"
            } else {
                ""
            },
        )
        .replace("@@PAGINATION@@\n", &pagination)
        .replace(
            "@@ENVIRONMENTS_TOC@@\n",
            if ir.environment.is_some() {
                "- [Environments](#environments)\n"
            } else {
                ""
            },
        )
        .replace(
            "@@ENVIRONMENTS@@\n\n",
            &if ir.environment.is_some() {
                // A lifted base-path parameter leads the constructor here too,
                // as in every snippet (the `lifted-base-path-docs-examples`
                // departure): without a default, Fern's call misses it.
                let lifted: String = ir
                    .client_path_parameters
                    .iter()
                    .map(|parameter| format!("    {},\n", client_path_parameter_example(parameter)))
                    .collect();
                format!(
                    "## Environments\n\nThis SDK allows you to configure different environments for API requests.\n\n```python\nfrom @@PKG@@ import @@CLIENT@@\nfrom @@PKG@@.environment import @@CLIENT@@Environment\n\nclient = @@CLIENT@@(\n{lifted}    environment=@@ENV_DEFAULT@@,\n)\n```\n\n"
                )
            } else {
                String::new()
            },
        )
        // Fern's shield credits the organization. Only the flat layout follows
        // it: the packaged README keeps the `Fern` it has always carried, so no
        // packaged output changes with the layout setting.
        .replace(
            "@@SHIELD_ORG@@",
            match ir.layout {
                Layout::Packaged => "Fern",
                Layout::Flat => &org,
            },
        )
        .replace("@@ORG@@", &org)
        .replace("@@PROJECT@@", &ir.project_name)
        .replace("@@PKG@@", pkg)
        .replace("@@RAW_BLOCK@@", &raw_block)
        .replace("@@CLIENT@@", &ir.client_name)
        .replace("@@ASYNC@@", &async_name)
        .replace(
            "@@ENV_DEFAULT@@",
            &ir.environment.as_ref().map_or_else(
                || format!("{}Environment.DEFAULT", ir.client_name),
                crate::ir::Environment::default_ref,
            ),
        )
        .replace("@@ERR_CALL@@", &err_call)
        .replace("@@RETRY_CALL@@", &retry_call)
        .replace("@@USAGE@@", &sync_example)
        .replace("@@ASYNC_EXAMPLE@@", &async_example);
    Some(GeneratedFile {
        path: PathBuf::from("README.md"),
        contents,
    })
}

/// The generated `reference.md`: a per-endpoint reference grouped by tag, each
/// entry a collapsible `<details>` with an optional description, a worked sync
/// usage example, and a parameter table. Endpoints without an example (a raw
/// bytes body) are omitted, exactly as Fern does; an API with no entry at all still
/// gets the bare `# Reference` heading Fern writes (`cyberark-conjur-api`).
/// Compared verbatim.
fn reference_file(
    env: &Environment<'static>,
    ir: &Ir,
    modules: &[&str],
    tag_types: &BTreeMap<String, String>,
) -> Result<GeneratedFile> {
    let pkg = &ir.package_name;
    let mut blocks: Vec<String> = vec!["# Reference".to_string()];

    let root_eps: Vec<&Endpoint> = ir
        .endpoints
        .iter()
        .filter(|e| e.module.is_empty() && e.emittable)
        .collect();
    for ep in root_eps {
        blocks.push(reference_entry(env, ir, ep, &ep.module, pkg, tag_types)?);
        blocks.push(String::new());
    }

    for module in modules {
        let eps: Vec<&Endpoint> = ir
            .endpoints
            .iter()
            .filter(|e| e.module.as_str() == *module && e.emittable)
            .collect();
        let title = ir
            .endpoint_module_titles
            .get(*module)
            .cloned()
            .unwrap_or_else(|| naming::to_pascal_case(module));
        blocks.push(format!("## {title}"));
        for ep in eps {
            blocks.push(reference_entry(env, ir, ep, module, pkg, tag_types)?);
            blocks.push(String::new());
        }
    }
    // The last entry's trailing blank line is kept: Fern ends the file with one.
    Ok(GeneratedFile {
        path: PathBuf::from("reference.md"),
        contents: format!("{}\n", blocks.join("\n")),
    })
}

/// One parameter row in the `reference.md` table: name, annotation, and the
/// suffix (a lone space, or ` — <description>`).
#[derive(Serialize)]
struct ParamRow {
    name: String,
    annot: String,
    suffix: String,
}

pub(crate) fn reference_param_suffix(description: Option<&str>) -> String {
    match description {
        // Importers sometimes retain leading blank lines around a one-line
        // description. Fern still renders those through the ordinary em-dash
        // form; only multiple substantive lines become a separate paragraph.
        Some(d) if d.trim().contains('\n') => {
            format!(" \n\n{}", d.trim_matches(['\r', '\n']))
        }
        Some(d) => format!(" — {d}"),
        None => " ".to_string(),
    }
}

/// The view model for one `reference.md` endpoint entry (`reference_entry.md.j2`).
#[derive(Serialize)]
struct ReferenceEntryView {
    client_prefix: String,
    href: String,
    pkg: String,
    method: String,
    dots: &'static str,
    return_type: String,
    description: Option<String>,
    example: String,
    example_gap: &'static str,
    params: Vec<ParamRow>,
}

/// Render one `reference.md` endpoint entry (a `<details>` block), returning it
/// without a trailing newline so [`reference_file`] can join entries uniformly.
fn reference_entry(
    env: &Environment<'static>,
    ir: &Ir,
    ep: &Endpoint,
    module: &str,
    pkg: &str,
    tag_types: &BTreeMap<String, String>,
) -> Result<String> {
    let mut imports = Imports::at(RefLoc::Client(module.to_string()), tag_types);
    let mp = method_params(ep, &mut imports);
    let has_args = !mp.path.is_empty()
        || !mp.query.is_empty()
        || !mp.header.is_empty()
        || !mp.body.is_empty()
        || !ep.constant_headers.is_empty();

    // The example (sync form). Bytes bodies are filtered out before this point.
    let mut ctx = ExampleCtx {
        types: &ir.types,
        enum_type: ir.enum_type,
        yaml_unquoted_timestamps: ir.yaml_unquoted_timestamps.as_ref(),
        tag_decls: &ir.tag_types,
        referenced: BTreeSet::new(),
        referenced_doc_order: Vec::new(),
        referenced_tag: BTreeSet::new(),
        referenced_tag_doc_order: Vec::new(),
        uses_datetime: false,
        datetime_precedes_tag_import: false,
        auth: &ir.auth,
        has_environment: ir.environment.is_some(),
        global_headers: &ir.global_headers,
        client_path_parameters: &ir.client_path_parameters,
        building: Default::default(),
        expanding_aliases: Vec::new(),
        documentation: false,
        reference: false,
        field_examples_ignored: false,
        untyped_arguments: BTreeSet::new(),
        sdk_first_party: true,
    };
    let example = (!ep.binary_schema_response || !module.is_empty() || ep.openapi_31)
        .then(|| {
            build_documentation_example(
                ep,
                false,
                module,
                pkg,
                &ir.client_name,
                &mut ctx,
                ir.environment.as_ref(),
                true,
            )
        })
        .flatten()
        .map_or_else(
            || {
                if has_args {
                    format!("{}(...)", client_call_prefix(ep))
                } else {
                    format!("{}()", client_call_prefix(ep))
                }
            },
            |lines| lines.join("\n"),
        );

    // The parameter rows, in signature order, then `request_options`.
    let mut params: Vec<ParamRow> = Vec::new();
    for (name, ty, desc) in &mp.path {
        // A *declared* description — even the empty string — gets the ` — {desc}`
        // separator (Fern emits the em-dash whenever the parameter carries a
        // `description` key); only an omitted (`None`) one gets the bare space.
        let suffix = reference_param_suffix(desc.as_deref());
        params.push(ParamRow {
            name: name.trim_end_matches('_').to_string(),
            annot: reference_param_annotation(&ty.replace("typing.Sequence[", "typing.List[")),
            suffix,
        });
    }
    let reference_body = if !ep.body_schema_dropped {
        ep.request_body
            .as_ref()
            .and(ep.reference_body_type.as_ref())
            .map_or_else(
                || mp.body,
                |request| {
                    // An optional `json=request` body is documented optional:
                    // marimo-plugins' `type: "null"` bodies read
                    // `typing.Optional[MarimoChatbotCancelPromptOutput]`.
                    let request = match &ep.request_body {
                        Some(RequestBody::Single(single)) if !single.required => {
                            TypeRef::Optional(Box::new(request.clone()))
                        }
                        _ => request.clone(),
                    };
                    vec![DocParam {
                        name: "request".to_string(),
                        annotation: raw_type_str_ctx(&request, &mut imports, true),
                        default: None,
                        description: None,
                    }]
                },
            )
    } else {
        mp.body
    };
    // Fern's reference writer does not document an idempotency argument.
    let mut reference_body: Vec<DocParam> = reference_body
        .into_iter()
        .filter(|param| {
            !ep.extensions
                .idempotency_headers
                .iter()
                .any(|header| header.py_name == param.name)
        })
        .collect();
    if let Some(RequestBody::Inline(fields)) = &ep.request_body {
        reference_body.sort_by_key(|param| {
            fields
                .iter()
                .find(|field| field.py_name == param.name)
                .map_or(usize::MAX, |field| field.reference_order)
        });
        // The field a stream condition fixes is not a method argument, but the
        // reference documents it — as a bare `typing.Literal`, after the required
        // fields and ahead of the optional ones: it takes no default, so the
        // required-first ordering below places it last of the required.
        if let Some((wire, _)) = ep.stream_condition.as_ref() {
            if let Some(field) = fields.iter().find(|field| field.wire_name == *wire) {
                reference_body.push(DocParam {
                    name: field.py_name.clone(),
                    annotation: "typing.Literal".to_string(),
                    default: None,
                    description: field.docstring.clone(),
                });
            }
        }
    }
    // Nor does it document a constant header, which Fern's does (the
    // `constant-header-docs-arguments` departure).
    let reference_header = mp.header;
    for dp in ordered_keyword_params(&mp.query, &reference_header, &reference_body) {
        let is_body = reference_body.iter().any(|body| body.name == dp.name);
        let query_is_list = ep
            .query_params
            .iter()
            .find(|query| query.py_name == dp.name)
            .is_some_and(|query| !query.allow_multiple);
        // A header's signature takes `typing.Sequence`; the reference documents it
        // as `typing.List`, as it does a body.
        let is_header = ep
            .header_params
            .iter()
            .any(|header| header.py_name == dp.name);
        // An inline wildcard (`*/*`) binary body is documented as the `bytes` its
        // signature takes: apideck-file-storage's `files.upload` and
        // `upload_sessions.upload` (measured at fern-python-sdk 5.41.3; 5.20.0's
        // reference writer wrote the OpenAPI source type `str`).
        let annotation = if is_body || query_is_list || is_header {
            reference_list_annotation(&dp.annotation)
        } else {
            dp.annotation.clone()
        };
        let description = if is_body {
            match &ep.request_body {
                Some(RequestBody::Single(_)) => ep.request_body_schema_doc.as_deref(),
                Some(RequestBody::Inline(_))
                    if !ep.body_schema_dropped && ep.reference_body_type.is_some() =>
                {
                    ep.request_body_schema_doc
                        .as_deref()
                        .or(dp.description.as_deref())
                }
                Some(RequestBody::Bytes { .. }) => ep.request_body_doc.as_deref(),
                Some(RequestBody::Form(form)) => form
                    .fields
                    .iter()
                    .find(|field| field.py_name == dp.name)
                    .and_then(|field| field.docstring.as_deref()),
                _ => dp.description.as_deref(),
            }
        } else {
            dp.description.as_deref()
        };
        let reference_name = if is_body {
            ep.request_body
                .as_ref()
                .and_then(|body| match body {
                    RequestBody::Inline(fields) => fields
                        .iter()
                        .find(|field| field.py_name == dp.name)
                        .filter(|field| {
                            field
                                .wire_name
                                .starts_with(|character: char| character.is_ascii_digit())
                        })
                        .map(|field| {
                            naming::model_field_name(&field.wire_name)
                                .trim_start_matches("f_")
                                .to_string()
                        }),
                    // A form property whose wire name ends in `[]` is documented
                    // with the bracket spelled out — DaniWeb's `emails[]` is
                    // `emails_array` in `reference.md`, while the Python argument
                    // the same property names stays `emails`.
                    RequestBody::Form(form) => form
                        .fields
                        .iter()
                        .find(|field| field.py_name == dp.name)
                        .filter(|field| field.wire_name.ends_with("[]"))
                        .map(|field| {
                            naming::field_name(&format!(
                                "{}_array",
                                field.wire_name.trim_end_matches("[]")
                            ))
                        }),
                    _ => None,
                })
                .unwrap_or_else(|| dp.name.trim_end_matches('_').to_string())
        } else {
            // A query parameter's `[]` is spelled out the same way: eNanoMapper's
            // `property_uris[]` is `property_uris_array` in `reference.md`.
            ep.query_params
                .iter()
                .find(|query| query.py_name == dp.name)
                .filter(|query| query.wire_name.ends_with("[]"))
                .map(|query| {
                    naming::field_name(&format!("{}_array", query.wire_name.trim_end_matches("[]")))
                })
                .unwrap_or_else(|| dp.name.trim_end_matches('_').to_string())
        };
        params.push(ParamRow {
            name: reference_name,
            annot: reference_param_annotation(&annotation),
            suffix: reference_param_suffix(description),
        });
    }
    params.push(ParamRow {
        name: "request_options".to_string(),
        annot: "typing.Optional[RequestOptions]".to_string(),
        suffix: " — Request-specific configuration.".to_string(),
    });

    let view = ReferenceEntryView {
        client_prefix: if module.is_empty() {
            "client.".to_string()
        } else {
            format!("client.{}.", module_path(module))
        },
        href: if module.is_empty() {
            format!("src/{pkg}/client.py")
        } else {
            format!("src/{pkg}/{module}/client.py")
        },
        pkg: pkg.to_string(),
        method: ep.method_name.trim_end_matches('_').to_string(),
        // A documented parameter the method does not take — a propertyless model
        // documented as `request` — still earns the dots.
        dots: if has_args || params.len() > 1 {
            "..."
        } else {
            ""
        },
        return_type: reference_return_type(
            ep,
            &mp.inner,
            &sse_chunk(
                ep,
                &mut Imports::at(RefLoc::Client(module.to_string()), tag_types),
            ),
        ),
        description: ep
            .docstring
            .as_ref()
            .map(|description| format!("{description}{}", ep.reference_description_suffix)),
        example,
        example_gap: if !endpoint_has_worked_example(ep)
            || !path_object_documented(ep, &ir.types, &ir.tag_types)
            || matches!(ep.request_body, Some(RequestBody::Bytes { .. }))
            || ep.binary_schema_response && module.is_empty() && !ep.openapi_31
        {
            ""
        } else {
            "\n"
        },
        params,
    };
    let rendered = render(
        env,
        "reference_entry.md",
        "reference.md",
        minijinja::Value::from_serialize(&view),
    )?;
    Ok(rendered.trim_end_matches('\n').to_string())
}

fn reference_list_annotation(annotation: &str) -> String {
    annotation.replace("typing.Sequence[", "typing.List[")
}

pub(crate) fn reference_param_annotation(annotation: &str) -> String {
    let mut annotation = annotation.replace("dt.", "datetime.");
    if let Some(inner) = annotation
        .strip_prefix("typing.Optional[typing.Optional[")
        .and_then(|value| value.strip_suffix("]]"))
    {
        annotation = format!("typing.Optional[{inner}]");
    }
    annotation
}

/// The ` -> <type>` suffix on a `reference.md` entry's `<summary>` line. Fern's
/// markdown writer spells the datetime types out in full (`datetime.datetime`)
/// rather than with the `dt.` alias the generated Python imports them under —
/// the same prose spelling [`reference_param_annotation`] applies to the
/// parameter rows, so no golden's `reference.md` ever carries a `dt.` type.
///
/// A stream is headed with the iterator its method returns, `chunk` typed —
/// where Fern heads every stream `typing.Iterator[bytes]` (the
/// `stream-reference-return-type` departure); a binary download does return
/// `typing.Iterator[bytes]`.
fn reference_return_type(ep: &Endpoint, response: &str, chunk: &str) -> String {
    let streamed = format!("typing.Iterator[{chunk}]");
    let rendered = if ep.streaming {
        streamed.as_str()
    } else if ep.binary_response {
        "typing.Iterator[bytes]"
    } else {
        response
    };
    if rendered == "None" {
        String::new()
    } else {
        format!(" -> {}", rendered.replace("dt.", "datetime."))
    }
}

/// Render `from <module> import <names>` at `indent` spaces, sorted, on one line.
/// `ruff format` wraps it into a parenthesized per-name block if it overflows.
/// Returns the statement with its trailing newline.
fn from_import_block(module: &str, names: &[String], indent: usize) -> String {
    // An empty group emits nothing — a `from <module> import` with no names is a
    // syntax error, and the source module (e.g. `errors/`) is not emitted when
    // there is nothing to import from it. The corpus always has errors, so this
    // path is only reached for specs that declare none.
    if names.is_empty() {
        return String::new();
    }
    let pad = " ".repeat(indent);
    let mut names = names.to_vec();
    names.sort();
    format!("{pad}from {module} import {}\n", names.join(", "))
}

/// Fern's SDK runtime, vendored under `assets/core/` and emitted into every SDK.
/// Each entry is (path under `core/`, verbatim contents). `client_wrapper.py` is
/// *not* here — Fern shapes it from the auth model, so it is generated by
/// [`client_wrapper_file`]. See `assets/README.md` and `NOTICE` for provenance.
const CORE_ASSETS: &[(&str, &str)] = &[
    ("__init__.py", include_str!("../assets/core/__init__.py")),
    ("api_error.py", include_str!("../assets/core/api_error.py")),
    (
        "datetime_utils.py",
        include_str!("../assets/core/datetime_utils.py"),
    ),
    ("enum.py", include_str!("../assets/core/enum.py")),
    ("file.py", include_str!("../assets/core/file.py")),
    (
        "force_multipart.py",
        include_str!("../assets/core/force_multipart.py"),
    ),
    (
        "http_client.py",
        include_str!("../assets/core/http_client.py"),
    ),
    (
        "http_response.py",
        include_str!("../assets/core/http_response.py"),
    ),
    (
        "http_sse/__init__.py",
        include_str!("../assets/core/http_sse/__init__.py"),
    ),
    (
        "http_sse/_api.py",
        include_str!("../assets/core/http_sse/_api.py"),
    ),
    (
        "http_sse/_decoders.py",
        include_str!("../assets/core/http_sse/_decoders.py"),
    ),
    (
        "http_sse/_exceptions.py",
        include_str!("../assets/core/http_sse/_exceptions.py"),
    ),
    (
        "http_sse/_models.py",
        include_str!("../assets/core/http_sse/_models.py"),
    ),
    (
        "jsonable_encoder.py",
        include_str!("../assets/core/jsonable_encoder.py"),
    ),
    ("logging.py", include_str!("../assets/core/logging.py")),
    (
        "parse_error.py",
        include_str!("../assets/core/parse_error.py"),
    ),
    (
        "pydantic_utilities.py",
        include_str!("../assets/core/pydantic_utilities.py"),
    ),
    (
        "query_encoder.py",
        include_str!("../assets/core/query_encoder.py"),
    ),
    (
        "remove_none_from_dict.py",
        include_str!("../assets/core/remove_none_from_dict.py"),
    ),
    (
        "request_options.py",
        include_str!("../assets/core/request_options.py"),
    ),
    (
        "serialization.py",
        include_str!("../assets/core/serialization.py"),
    ),
];

/// Placeholders substituted in `client_wrapper.py` and the scaffolding files.
const SDK_NAME_PLACEHOLDER: &str = "@@CROZIER_SDK_NAME@@";
const SDK_VERSION_PLACEHOLDER: &str = "@@CROZIER_SDK_VERSION@@";
/// The import package name (directory under `src/`) placeholder in `pyproject.toml`.
const PACKAGE_PLACEHOLDER: &str = "@@CROZIER_PACKAGE@@";
/// Default SDK version stamped into the runtime (Fern uses `0.0.0` when none is
/// configured). Not yet exposed as a flag. The `sdk-name-version-headers`
/// departure recognises exactly this value.
pub(crate) const DEFAULT_SDK_VERSION: &str = "0.0.0";

/// The one `.fern/metadata.json` crozier writes, whatever it is configured with;
/// the `fern-metadata-generator-config` departure recognises exactly this record.
pub(crate) const FERN_METADATA_RECORD: &str = include_str!("../assets/scaffolding/metadata.json");

/// The Fern pair crozier is certified against: a Fern CLI version and the
/// `fernapi/fern-python-sdk` generator version it drives.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FernPair {
    /// The Fern CLI version (`cliVersion`).
    pub cli: &'static str,
    /// The `fernapi/fern-python-sdk` version (`generatorVersion`).
    pub python_sdk: &'static str,
}

/// The certified Fern pair, read from `assets/scaffolding/metadata.json` — the
/// one declaration of it. Tests and tools derive every spelling of the pair from
/// that file rather than restating it.
pub fn certified_fern_pair() -> FernPair {
    #[derive(serde::Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct Record {
        cli_version: &'static str,
        generator_version: &'static str,
    }
    // The record is compiled in; a malformed one fails every test that reads it.
    let record: Record = serde_json::from_str(FERN_METADATA_RECORD)
        .expect("assets/scaffolding/metadata.json names cliVersion and generatorVersion");
    FernPair {
        cli: record.cli_version,
        python_sdk: record.generator_version,
    }
}

/// Project scaffolding Fern emits verbatim apart from project/package names and
/// the SDK version. The Python test/default-client templates carry the same
/// substitutions so non-`fern` package names remain importable. Vendored under
/// `assets/scaffolding/` (Apache-2.0; see `NOTICE`).
///
/// The flat layout drops the packaging (`pyproject.toml`, `requirements.txt`), and
/// its aiohttp install hint names the package (import) name where the packaged
/// form names the distribution — Fern's flat tree has no distribution to name.
fn scaffolding_files(pkg: &str, project_name: &str, layout: Layout) -> Vec<GeneratedFile> {
    let pyproject = include_str!("../assets/scaffolding/pyproject.toml")
        .replace(SDK_NAME_PLACEHOLDER, project_name)
        .replace(PACKAGE_PLACEHOLDER, pkg)
        .replace(SDK_VERSION_PLACEHOLDER, DEFAULT_SDK_VERSION);
    let install_name = match layout {
        Layout::Packaged => project_name,
        Layout::Flat => pkg,
    };
    let substitute_names = |contents: &str| {
        contents
            .replace(SDK_NAME_PLACEHOLDER, install_name)
            .replace(PACKAGE_PLACEHOLDER, pkg)
    };
    let mut default_clients = substitute_names(include_str!(
        "../assets/scaffolding/default_clients.py.tmpl"
    ));
    // The vendored file carries the hint as ruff lays out a mid-length name: the
    // call split over three lines. Ruff joins it back onto one line when that fits
    // in 120 columns (a short name, e.g. a flat tree's `fern`), and splits the
    // string itself when even the split call overflows. Only the flat layout
    // joins it, so no packaged output changes with the layout setting.
    let hint = format!(
        "\"To use the aiohttp client, install the aiohttp extra: pip install {install_name}[aiohttp]\""
    );
    let one_line = format!("            raise RuntimeError({hint})");
    if layout == Layout::Flat && one_line.chars().count() <= crate::pyfmt::LINE_LENGTH {
        default_clients = default_clients.replace(
            &format!("            raise RuntimeError(\n                {hint}\n            )"),
            &one_line,
        );
    } else if default_clients.lines().any(|line| line.len() > 120) {
        default_clients = default_clients.replace(
            &hint,
            &format!(
                "\"To use the aiohttp client, install the aiohttp extra: \"\n                \"pip install {install_name}[aiohttp]\""
            ),
        );
    }
    let mut aiohttp_test = substitute_names(include_str!(
        "../assets/scaffolding/test_aiohttp_autodetect.py.tmpl"
    ));
    // Fern's isort pass files the SDK's own import in a first-party section of
    // its own when ruff can find the package: always in the packaged tree
    // (`src/<pkg>/`), and in the flat tree only for a package named `fern`, which
    // ruff resolves inside Fern's `/fern` container. Any other flat package is
    // third-party to it and joins `import httpx_aiohttp` with no blank line.
    if layout == Layout::Flat && pkg != "fern" {
        aiohttp_test = aiohttp_test.replace(
            &format!("        import httpx_aiohttp  # type: ignore[import-not-found]\n\n        from {pkg}."),
            &format!("        import httpx_aiohttp  # type: ignore[import-not-found]\n        from {pkg}."),
        );
    }
    let mut files = Vec::new();
    if layout == Layout::Packaged {
        files.push(GeneratedFile {
            path: PathBuf::from("pyproject.toml"),
            contents: pyproject,
        });
        files.push(GeneratedFile {
            path: PathBuf::from("requirements.txt"),
            contents: include_str!("../assets/scaffolding/requirements.txt").to_string(),
        });
    }
    files.extend([
        GeneratedFile {
            path: PathBuf::from(".fern/metadata.json"),
            contents: FERN_METADATA_RECORD.to_string(),
        },
        GeneratedFile {
            path: PathBuf::from("CONTRIBUTING.md"),
            contents: include_str!("../assets/scaffolding/CONTRIBUTING.md").to_string(),
        },
        GeneratedFile {
            path: PathBuf::from(format!("src/{pkg}/_default_clients.py")),
            contents: default_clients,
        },
        GeneratedFile {
            path: PathBuf::from("tests/conftest.py"),
            contents: include_str!("../assets/scaffolding/conftest.py").to_string(),
        },
        GeneratedFile {
            path: PathBuf::from("tests/test_aiohttp_autodetect.py"),
            contents: aiohttp_test,
        },
    ]);
    files
}

/// Emit the vendored core runtime for a package. The runtime assets are emitted
/// verbatim; `client_wrapper.py` is generated separately (see
/// [`client_wrapper_file`]) because Fern shapes it from the auth model. Fern
/// ships `core/enum.py` (the `StrEnum` base) only when enums render as classes,
/// so a literals SDK omits it.
fn core_files(pkg: &str, paginated: bool, enum_type: EnumType, auth: &Auth) -> Vec<GeneratedFile> {
    let mut files: Vec<GeneratedFile> = CORE_ASSETS
        .iter()
        .filter(|(rel, _)| *rel != "enum.py" || enum_type == EnumType::PythonEnums)
        .map(|(rel, content)| GeneratedFile {
            path: PathBuf::from(format!("src/{pkg}/core/{rel}")),
            contents: if paginated && *rel == "__init__.py" {
                core_init_with_pagination(content)
            } else if *rel == "http_client.py" {
                http_client_with_auth_headers(content, auth)
            } else {
                (*content).to_string()
            },
        })
        .collect();
    // The pager runtime ships only with an SDK that has a paginated endpoint —
    // Fern emits `core/pagination.py` and exports its two classes from `core`
    // exactly then, and leaves both out otherwise.
    if paginated {
        files.push(GeneratedFile {
            path: PathBuf::from(format!("src/{pkg}/core/pagination.py")),
            contents: include_str!("../assets/core/pagination.py").to_string(),
        });
    }
    files
}

/// The empty `_AUTH_HEADERS` set the vendored `core/http_client.py` declares.
const EMPTY_AUTH_HEADERS: &str = "_AUTH_HEADERS: typing.FrozenSet[str] = frozenset()\n";

/// `core/http_client.py` with `_AUTH_HEADERS` naming the header an `apiKey`
/// credential travels in, lower-cased, so its logged requests redact it. Fern
/// names only header schemes there: a bearer or basic credential's
/// `Authorization` is already in the runtime's own sensitive set, and the set
/// stays empty for it as for an unauthenticated API.
fn http_client_with_auth_headers(asset: &str, auth: &Auth) -> String {
    let header = match auth {
        Auth::ApiKey { header, .. } => Some(header.to_ascii_lowercase()),
        Auth::Bearer { .. } | Auth::Basic { .. } | Auth::None => None,
    };
    debug_assert!(
        asset.contains(EMPTY_AUTH_HEADERS),
        "core/http_client.py asset no longer carries {EMPTY_AUTH_HEADERS:?}"
    );
    match header {
        Some(header) => asset.replacen(
            EMPTY_AUTH_HEADERS,
            &format!(
                "_AUTH_HEADERS: typing.FrozenSet[str] = frozenset({{{}}})\n",
                literal_enum_value(&header)
            ),
            1,
        ),
        None => asset.to_owned(),
    }
}

/// `core/__init__.py` with the two pager names spliced into its three sorted
/// blocks. The asset is verbatim Fern output, so the splices are anchored on the
/// neighbours the sort puts them beside rather than re-rendering the file.
fn core_init_with_pagination(asset: &str) -> String {
    let mut out = asset.to_string();
    for (anchor, inserted) in [
        (
            "    from .parse_error import ParsingError\n",
            "    from .pagination import AsyncPager, SyncPager\n",
        ),
        (
            "    \"BaseClientWrapper\": \".client_wrapper\",\n",
            "    \"AsyncPager\": \".pagination\",\n",
        ),
        (
            "    \"UniversalBaseModel\": \".pydantic_utilities\",\n",
            "    \"SyncPager\": \".pagination\",\n",
        ),
        ("    \"BaseClientWrapper\",\n", "    \"AsyncPager\",\n"),
        ("    \"UniversalBaseModel\",\n", "    \"SyncPager\",\n"),
    ] {
        debug_assert!(
            out.contains(anchor),
            "core/__init__.py asset no longer carries {anchor:?}"
        );
        out = out.replacen(anchor, &format!("{inserted}{anchor}"), 1);
    }
    out
}

/// The auth-varying fragments of `client_wrapper.py`: the credential constructor
/// parameter, its assignment in `BaseClientWrapper.__init__`, the `get_headers`
/// lines that apply it, the optional `_get_token` helper, and the `super().__init__`
/// argument. Each fragment carries its own trailing newline where the template
/// expects one.
struct AuthWrapper {
    /// Each credential's `(name, constructor parameter line)`; a credential a
    /// promoted header of the same name already declares is not declared again.
    params: Vec<(String, String)>,
    assign: String,
    header_block: String,
    token_method: String,
    /// Whether the credential is a bearer token sent in a header — Fern's
    /// `_has_flat_token_header`. Such a wrapper's `get_headers` takes an
    /// `include_token` flag, and `async_get_headers` turns it off when an async
    /// token supplier is set, so a blocking token callable never runs on the event
    /// loop (`airbyte.local-config`, `squareup.com`, `xero.com-xero-payroll-au`;
    /// measured at fern-python-sdk 5.41.3).
    flat_token: bool,
}

impl AuthWrapper {
    /// The constructor parameters no name in `taken` already declares.
    fn param(&self, taken: &std::collections::HashSet<&str>) -> String {
        self.params
            .iter()
            .filter(|(name, _)| !taken.contains(name.as_str()))
            .map(|(_, line)| line.as_str())
            .collect()
    }

    /// The `super().__init__` arguments no name in `taken` already passes.
    fn super_arg(&self, taken: &std::collections::HashSet<&str>) -> String {
        self.params
            .iter()
            .filter(|(name, _)| !taken.contains(name.as_str()))
            .map(|(name, _)| format!("{name}={name}, "))
            .collect()
    }
}

fn auth_wrapper_parts(auth: &Auth) -> AuthWrapper {
    // The callable-or-string getter Fern writes for a bearer or basic credential.
    let getter = |param: &str, required: bool| {
        if required {
            format!("    def _get_{param}(self) -> str:\n        if isinstance(self._{param}, str):\n            return self._{param}\n        else:\n            return self._{param}()\n\n")
        } else {
            format!("    def _get_{param}(self) -> typing.Optional[str]:\n        if isinstance(self._{param}, str) or self._{param} is None:\n            return self._{param}\n        else:\n            return self._{param}()\n\n")
        }
    };
    let callable = |required: bool| {
        if required {
            "typing.Union[str, typing.Callable[[], str]]".to_string()
        } else {
            "typing.Optional[typing.Union[str, typing.Callable[[], str]]] = None".to_string()
        }
    };
    match auth {
        Auth::ApiKey {
            header,
            required,
            credential,
            prefix,
        } => {
            let p = &credential.param;
            let value = prefix.as_ref().map_or_else(
                || format!("self.{p}"),
                |prefix| format!("f\"{} {{self.{p}}}\"", escape_py_str(prefix)),
            );
            let (ty, header_block) = if *required {
                (
                    "str".to_string(),
                    format!("        headers[\"{header}\"] = {value}\n"),
                )
            } else {
                (
                    "typing.Optional[str] = None".to_string(),
                    format!("        if self.{p} is not None:\n            headers[\"{header}\"] = {value}\n"),
                )
            };
            AuthWrapper {
                params: vec![(p.clone(), format!("        {p}: {ty},\n"))],
                assign: format!("        self.{p} = {p}\n"),
                header_block,
                token_method: String::new(),
                flat_token: false,
            }
        }
        Auth::Bearer {
            required,
            credential,
        } => {
            let p = &credential.param;
            // The token is written under the `include_token` flag (see
            // [`AuthWrapper::flat_token`]).
            let header_block = if *required {
                format!("        if include_token:\n            headers[\"Authorization\"] = f\"Bearer {{self._get_{p}()}}\"\n")
            } else {
                format!("        if include_token:\n            {p} = self._get_{p}()\n            if {p} is not None:\n                headers[\"Authorization\"] = f\"Bearer {{{p}}}\"\n")
            };
            AuthWrapper {
                params: vec![(
                    p.clone(),
                    format!("        {p}: {},\n", callable(*required)),
                )],
                assign: format!("        self._{p} = {p}\n"),
                header_block,
                token_method: getter(p, *required),
                flat_token: true,
            }
        }
        Auth::Basic {
            required,
            username,
            password,
        } => {
            let (u, w) = (&username.param, &password.param);
            let header_block = if *required {
                format!("        headers[\"Authorization\"] = httpx.BasicAuth(self._get_{u}(), self._get_{w}())._auth_header\n")
            } else {
                format!("        {u} = self._get_{u}()\n        {w} = self._get_{w}()\n        if {u} is not None and {w} is not None:\n            headers[\"Authorization\"] = httpx.BasicAuth({u}, {w})._auth_header\n")
            };
            AuthWrapper {
                params: vec![
                    (
                        u.clone(),
                        format!("        {u}: {},\n", callable(*required)),
                    ),
                    (
                        w.clone(),
                        format!("        {w}: {},\n", callable(*required)),
                    ),
                ],
                assign: format!("        self._{u} = {u}\n        self._{w} = {w}\n"),
                header_block,
                token_method: getter(u, *required) + &getter(w, *required),
                flat_token: false,
            }
        }
        // No auth: no credential parameter, assignment, header, or token helper.
        Auth::None => AuthWrapper {
            params: Vec::new(),
            assign: String::new(),
            header_block: String::new(),
            token_method: String::new(),
            flat_token: false,
        },
    }
}

/// The auth-varying fragments of the root client: the constructor parameter, the
/// credential name passed to the client wrapper, the docstring `Parameters` line,
/// and the `Examples` instantiation argument.
struct AuthClient {
    ctor_param: String,
    wrapper_arg: String,
    doc_param: String,
    example_line: String,
    /// The checks a required credential read from the environment gets once the
    /// client is built.
    check: String,
}

fn auth_client_parts(auth: &Auth, taken: &std::collections::HashSet<&str>) -> AuthClient {
    // Each credential: its parameter, its type, and whether the client requires
    // it; basic auth carries two (`username`/`password`), every other scheme one.
    let callable = "typing.Union[str, typing.Callable[[], str]]";
    let creds: Vec<(&Credential, &str, bool)> = match auth {
        Auth::ApiKey {
            required,
            credential,
            ..
        } => vec![(credential, "str", *required)],
        Auth::Bearer {
            required,
            credential,
        } => vec![(credential, callable, *required)],
        Auth::Basic {
            required,
            username,
            password,
        } => vec![
            (username, callable, *required),
            (password, callable, *required),
        ],
        Auth::None => Vec::new(),
    };
    // A credential read from the environment is optional in the signature,
    // defaulting to `None`: the variable is read when the client is built (see
    // [`env_fallback`]), not when the module is imported (`x-fern-bearer: {env:
    // …}`).
    let ctor_ty = |credential: &Credential, ty: &str, required: bool| match &credential.env {
        Some(_) => format!("typing.Optional[{ty}] = None"),
        None if required => ty.to_string(),
        None => format!("typing.Optional[{ty}] = None"),
    };
    let doc_ty = |credential: &Credential, ty: &str, required: bool| {
        if required && credential.env.is_none() {
            ty.to_string()
        } else {
            format!("typing.Optional[{ty}]")
        }
    };
    let declared = |credential: &&Credential| !taken.contains(credential.param.as_str());
    let ctor_param: String = creds
        .iter()
        .filter(|(credential, _, _)| declared(credential))
        .map(|(credential, ty, required)| {
            format!(
                "        {}: {},\n",
                credential.param,
                ctor_ty(credential, ty, *required)
            )
        })
        .collect();
    let doc_param: String = creds
        .iter()
        .map(|(credential, ty, required)| {
            format!(
                "    {} : {}\n",
                credential.param,
                doc_ty(credential, ty, *required)
            )
        })
        .collect();
    // The wrapper call passes each credential through; the template supplies the
    // final newline, so the joined block carries none.
    let wrapper_arg = creds
        .iter()
        .filter(|(credential, _, _)| declared(credential))
        .map(|(credential, _, _)| format!("            {0}={0},\n", credential.param))
        .collect::<String>()
        .trim_end_matches('\n')
        .to_string();
    // A credential sharing a promoted header's parameter is passed once (the
    // `repeated-credential-example-keyword` departure).
    let example_line: String = auth_example_args(auth)
        .iter()
        .filter(|arg| !taken.contains(keyword_name(arg)))
        .map(|arg| format!("        {arg},\n"))
        .collect();
    let check: String = creds
        .iter()
        .filter(|(credential, _, _)| declared(credential))
        .filter_map(|(credential, _, required)| {
            Some(env_fallback(
                &credential.param,
                credential.env.as_ref()?,
                *required,
            ))
        })
        .collect();
    AuthClient {
        ctor_param,
        wrapper_arg,
        doc_param,
        example_line,
        check,
    }
}

/// The root client's read of the environment variable `env` for a constructor
/// field the caller left `None`, and, for a `required` one, the check that it
/// was given one way or the other. Fern reads the variable when the client is
/// built, not as the signature default (`lock-keeper-vault`, `ski-lift-gates`,
/// `relay-station-header-names`; measured at fern-python-sdk 5.41.3).
fn env_fallback(param: &str, env: &str, required: bool) -> String {
    let env = escape_py_str(env);
    let fallback =
        format!("        {param} = {param} if {param} is not None else os.getenv(\"{env}\")\n");
    if required {
        format!(
            "{fallback}        if {param} is None:\n            raise ApiError(body=\"The client must be instantiated be either passing in {param} or setting {env}\")\n"
        )
    } else {
        fallback
    }
}

/// The environment variable a promoted header's constructor field defaults to:
/// an additional header `apiKey` scheme's declared `env`.
fn header_env(header: &GlobalHeader) -> Option<&str> {
    header.credential.as_ref()?.env.as_deref()
}

/// Every credential of `auth`, in constructor order.
fn auth_credentials(auth: &Auth) -> Vec<&Credential> {
    auth.credentials()
}

/// The credential arguments in a worked `Examples` client instantiation, e.g.
/// `["token=\"YOUR_TOKEN\""]` or, for basic auth, both `username`/`password` lines
/// (each with no indent or trailing comma — the caller adds those).
fn auth_example_args(auth: &Auth) -> Vec<String> {
    auth_credentials(auth)
        .into_iter()
        .map(|credential| {
            // A keyword's escape (`class_`) is not in the placeholder: Fern
            // writes `class_="YOUR_CLASS"`.
            let stem = credential
                .param
                .strip_suffix('_')
                .filter(|stem| naming::is_python_keyword(stem))
                .unwrap_or(&credential.param);
            format!(
                "{}=\"YOUR_{}\"",
                credential.param,
                stem.to_ascii_uppercase()
            )
        })
        .collect()
}

/// A header parameter's synthesized example. Fern fills a header with its own
/// name, and an array header with two such elements
/// (`probe_param=["probeParam", "probeParam"]`) where a list anywhere else holds
/// one.
fn header_example(value: Example) -> Example {
    match value {
        Example::List(items) if items.len() == 1 => {
            Example::List(vec![items[0].clone(), items[0].clone()])
        }
        Example::ReferenceList(items) if items.len() == 1 => {
            Example::ReferenceList(vec![items[0].clone(), items[0].clone()])
        }
        other => other,
    }
}

/// The subsequence of promoted global headers that carries a distinct Python
/// parameter name, keeping the first of each. Two header apiKey schemes can
/// normalize alike — EN 18222 declares both `X-API-KEY` and `API-KEY`, and both
/// give `api_key` — and Fern splits them: each keeps its own assignment, header
/// write, docstring line, and example argument, but the emitted *signatures* and
/// call sites carry one `api_key`, because a repeated keyword there is a Python
/// syntax error rather than a redundancy.
fn distinct_global_header_params(global_headers: &[GlobalHeader]) -> Vec<&GlobalHeader> {
    let mut seen = std::collections::HashSet::new();
    global_headers
        .iter()
        .filter(|h| seen.insert(h.py_name.as_str()))
        .collect()
}

/// Generate `core/client_wrapper.py`, shaped by the SDK's [`Auth`] model. The
/// bearer-optional form is byte-identical to Fern's default wrapper; api-key and
/// required-credential forms swap the constructor parameter, the header wiring,
/// and the token helper. Every wrapper's `max_retries` parameter defaults to
/// `default_max_retries`, as Fern's does to its `default_max_retries`.
/// Assembled from literal blocks (no source-line continuations, which would eat
/// the Python indentation).
fn client_wrapper_file(
    pkg: &str,
    sdk_name: Option<&str>,
    auth: &Auth,
    global_headers: &[GlobalHeader],
    client_path_parameters: &[ClientPathParameter],
    default_max_retries: u32,
    environment: Option<&crate::ir::Environment>,
) -> GeneratedFile {
    let a = auth_wrapper_parts(auth);
    // A defaulted header trails every built-in field instead (see
    // [`GlobalHeader::default`]), so it is split off the leading ones. The
    // `353-string-default-*` authored probes byte-match this file end to end.
    let (trailing, leading): (Vec<GlobalHeader>, Vec<GlobalHeader>) = global_headers
        .iter()
        .cloned()
        .partition(|h| h.default().is_some());
    // A credential a promoted header of the same name already declares is not
    // declared or passed again: Fern's two `X-Api-Key` schemes, or a bearer named
    // `api_key` beside one, share the header's `api_key` parameter.
    let taken: std::collections::HashSet<&str> =
        leading.iter().map(|h| h.py_name.as_str()).collect();
    let tr_param: String = trailing
        .iter()
        .map(|h| format!("        {}: typing.Optional[str] = None,\n", h.py_name))
        .collect();
    let tr_assign: String = trailing
        .iter()
        .map(|h| format!("        self._{0} = {0}\n", h.py_name))
        .collect();
    let tr_super: String = trailing
        .iter()
        .map(|h| format!("            {0}={0},\n", h.py_name))
        .collect();
    // Promoted global headers: a constructor parameter, an assignment, a
    // `get_headers` block, and a `super().__init__` argument — all emitted before
    // the auth credential's, matching Fern's ordering. A required header is a
    // mandatory `str` set unconditionally; an optional one is `Optional[str] = None`
    // set only when provided.
    // A lifted base-path parameter leads them: a field the routes read, never a
    // header.
    let path_param: String = client_path_parameters
        .iter()
        .map(|parameter| client_path_parameter_param(parameter, false))
        .collect();
    let client_param: String = path_param
        + &distinct_global_header_params(&leading)
            .into_iter()
            .map(|h| {
                if h.required() {
                    format!("        {}: {},\n", h.py_name, global_header_annotation(h))
                } else {
                    format!(
                        "        {}: typing.Optional[{}] = None,\n",
                        h.py_name,
                        global_header_annotation(h)
                    )
                }
            })
            .collect::<String>();
    let client_assign: String = client_path_parameters
        .iter()
        .map(|parameter| &parameter.py_name)
        .chain(leading.iter().map(|h| &h.py_name))
        .map(|name| format!("        self._{name} = {name}\n"))
        .collect();
    let header_line = |h: &GlobalHeader| {
        // A non-string header is written as its `str()`, as Fern's is.
        let value = if h.py_type() == HeaderType::Str {
            format!("self._{}", h.py_name)
        } else {
            format!("str(self._{})", h.py_name)
        };
        if let Some(default) = h.default() {
            format!(
                "        headers[\"{}\"] = {value} if {value} is not None else \"{}\"\n",
                escape_py_str(&h.wire_name),
                escape_py_str(default)
            )
        } else if h.required() {
            format!(
                "        headers[\"{}\"] = {value}\n",
                escape_py_str(&h.wire_name)
            )
        } else {
            format!(
                "        if self._{} is not None:\n            headers[\"{}\"] = {value}\n",
                h.py_name,
                escape_py_str(&h.wire_name)
            )
        }
    };
    // A defaulted header is written after the credential, as its field follows
    // `logging`: Fern's `Accept`, defaulted on every operation, comes after the
    // api-key header it sits beside.
    let gh_header: String = global_headers
        .iter()
        .filter(|h| h.default().is_none())
        .map(header_line)
        .collect();
    let tr_header: String = global_headers
        .iter()
        .filter(|h| h.default().is_some())
        .map(header_line)
        .collect();
    let client_super: String = client_path_parameters
        .iter()
        .map(|parameter| &parameter.py_name)
        .chain(
            distinct_global_header_params(&leading)
                .into_iter()
                .map(|h| &h.py_name),
        )
        .map(|name| format!("{name}={name}, "))
        .collect();
    // crozier brands its own SDK-identity headers rather than impersonating Fern:
    // the `sdk-identity-header-prefix` departure (docs/departures/README.md),
    // which the comparison recognises line by line.
    // The SDK-identity pair names the published distribution, so only the
    // packaged form (`sdk_name` present) sends it.
    let sdk_identity = sdk_name.map_or_else(String::new, |name| {
        format!(
            "            \"X-Crozier-SDK-Name\": \"{name}\",\n            \"X-Crozier-SDK-Version\": \"{DEFAULT_SDK_VERSION}\",\n"
        )
    });
    let include_token_param = if a.flat_token {
        ", *, include_token: bool = True"
    } else {
        ""
    };
    let get_headers_head = format!(
        "        self._headers = headers\n        self._base_url = base_url\n        self._timeout = timeout\n        self._max_retries = max_retries\n        self._stream_reconnection_enabled = stream_reconnection_enabled\n        self._max_stream_reconnection_attempts = max_stream_reconnection_attempts\n        self._logging = logging\n{tr_assign}\n    def get_headers(self{include_token_param}) -> typing.Dict[str, str]:\n        import platform\n\n        headers: typing.Dict[str, str] = {{\n            \"X-Crozier-Language\": \"Python\",\n            \"X-Crozier-Runtime\": f\"python/{{platform.python_version()}}\",\n            \"X-Crozier-Platform\": f\"{{platform.system().lower()}}/{{platform.release()}}\",\n{sdk_identity}            **(self.get_custom_headers() or {{}}),\n        }}\n"
    );
    let mut c = String::new();
    // Lead with the generated-file header (like every other emitted module): it
    // survives `ruff format` and the e2e comment-strip folds it to the blank lines
    // Fern's own stripped header leaves, so the leading layout matches.
    c.push_str(HEADER);
    if global_headers
        .iter()
        .any(|header| matches!(header.py_type(), HeaderType::Date))
    {
        c.push_str("\n\nimport datetime as dt");
    } else {
        c.push('\n');
    }
    c.push_str("\nimport typing\n\nimport httpx\nfrom .http_client import AsyncHttpClient, HttpClient\nfrom .logging import LogConfig, Logger\n\n\nclass BaseClientWrapper:\n    def __init__(\n        self,\n        *,\n");
    c.push_str(&client_param);
    c.push_str(&a.param(&taken));
    c.push_str(&format!("        headers: typing.Optional[typing.Dict[str, str]] = None,\n        base_url: str,\n        timeout: typing.Optional[float] = None,\n        max_retries: int = {default_max_retries},\n        stream_reconnection_enabled: typing.Optional[bool] = None,\n        max_stream_reconnection_attempts: typing.Optional[int] = None,\n        logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,\n{tr_param}    ):\n"));
    c.push_str(&client_assign);
    c.push_str(&a.assign);
    c.push_str(&get_headers_head);
    // Fern applies the credential after the global headers for every scheme but
    // basic, which it applies before them: `squareup.com`, `exa-gate` and
    // `openepcis-dpp-ready` each write their api-key global headers ahead of a
    // bearer block declared before those schemes, while `cyberark-conjur-api`
    // writes its basic block ahead of the `Authorization` api-key header. No
    // golden pairs basic auth with an *ordinary* promoted header, so this places
    // it ahead of both kinds — the only ordering the corpus measures.
    let basic_auth = matches!(auth, Auth::Basic { .. });
    if basic_auth {
        c.push_str(&a.header_block);
    }
    c.push_str(&gh_header);
    if !basic_auth {
        c.push_str(&a.header_block);
    }
    c.push_str(&tr_header);
    c.push_str("        return headers\n\n");
    c.push_str(&a.token_method);
    c.push_str("    def get_custom_headers(self) -> typing.Optional[typing.Dict[str, str]]:\n        return self._headers\n\n    def get_base_url(self) -> str:\n        return self._base_url\n\n    def get_timeout(self) -> typing.Optional[float]:\n        return self._timeout\n\n    def get_max_retries(self) -> int:\n        return self._max_retries\n\n    def get_stream_reconnection_enabled(self) -> bool:\n        return self._stream_reconnection_enabled if self._stream_reconnection_enabled is not None else True\n\n    def get_max_stream_reconnection_attempts(self) -> typing.Optional[int]:\n        return self._max_stream_reconnection_attempts\n\n\nclass SyncClientWrapper(BaseClientWrapper):\n    def __init__(\n        self,\n        *,\n");
    c.push_str(&client_param);
    c.push_str(&a.param(&taken));
    c.push_str(&format!("        headers: typing.Optional[typing.Dict[str, str]] = None,\n        base_url: str,\n        timeout: typing.Optional[float] = None,\n        max_retries: int = {default_max_retries},\n        stream_reconnection_enabled: typing.Optional[bool] = None,\n        max_stream_reconnection_attempts: typing.Optional[int] = None,\n        logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,\n{tr_param}        httpx_client: httpx.Client,\n    ):\n        super().__init__(\n            "));
    c.push_str(&client_super);
    c.push_str(&a.super_arg(&taken));
    c.push_str("headers=headers,\n            base_url=base_url,\n            timeout=timeout,\n            max_retries=max_retries,\n            stream_reconnection_enabled=stream_reconnection_enabled,\n            max_stream_reconnection_attempts=max_stream_reconnection_attempts,\n            logging=logging,\n");
    c.push_str(&tr_super);
    c.push_str("        )\n        self.httpx_client = HttpClient(\n            httpx_client=httpx_client,\n            base_headers=self.get_headers,\n            base_timeout=self.get_timeout,\n            base_url=self.get_base_url,\n            base_max_retries=self.get_max_retries(),\n            logging_config=self._logging,\n        )\n\n\nclass AsyncClientWrapper(BaseClientWrapper):\n    def __init__(\n        self,\n        *,\n");
    c.push_str(&client_param);
    c.push_str(&a.param(&taken));
    c.push_str(&format!("        headers: typing.Optional[typing.Dict[str, str]] = None,\n        base_url: str,\n        timeout: typing.Optional[float] = None,\n        max_retries: int = {default_max_retries},\n        stream_reconnection_enabled: typing.Optional[bool] = None,\n        max_stream_reconnection_attempts: typing.Optional[int] = None,\n        logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,\n{tr_param}        async_token: typing.Optional[typing.Callable[[], typing.Awaitable[str]]] = None,\n        httpx_client: httpx.AsyncClient,\n    ):\n        super().__init__(\n            "));
    c.push_str(&client_super);
    c.push_str(&a.super_arg(&taken));
    c.push_str("headers=headers,\n            base_url=base_url,\n            timeout=timeout,\n            max_retries=max_retries,\n            stream_reconnection_enabled=stream_reconnection_enabled,\n            max_stream_reconnection_attempts=max_stream_reconnection_attempts,\n            logging=logging,\n");
    c.push_str(&tr_super);
    c.push_str("        )\n        self._async_token = async_token\n        self.httpx_client = AsyncHttpClient(\n            httpx_client=httpx_client,\n            base_headers=self.get_headers,\n            base_timeout=self.get_timeout,\n            base_url=self.get_base_url,\n            base_max_retries=self.get_max_retries(),\n            async_base_headers=self.async_get_headers,\n            logging_config=self._logging,\n        )\n\n    async def async_get_headers(self) -> typing.Dict[str, str]:\n        headers = self.get_headers(");
    if a.flat_token {
        c.push_str("include_token=self._async_token is None");
    }
    c.push_str(")\n        if self._async_token is not None:\n            token = await self._async_token()\n            headers[\"Authorization\"] = f\"Bearer {token}\"\n        return headers\n");
    // A multi-URL environment replaces the single `base_url` throughout: the
    // wrapper is built from, keeps and serves the environment, each request
    // naming its field's URL.
    let c = match environment.filter(|environment| !environment.urls.is_empty()) {
        None => c,
        Some(environment) => {
            let name = &environment.enum_name;
            c.replacen(
                "from .http_client import AsyncHttpClient, HttpClient\n",
                &format!("from ..environment import {name}\nfrom .http_client import AsyncHttpClient, HttpClient\n"),
                1,
            )
            .replace("        base_url: str,\n", &format!("        environment: {name},\n"))
            .replacen("        self._base_url = base_url\n", "        self._environment = environment\n", 1)
            .replacen(
                "    def get_base_url(self) -> str:\n        return self._base_url\n",
                &format!("    def get_environment(self) -> {name}:\n        return self._environment\n"),
                1,
            )
            .replace("            base_url=base_url,\n", "            environment=environment,\n")
            .replace("            base_url=self.get_base_url,\n", "")
        }
    };
    GeneratedFile {
        path: PathBuf::from(format!("src/{pkg}/core/client_wrapper.py")),
        contents: c,
    }
}

/// Render a docstring as an indented triple-quoted block, escaping backslashes
/// the way Fern's class and field writers do so the value round-trips. Every line
/// (including blank ones) carries the block indent; the block has no trailing
/// newline.
fn render_docstring(doc: &str, indent: usize) -> String {
    render_doc_block(doc, indent, true)
}

/// The same block for an *alias* module docstring, which Fern writes verbatim.
/// Only the class and field writers escape: `dnd5eapi.co`'s `DC.success_type`
/// field carries `\\"none\\"` for a source `\"none\"`, while Listen Notes'
/// `TranscriptField` alias describes "the newline character \n" and its golden
/// keeps that one backslash.
fn render_alias_docstring(doc: &str, indent: usize) -> String {
    render_doc_block(doc, indent, false)
}

fn render_doc_block(doc: &str, indent: usize, escape: bool) -> String {
    let pad = " ".repeat(indent);
    let mut out = format!("{pad}\"\"\"\n");
    for line in doc.split('\n') {
        out.push_str(&pad);
        if escape {
            out.push_str(&line.replace('\\', "\\\\"));
        } else {
            out.push_str(line);
        }
        out.push('\n');
    }
    out.push_str(&pad);
    out.push_str("\"\"\"");
    out
}

/// The class-body context for `class_body.py.j2`: the optional class docstring
/// and the fields. Shared by the object template (via `{% include %}`) and the
/// discriminated-union path, so one template lays out every pydantic model body.
#[derive(Serialize)]
struct ClassBodyView {
    docstring: Option<String>,
    fields: Vec<FieldView>,
    /// The `pydantic.ConfigDict(...)` argument list for the v2 `model_config`.
    config_dict_args: String,
    /// The v1 `Config`'s `pydantic.Extra.<extra_kind>` member, or `None` for a
    /// class whose config declares no extra-fields behaviour at all.
    extra_kind: Option<&'static str>,
    /// Methods rendered after the fields (an XML model's), class-body indented.
    members: Option<String>,
    /// Whether the v1 `Config` sets `copy_on_model_validation = "none"`.
    copy_on_model_validation: bool,
}

/// The two `extra`-fields fragments a pydantic model body needs: the v2
/// `pydantic.ConfigDict(...)` argument list, and the v1 `pydantic.Extra.<name>`
/// member. Fern **omits** the `extra=` kwarg under pydantic v2 for `ignore` (v2's
/// own default, so `pydantic.ConfigDict(frozen=True)`), while the v1 `Config`
/// always spells the member out.
fn extra_config(extra: ExtraFields) -> (String, &'static str) {
    let args = match extra {
        ExtraFields::Ignore => "frozen=True".to_string(),
        ExtraFields::Allow | ExtraFields::Forbid => {
            format!("extra=\"{}\", frozen=True", extra.as_str())
        }
    };
    (args, extra.as_str())
}

/// The flat field declaration line, `name: annotation = default`. `ruff format`
/// wraps a long annotation downstream.
fn field_decl(f: &RenderedField) -> String {
    format!("{}: {}{}", f.py_name, f.annotation.flat(), f.default)
}

/// Render a class body (docstring + fields + config block) with `class_body.py`.
/// Used directly for the discriminated-union variant classes; the object template
/// renders the same partial through `{% include %}`.
fn render_class_body(
    env: &Environment<'static>,
    docstring: Option<String>,
    fields: Vec<FieldView>,
    extra: Option<ExtraFields>,
) -> Result<String> {
    render_class_body_with(env, docstring, fields, extra, xml::XmlMembers::default())
}

/// [`render_class_body`] for a model that also carries methods: an XML model's
/// (see [`xml::XmlMembers`]).
fn render_class_body_with(
    env: &Environment<'static>,
    docstring: Option<String>,
    fields: Vec<FieldView>,
    extra: Option<ExtraFields>,
    members: xml::XmlMembers,
) -> Result<String> {
    // `None` is the value-wrapper form: `frozen=True` alone under v2, and a v1
    // `Config` that stops after `smart_union`.
    let (config_dict_args, extra_kind) = match extra {
        Some(extra) => {
            let (args, kind) = extra_config(extra);
            (args, Some(kind))
        }
        None => ("frozen=True".to_string(), None),
    };
    let view = ClassBodyView {
        docstring,
        fields,
        config_dict_args,
        extra_kind,
        members: members.methods,
        copy_on_model_validation: members.preserve_child_identity,
    };
    render(
        env,
        "class_body.py",
        "class_body",
        minijinja::Value::from_serialize(&view),
    )
}

fn update_forward_refs_call(target: &str, repair: &ForwardRepair) -> String {
    let mut names: Vec<&str> = repair.names.iter().map(String::as_str).collect();
    names.sort_by(|left, right| natural_cmp(left, right));
    let mut arguments = vec![target.to_string()];
    arguments.extend(names.into_iter().map(|name| format!("{name}={name}")));
    format!("update_forward_refs({})", arguments.join(", "))
}

/// The generator settings a declaration's rendered shape reads, bundled to keep
/// [`render_type_decl`] within the argument limit.
#[derive(Debug, Clone, Copy, Default)]
struct DeclSettings<'a> {
    /// Fern's `pydantic_config.extra_fields`: every model's `extra` config.
    extra: ExtraFields,
    /// Fern's `pydantic_config.enum_type`: an enum's class or literal form.
    enum_type: EnumType,
    /// Every declaration in the SDK, which an XML model reads to type its child
    /// elements (see [`xml::XmlTypes`]).
    xml_types: xml::XmlTypes<'a>,
}

/// Render one type declaration to a file body. `loc` is the file's location
/// (package-root `types/` or a tag's `types/`), which sets the `core`/type import
/// depth; `tag_types` maps hoisted type names to their tags for those references.
fn render_type_decl(
    env: &Environment<'static>,
    decl: &TypeDecl,
    loc: RefLoc,
    tag_types: &BTreeMap<String, String>,
    settings: DeclSettings<'_>,
    forward: &std::collections::HashSet<String>,
    repair: Option<&ForwardRepair>,
) -> Result<String> {
    // Reaching a cycle requires `from __future__` + `update_forward_refs`, even
    // when `forward` (this declaration's own quoted field refs) is empty.
    let needs_forward = repair.is_some();
    let empty_repair = ForwardRepair::default();
    let repair = repair.unwrap_or(&empty_repair);
    let DeclSettings {
        extra,
        enum_type,
        xml_types,
    } = settings;
    match decl {
        TypeDecl::Object(obj) => {
            let mut imports = Imports::at(loc, tag_types);
            // A recursive model's cyclic field references render as string forward
            // references and skip/defer their imports (issue #84).
            imports.forward = forward.clone();
            imports.forward.extend(repair.names.iter().cloned());
            imports.forward.retain(|name| !obj.bases.contains(name));
            imports.cur_module = obj.module.clone();
            // A model's own pydantic `class Config` holds the name `Config`, so a
            // component of that name is imported under its package path: Hasura's
            // `DataConnectorConnSourceConfig.value` is `types_config_Config`.
            let config_module = imports.type_import_path("Config");
            let config_alias = format!(
                "{}_Config",
                imports.package_module_path("Config").replace('.', "_")
            );
            imports
                .aliases
                .insert((config_module, "Config".to_string()), config_alias);
            imports.add_plain("typing");
            imports.add_plain("pydantic");
            imports.add_core("pydantic_utilities", "IS_PYDANTIC_V2");
            // Bases come from an `allOf`'s `$ref` members; with none, the model
            // extends Fern's `UniversalBaseModel`.
            let bases = if obj.bases.is_empty() {
                imports.add_core("pydantic_utilities", "UniversalBaseModel");
                "UniversalBaseModel".to_string()
            } else {
                for base in &obj.bases {
                    imports.add_type(base);
                }
                obj.bases.join(", ")
            };
            let xml_model = obj
                .xml
                .as_ref()
                .map(|encoding| xml::XmlModel::new(obj, encoding, xml_types));
            let own_fields = xml_model
                .as_ref()
                .map_or_else(|| obj.fields.clone(), xml::XmlModel::declared_fields);
            let mut fields: Vec<FieldView> = own_fields
                .iter()
                .map(|f| {
                    let rf = render_field(f, &mut imports);
                    FieldView {
                        decl: field_decl(&rf),
                        docstring: rf.docstring,
                    }
                })
                .collect();
            let members = match &xml_model {
                Some(model) => model.members(&mut imports, &mut fields),
                None => xml::XmlMembers::default(),
            };
            for name in &repair.import_order {
                imports.add_type(name);
            }
            if !forward.is_empty() || needs_forward {
                // A recursive model: `from __future__ import annotations` lets the
                // string forward refs resolve lazily, and `update_forward_refs`
                // rebuilds the model once the referenced names exist. A model that
                // only *inherits* its way into a cycle takes the future import and
                // nothing else.
                let repairs = !forward.is_empty() || repair.own_reach;
                if repairs {
                    imports.add_core("pydantic_utilities", "update_forward_refs");
                }
                let body = render_class_body_with(
                    env,
                    obj.docstring.clone(),
                    fields,
                    Some(extra),
                    members,
                )?;
                let mut file = format!(
                    "{HEADER}\n\nfrom __future__ import annotations\n\n{}\n\n\nclass {}({}):\n{}",
                    imports.render(),
                    obj.name,
                    bases,
                    body.trim_end(),
                );
                if !repairs {
                    file.push('\n');
                    return Ok(file);
                }
                // Two blank lines, then any deferred cross-module forward-ref
                // imports (a blank line after them), then the forward-ref repair.
                file.push_str("\n\n\n");
                if !imports.deferred.is_empty() {
                    // Fern's per-field order (`ForwardRepair::deferred_order`); a
                    // line naming nothing in it keeps the sorted tail.
                    let mut deferred: Vec<&String> = imports.deferred.iter().collect();
                    deferred.sort_by_key(|line| {
                        let name = line.rsplit(' ').next().unwrap_or_default();
                        repair
                            .deferred_order
                            .iter()
                            .position(|known| known == name)
                            .unwrap_or(usize::MAX)
                    });
                    for line in deferred {
                        file.push_str(line);
                        file.push('\n');
                    }
                    file.push('\n');
                }
                file.push_str(&update_forward_refs_call(&obj.name, repair));
                file.push('\n');
                return Ok(file);
            }
            let (config_dict_args, extra_kind) = extra_config(extra);
            let view = ObjectView {
                header: HEADER,
                imports: imports.render(),
                class_name: obj.name.clone(),
                bases,
                docstring: obj.docstring.clone(),
                fields,
                config_dict_args,
                extra_kind,
                future: xml_model.is_some(),
                copy_on_model_validation: members.preserve_child_identity,
                members: members.methods,
            };
            render(
                env,
                "object.py",
                &obj.name,
                minijinja::Value::from_serialize(&view),
            )
        }
        TypeDecl::Alias(alias) => {
            let mut imports = Imports::at(loc, tag_types);
            imports.forward = forward.clone();
            imports.cur_module = alias.module.clone();
            let target = render_type(&alias.target, &mut imports);
            let assignment = format!("{} = {}", alias.name, target.flat());
            // Fern drops descriptions from union aliases while preserving
            // them for scalar and collection aliases.
            let docstring = (!matches!(alias.target, TypeRef::Union(_)))
                .then_some(alias.docstring.as_deref())
                .flatten()
                .map(|doc| {
                    if doc.is_empty() {
                        "\"\"\"\n\"\"\"\n".to_string()
                    } else {
                        format!("{}\n", render_alias_docstring(doc, 0))
                    }
                })
                .unwrap_or_default();
            if !forward.is_empty() {
                let mut contents = format!(
                    "{HEADER}\n\nfrom __future__ import annotations\n\n{}\n",
                    imports.render()
                );
                if !imports.deferred.is_empty() {
                    contents.push_str("\nif typing.TYPE_CHECKING:\n");
                    for line in &imports.deferred {
                        contents.push_str(&format!("    {line}\n"));
                    }
                }
                contents.push_str(&format!("{assignment}\n{docstring}"));
                return Ok(contents);
            }
            render(
                env,
                "alias.py",
                &alias.name,
                context! {
                    header => HEADER,
                    imports => imports.render(),
                    assignment => assignment,
                    docstring => docstring,
                },
            )
        }
        TypeDecl::Enum(e) if enum_type == EnumType::Literals => render_literal_enum(env, e),
        TypeDecl::Enum(e) => render_enum(env, e, &loc),
        TypeDecl::DiscriminatedUnion(union) => {
            render_discriminated_union(env, union, extra, loc, tag_types, forward, repair)
        }
    }
}

/// Render a string enum to its module: a `class {Name}(enum.StrEnum)` with a
/// `SCREAMING_SNAKE = "value"` member per value and a `visit` dispatch method
/// (Fern's `enum_type: python_enums` shape). Fern's compatibility enum is under
/// the package-root `core`, so tag-hoisted types need one more relative dot.
fn render_enum(
    env: &Environment<'static>,
    e: &crate::ir::EnumType,
    loc: &RefLoc,
) -> Result<String> {
    // A nested client module (`doc/forms`) sits one level deeper per segment:
    // CloudPDF's `ExportDataFormsRequestFormat` imports `from ....core`.
    let core_parent = match loc {
        RefLoc::TagTypes(module) => format!("{}core", ".".repeat(module_depth(module) + 2)),
        RefLoc::RootTypes => "..core".to_string(),
        RefLoc::Client(_) | RefLoc::Errors | RefLoc::PackageRoot => ".core".to_string(),
    };
    let mut body = format!(
        "import typing\n\nfrom {core_parent} import enum\n\nT_Result = typing.TypeVar(\"T_Result\")\n\n\nclass "
    );
    body.push_str(&e.name);
    body.push_str("(enum.StrEnum):\n");
    if let Some(doc) = &e.docstring {
        // Fern renders the schema description as a class docstring, then a blank
        // line before the first member.
        body.push_str(&render_docstring(doc, 4));
        body.push_str("\n\n");
    }
    for m in &e.members {
        body.push_str(&format!(
            "    {} = \"{}\"\n",
            m.name,
            escape_py_str(&m.value)
        ));
        if let Some(doc) = &m.docstring {
            body.push_str(&render_docstring(doc, 4));
            body.push_str("\n\n");
        }
    }
    body.push('\n');
    // Two members Fern names alike share one callback, listed once.
    let mut listed = std::collections::HashSet::new();
    let params: Vec<String> = e
        .members
        .iter()
        .filter(|m| listed.insert(m.visit_param.as_str()))
        .map(|m| format!("{}: typing.Callable[[], T_Result]", m.visit_param))
        .collect();
    body.push_str(&format!(
        "    def visit(self, {}) -> T_Result:\n",
        params.join(", ")
    ));
    for m in &e.members {
        body.push_str(&format!(
            "        if self is {}.{}:\n            return {}()\n",
            e.name, m.name, m.visit_param
        ));
    }
    render(
        env,
        "file.py",
        &e.name,
        context! { header => HEADER, body => body },
    )
}

/// A literal enum value as Fern's snippet writer spells it: the wire value in
/// double quotes, with backslashes and double quotes escaped.
fn literal_enum_value(value: &str) -> String {
    format!("\"{}\"", value.replace('\\', "\\\\").replace('"', "\\\""))
}

/// Render a string enum as Fern does with `enum_type` unset (its `literals`
/// default): an open alias, `{Name} = typing.Union[typing.Literal["a", "b"],
/// typing.Any]`, that takes any value besides the listed ones, so a response
/// carrying a value the spec does not list still parses. Fern writes the alias
/// without the enum's description and imports nothing from `core`; ruff wraps a
/// long value list.
fn render_literal_enum(env: &Environment<'static>, e: &crate::ir::EnumType) -> Result<String> {
    let values: Vec<String> = e
        .members
        .iter()
        .map(|m| format!("\"{}\"", escape_py_str(&m.value)))
        .collect();
    let body = format!(
        "import typing\n\n{} = typing.Union[typing.Literal[{}], typing.Any]",
        e.name,
        values.join(", ")
    );
    render(
        env,
        "file.py",
        &e.name,
        context! { header => HEADER, body => body },
    )
}

/// Render a discriminated union to its module: a `{Union}_{Variant}` pydantic
/// model per variant (each with the discriminant as a `Literal` field), then the
/// `{Union} = typing.Union[..]` alias over them. Built directly (not via the
/// object template) because it carries a `from __future__ import annotations` and
/// several classes in one file. Each variant's body is rendered with the shared
/// `class_body.py` partial (same as a plain object).
fn render_discriminated_union(
    env: &Environment<'static>,
    union: &crate::ir::DiscriminatedUnion,
    extra: ExtraFields,
    loc: RefLoc,
    tag_types: &BTreeMap<String, String>,
    forward: &std::collections::HashSet<String>,
    repair: &ForwardRepair,
) -> Result<String> {
    let mut imports = Imports::at(loc, tag_types);
    // A recursive variant references the union by string forward reference
    // (issue #84); a same-module ref (the common case — the union alias lives in
    // this file) needs no import.
    imports.forward = forward.clone();
    imports.forward.extend(repair.names.iter().cloned());
    imports.cur_module = union.module.clone();
    imports.add_plain("typing");
    imports.add_plain("pydantic");
    if union.members.len() > 1 {
        imports.add_plain("typing_extensions");
    }
    imports.add_core("pydantic_utilities", "IS_PYDANTIC_V2");
    imports.add_core("pydantic_utilities", "UniversalBaseModel");

    // The `update_forward_refs` calls, resolved before the imports they need. Each
    // wrapper takes the module's cyclic names *minus the model it flattened* — a
    // wrapper cannot be asked to resolve its own source — so the calls differ from
    // one another, and the deferred imports follow the order the calls first name
    // them rather than a sort of the whole set.
    let repair_calls: Vec<(&str, Vec<String>)> = if forward.is_empty() && repair.triggers.is_empty()
    {
        Vec::new()
    } else {
        union
            .members
            .iter()
            .filter(|member| {
                member.fields.iter().any(|f| {
                    type_uses_forward(&f.type_ref, forward)
                        || type_uses_forward(&f.type_ref, &repair.triggers)
                })
            })
            .map(|member| {
                let names = repair
                    .member_names
                    .get(&member.class_name)
                    .cloned()
                    .unwrap_or_default();
                (member.class_name.as_str(), names)
            })
            .collect()
    };
    if repair_calls.is_empty() {
        for name in &repair.import_order {
            imports.add_type(name);
        }
    } else {
        let mut ordered: Vec<&str> = Vec::new();
        for name in repair_calls.iter().flat_map(|(_, names)| names) {
            if !ordered.contains(&name.as_str()) {
                ordered.push(name.as_str());
            }
        }
        for name in ordered {
            imports.add_type(name);
        }
    }

    let mut classes: Vec<String> = Vec::new();
    // The shared superclass, when the union came from a base object schema that
    // declares properties of its own. Fern spells it `Base` in every module that
    // needs one, and every wrapper below extends it instead of `UniversalBaseModel`.
    let base_class = if union.base_fields.is_empty() {
        "UniversalBaseModel"
    } else {
        let fields: Vec<FieldView> = union
            .base_fields
            .iter()
            .map(|f| render_field(f, &mut imports))
            .map(|rf| FieldView {
                decl: field_decl(&rf),
                docstring: rf.docstring,
            })
            .collect();
        let body = render_class_body(env, None, fields, Some(extra))?;
        classes.push(format!(
            "class Base(UniversalBaseModel):\n{}",
            body.trim_end()
        ));
        "Base"
    };
    for member in &union.members {
        // The discriminant field: `type: typing.Literal["circle"] = "circle"`. It is
        // an ordinary field bar its default, so a wire name that does not survive
        // Python naming carries the same alias metadata every other field would —
        // EN 18222's `objectType` tag renders as `object_type: Annotated[...,
        // FieldMetadata(alias="objectType"), pydantic.Field(alias="objectType")]`.
        let mut discriminant = render_field(
            &Field {
                wire_name: union.discriminant_property.clone(),
                py_name: discriminant_field_name(union),
                type_ref: TypeRef::Literal(vec![member.discriminant.clone()]),
                optional: false,
                nullable: false,
                spec_required: true,
                docstring: None,
                example: None,
                declared_name: None,
                deprecated: false,
                xml: None,
                admits_only_empty_object: false,
                inherited_from_flattened_parent: false,
                min_length: None,
                max_length: None,
            },
            &mut imports,
        );
        discriminant.default = format!(" = \"{}\"", member.discriminant);
        // A `value`-wrapped variant holds a payload rather than a flattened
        // object, so its `value` field is written ahead of the tag and its config
        // carries no `extra` member — the wrapper owns no open field set of its own.
        let mut rendered: Vec<RenderedField> = member
            .fields
            .iter()
            .map(|f| render_field(f, &mut imports))
            .collect();
        let tag_at = if member.wrapped { rendered.len() } else { 0 };
        rendered.insert(tag_at, discriminant);
        let fields: Vec<FieldView> = rendered
            .into_iter()
            .map(|rf| FieldView {
                decl: field_decl(&rf),
                docstring: rf.docstring,
            })
            .collect();
        let member_extra = (!member.wrapped).then_some(extra);
        let body = render_class_body(env, member.docstring.clone(), fields, member_extra)?;
        classes.push(format!(
            "class {}({base_class}):\n{}",
            member.class_name,
            body.trim_end()
        ));
    }

    let variants: Vec<Doc> = union
        .members
        .iter()
        .map(|m| Doc::atom(m.class_name.clone()))
        .collect();
    // Fern wraps a discriminated union's alias in
    // `typing_extensions.Annotated[Union[...], pydantic.Field(discriminator="…")]`
    // (issue #50 part 2) so pydantic can dispatch on the tag — a plain
    // `Union[...]` cannot. `ruff` wraps the line if it exceeds the width.
    let alias = if let [member] = union.members.as_slice() {
        format!("{} = {}", union.name, member.class_name)
    } else {
        let union_expr = Doc::group("typing.Union[", variants, "]").flat();
        format!(
            "{} = typing_extensions.Annotated[{union_expr}, pydantic.Field(discriminator=\"{}\")]",
            union.name,
            discriminant_field_name(union)
        )
    };

    // A recursive variant is repaired at load time: import `update_forward_refs`
    // and, right after the alias, call it on each wrapper that carries a forward
    // reference (issue #84). Non-recursive unions emit no such trailer.
    let mut trailer = String::new();
    if !forward.is_empty() || !repair.triggers.is_empty() {
        imports.add_core("pydantic_utilities", "update_forward_refs");
        for (class_name, names) in &repair_calls {
            let mut arguments = vec![(*class_name).to_string()];
            arguments.extend(names.iter().map(|name| format!("{name}={name}")));
            trailer.push('\n');
            trailer.push_str(&format!("update_forward_refs({})", arguments.join(", ")));
        }
    }

    // Registration order, not sorted: the union registers each name as its repair
    // calls first mention it, and Fern's block follows the calls. EN 18222's
    // `data_element.py` imports `MultiValuedDataElementValueItem` (named by the
    // first call) ahead of `MultiValuedDataElement` (named only by the second),
    // which no sort of the three puts in that order.
    let deferred = if imports.deferred_order.is_empty() {
        String::new()
    } else {
        format!("\n{}\n", imports.deferred_order.join("\n"))
    };

    Ok(format!(
        "{HEADER}\n\nfrom __future__ import annotations\n\n{}\n\n\n{}\n\n\n{alias}{deferred}{trailer}\n",
        imports.render(),
        classes.join("\n\n\n")
    ))
}

/// The Python name of a discriminated union's tag field: the one its
/// discriminator declares, else its wire key's. The hand-written
/// `telescope-renamed-discriminant` fixture renames `$class` to `targetClass`,
/// and Fern's wrappers declare `target_class` aliased to `$class`.
fn discriminant_field_name(union: &crate::ir::DiscriminatedUnion) -> String {
    naming::model_field_name(
        union
            .discriminant_name
            .as_deref()
            .unwrap_or(&union.discriminant_property),
    )
}

/// Whether a resolved type references any name that must render as a forward
/// reference — i.e. the enclosing model needs an `update_forward_refs` repair.
fn type_uses_forward(t: &TypeRef, forward: &std::collections::HashSet<String>) -> bool {
    let mut names = Vec::new();
    collect_named_refs(t, &mut names);
    names.iter().any(|n| forward.contains(n))
}

/// Render a resolved type to its flat Python expression for the raw client,
/// registering imports relative to the package root (named types live under
/// `..types`). Only the shapes reachable in an emittable endpoint are exercised;
/// the rest are rendered structurally for completeness.
fn raw_type_str(t: &TypeRef, imports: &mut Imports) -> String {
    raw_type_str_ctx(t, imports, false)
}

/// Like [`raw_type_str`], but in *request context* (`seq = true`) Fern renders
/// list/set collections as `typing.Sequence` rather than `typing.List`/`Set` —
/// request inputs accept any sequence. Response types use `seq = false`.
fn raw_type_str_ctx(t: &TypeRef, imports: &mut Imports, seq: bool) -> String {
    match t {
        TypeRef::Primitive(Prim::Str) => "str".to_string(),
        TypeRef::Primitive(Prim::Bytes) => "bytes".to_string(),
        TypeRef::Primitive(Prim::Int | Prim::Long) => "int".to_string(),
        TypeRef::Primitive(Prim::Float) => "float".to_string(),
        TypeRef::Primitive(Prim::Bool) => "bool".to_string(),
        TypeRef::Primitive(Prim::LiteralBool(value)) => {
            imports.add_plain("typing");
            format!("typing.Literal[{}]", if *value { "True" } else { "False" })
        }
        TypeRef::Primitive(Prim::Any) => {
            imports.add_plain("typing");
            "typing.Any".to_string()
        }
        TypeRef::Primitive(Prim::Datetime) => {
            imports.add_plain_as("datetime", "dt");
            "dt.datetime".to_string()
        }
        TypeRef::Primitive(Prim::Date) => {
            imports.add_plain_as("datetime", "dt");
            "dt.date".to_string()
        }
        TypeRef::Named(class) => imports.add_type(class),
        TypeRef::Optional(inner) => {
            imports.add_plain("typing");
            format!("typing.Optional[{}]", raw_type_str_ctx(inner, imports, seq))
        }
        TypeRef::List(inner) => {
            imports.add_plain("typing");
            let kw = if seq { "Sequence" } else { "List" };
            format!("typing.{kw}[{}]", raw_type_str_ctx(inner, imports, seq))
        }
        TypeRef::Set(inner) => {
            imports.add_plain("typing");
            let kw = if seq { "Sequence" } else { "Set" };
            format!("typing.{kw}[{}]", raw_type_str_ctx(inner, imports, seq))
        }
        TypeRef::Dict(k, v) => {
            imports.add_plain("typing");
            format!(
                "typing.Dict[{}, {}]",
                raw_type_str_ctx(k, imports, seq),
                raw_type_str_ctx(v, imports, seq)
            )
        }
        TypeRef::Union(variants) => {
            imports.add_plain("typing");
            let inner = variants
                .iter()
                .map(|v| raw_type_str_ctx(v, imports, seq))
                .collect::<Vec<_>>()
                .join(", ");
            format!("typing.Union[{inner}]")
        }
        TypeRef::Literal(values) => {
            imports.add_plain("typing");
            let inner = values
                .iter()
                .map(|v| format!("\"{v}\""))
                .collect::<Vec<_>>()
                .join(", ");
            format!("typing.Literal[{inner}]")
        }
    }
}

/// The request's URL argument: the document path with its leading slash stripped,
/// rendered as an f-string interpolating `encode_path_param(param)` for each path
/// placeholder, or a plain string literal when there are none.
///
/// An object-typed path parameter is interpolated through the same
/// `convert_and_respect_annotation_metadata` wrapper Fern gives an object-typed
/// *query* parameter, so its field aliases are respected on the way into the URL
/// segment. No registered source declares that shape; it is measured on
/// `docs/openapi-surface/probes/parameter-style-simple-path-object.yml`, whose
/// Fern tree interpolates
/// `encode_path_param(convert_and_respect_annotation_metadata(object_=probe_param,
/// annotation=ProbeParam, direction='write'))`. The quoting is single inside the
/// f-string, which is Fern's own rendering there.
fn url_arg(ep: &Endpoint, imports: &mut Imports) -> String {
    let stripped = ep.path.strip_prefix('/').unwrap_or(&ep.path);
    if stripped.contains('{') {
        let mut rendered = stripped.to_string();
        // A placeholder the document's base path lifts to the client reads the
        // value the client was constructed with.
        for lifted in &ep.client_path_params {
            rendered = rendered.replace(
                &format!("{{{}}}", lifted.wire_name),
                &format!(
                    "{{encode_path_param(self._client_wrapper._{})}}",
                    lifted.py_name
                ),
            );
        }
        for pp in &ep.path_params {
            let value = if pp.convert {
                imports.add_core("serialization", "convert_and_respect_annotation_metadata");
                let annotation = raw_type_str_ctx(&pp.type_ref, imports, true);
                format!(
                    "convert_and_respect_annotation_metadata(object_={}, annotation={annotation}, direction='write')",
                    pp.py_name
                )
            } else {
                pp.py_name.clone()
            };
            rendered = rendered.replace(
                &format!("{{{}}}", pp.wire_name),
                &format!("{{encode_path_param({value})}}"),
            );
        }
        format!("f\"{rendered}\"")
    } else {
        format!("\"{stripped}\"")
    }
}

/// The method's resolved parameters and response type, shared by the raw client
/// and the higher-level client so their signatures stay identical.
struct MethodParams {
    /// The response Python type (`None` when the endpoint returns no content).
    inner: String,
    /// Positional path parameters: `(py_name, annotation, description)`.
    path: Vec<(String, String, Option<String>)>,
    /// Keyword-only query parameters.
    query: Vec<DocParam>,
    /// Keyword-only header parameters.
    header: Vec<DocParam>,
    /// The request body, as one `request` argument or the hoisted inline fields.
    body: Vec<DocParam>,
}

/// Compute an endpoint's resolved parameters and response type, registering the
/// imports the annotations need. Independent of sync/async.
fn method_params(ep: &Endpoint, imports: &mut Imports) -> MethodParams {
    let inner = if ep.binary_response {
        "None".to_string()
    } else {
        ep.response
            .as_ref()
            .map_or_else(|| "None".to_string(), |t| raw_type_str(t, imports))
    };

    let path: Vec<(String, String, Option<String>)> = ep
        .path_params
        .iter()
        .map(|pp| {
            (
                pp.py_name.clone(),
                raw_type_str_ctx(&pp.type_ref, imports, true),
                pp.docstring.clone(),
            )
        })
        .collect();

    // Query and header parameters become keyword-only arguments; each carries an
    // annotation (`Optional[..]` unless required, `= None` when optional) and its
    // description.
    let optional_arg = |base: String, required: bool, description: Option<String>, name: String| {
        // A required parameter whose *type* is optional still defaults to `None`:
        // Fern's `optional<..>` decides the default, and the Parameter Object's
        // `required` only decides whether a worked call passes the argument.
        // oSPARC's `file_size` is `required: true` over an `anyOf` carrying a
        // `type: null` alternative, and its golden writes
        // `file_size: typing.Optional[UploadFileRequestFileSize] = None` and
        // still passes `file_size="file_size"` in the example.
        if required && !base.starts_with("typing.Optional[") {
            DocParam {
                name,
                annotation: base,
                default: None,
                description,
            }
        } else {
            // An argument whose own type is already optional — a `$ref` to a
            // nullable schema — is not wrapped twice.
            let annotation = if base.starts_with("typing.Optional[") {
                base
            } else {
                format!("typing.Optional[{base}]")
            };
            DocParam {
                name,
                annotation,
                default: Some("None".to_string()),
                description,
            }
        }
    };
    let query: Vec<DocParam> = ep
        .query_params
        .iter()
        .map(|qp| {
            // An array query parameter allows multiple values: Fern types it
            // `Optional[Union[T, Sequence[T]]]` and always defaults it to `None`.
            // A *nullable* array (`anyOf: [array, null]`, letta's `tags`) renders
            // the same way — the null alternative only supplies the `Optional`
            // this arm already writes.
            let array = match &qp.type_ref {
                TypeRef::List(inner) => Some((inner, qp.argument_required())),
                TypeRef::Optional(inner) => match inner.as_ref() {
                    TypeRef::List(item) => Some((item, false)),
                    _ => None,
                },
                _ => None,
            };
            if let Some((inner, required)) = array {
                if qp.allow_multiple {
                    // Nullable items lose their `Optional` here, as in Fern's
                    // signature; its `reference.md` keeps it (the
                    // `nullable-items-docs` departure).
                    let inner = match inner.as_ref() {
                        TypeRef::Optional(item) => item,
                        _ => inner,
                    };
                    let item = raw_type_str(inner, imports);
                    return DocParam {
                        name: qp.py_name.clone(),
                        annotation: format!(
                            "typing.Optional[typing.Union[{item}, typing.Sequence[{item}]]]"
                        ),
                        default: Some("None".to_string()),
                        description: qp.docstring.clone(),
                    };
                }
                return optional_arg(
                    format!("typing.Sequence[{}]", raw_type_str(inner, imports)),
                    required,
                    qp.docstring.clone(),
                    qp.py_name.clone(),
                );
            }
            let mut parameter = optional_arg(
                raw_type_str(&qp.type_ref, imports),
                qp.argument_required(),
                qp.docstring.clone(),
                qp.py_name.clone(),
            );
            if let Some(default) = &qp.default {
                parameter.default = Some(default.clone());
            }
            parameter
        })
        .collect();
    let header: Vec<DocParam> = ep
        .header_params
        .iter()
        .map(|header_param| {
            optional_arg(
                raw_type_str_ctx(&header_param.type_ref, imports, true),
                header_param.required,
                header_param.docstring.clone(),
                header_param.py_name.clone(),
            )
        })
        .collect();

    // The request body renders either as a single keyword-only `request` argument
    // or, for an inlined object, as one keyword-only argument per hoisted field
    // (`Optional[..] = OMIT` when optional). Collection types render in request
    // context (`typing.Sequence`).
    let body: Vec<DocParam> = match &ep.request_body {
        None => Vec::new(),
        Some(RequestBody::Single(s)) => {
            let base = raw_type_str_ctx(&s.type_ref, imports, true);
            vec![optional_arg(base, s.required, None, "request".to_string())]
        }
        Some(RequestBody::Inline(fields)) => {
            let mut params: Vec<DocParam> = fields
                .iter()
                // The field a stream condition fixes is not an argument: this
                // method's whole identity is the value it sends there.
                .filter(|f| {
                    ep.stream_condition
                        .as_ref()
                        .is_none_or(|(wire, _)| *wire != f.wire_name)
                })
                .map(|f| {
                    let base = raw_type_str_ctx(&f.type_ref, imports, true);
                    if f.optional {
                        DocParam {
                            name: f.py_name.clone(),
                            annotation: format!("typing.Optional[{base}]"),
                            default: Some("OMIT".to_string()),
                            description: f.docstring.clone(),
                        }
                    } else {
                        DocParam {
                            name: f.py_name.clone(),
                            annotation: base,
                            default: None,
                            description: f.docstring.clone(),
                        }
                    }
                })
                .collect();
            // Fern orders the inlined body's *signature* and docstring required-first
            // (optional `= OMIT` args last), a stable partition that preserves schema
            // order within each group. The `json={...}` dict keeps pure schema order
            // (built separately below), so only this view is reordered — matching how
            // a `readOnly`/`writeOnly` field lands after the required ones.
            params.sort_by_key(|p| p.default.is_some());
            params
        }
        Some(RequestBody::Bytes { .. }) => vec![DocParam {
            name: "request".to_string(),
            annotation: "typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]]"
                .to_string(),
            default: None,
            description: None,
        }],
        Some(RequestBody::Form(form)) => form
            .fields
            .iter()
            .map(|f| {
                // A file field is typed `core.File` (importing the `core` package)
                // and documented with Fern's fixed pointer to `core.File`.
                let base = if f.is_file {
                    imports.add_core_package();
                    if matches!(f.type_ref, TypeRef::List(_) | TypeRef::Set(_)) {
                        imports.add_plain("typing");
                        let container = if form.multipart { "List" } else { "Sequence" };
                        format!("typing.{container}[core.File]")
                    } else {
                        "core.File".to_string()
                    }
                } else {
                    raw_type_str_ctx(&f.type_ref, imports, !form.multipart)
                };
                let description = if f.is_file {
                    Some("See core.File for more documentation".to_string())
                } else {
                    f.docstring.clone()
                };
                if f.optional {
                    DocParam {
                        name: f.py_name.clone(),
                        annotation: format!("typing.Optional[{base}]"),
                        default: Some("OMIT".to_string()),
                        description,
                    }
                } else {
                    DocParam {
                        name: f.py_name.clone(),
                        annotation: base,
                        default: None,
                        description,
                    }
                }
            })
            .collect(),
    };
    // An idempotent operation's idempotency headers follow its body, optional
    // `str` arguments with no description (Fern's `dedupe_token`).
    let mut body = body;
    body.extend(ep.extensions.idempotency_headers.iter().map(|idempotency| {
        optional_arg(
            raw_type_str_ctx(&idempotency.type_ref, imports, true),
            idempotency.required,
            idempotency.docstring.clone(),
            idempotency.py_name.clone(),
        )
    }));

    MethodParams {
        inner,
        path,
        query,
        header,
        body,
    }
}

/// A keyword-only argument atom for a method signature: `name: annotation`, plus
/// ` = <default>` when the argument has a default.
fn doc_arg(dp: &DocParam) -> Doc {
    Doc::atom(match &dp.default {
        Some(d) => format!("{}: {} = {}", dp.name, dp.annotation, d),
        None => format!("{}: {}", dp.name, dp.annotation),
    })
}

/// The `def`/`async def` signature line (indent 4), laid out with ruff's
/// right-hand-split: `self`, positional path params, `*`, keyword-only query,
/// header, and body arguments, then `request_options`, closing with the return
/// type.
fn signature(ep: &Endpoint, mp: &MethodParams, return_type: &str, is_async: bool) -> String {
    let mut args: Vec<Doc> = vec![Doc::atom("self")];
    for (name, ty, _) in &mp.path {
        args.push(Doc::atom(format!("{name}: {ty}")));
    }
    args.push(Doc::atom("*"));
    args.extend(
        ordered_keyword_params(&mp.query, &mp.header, &mp.body)
            .into_iter()
            .map(doc_arg),
    );
    args.push(Doc::atom(
        "request_options: typing.Optional[RequestOptions] = None",
    ));
    let sig_open = format!(
        "    {}def {}(",
        if is_async { "async " } else { "" },
        ep.method_name
    );
    // Emit the signature flat; `ruff format` splits it across lines if it
    // overflows (its right-hand-split matches Fern's output).
    format!(
        "{} -> {return_type}:",
        Doc::group(sig_open, args, ")").flat()
    )
}

/// Build one raw-client method (sync or async), registering its imports.
fn raw_method(ep: &Endpoint, is_async: bool, imports: &mut Imports) -> String {
    imports.add_plain("typing");
    imports.add_core("request_options", "RequestOptions");

    if ep.streaming {
        return raw_stream_method(ep, is_async, imports);
    }
    if ep.binary_response {
        return raw_binary_stream_method(ep, is_async, imports);
    }

    let wrapper = if is_async {
        imports.core_local("http_response", "AsyncHttpResponse")
    } else {
        imports.core_local("http_response", "HttpResponse")
    };
    let mp = method_params(ep, imports);
    let return_type = pager_return_type(ep, &mp.inner, is_async, imports)
        .unwrap_or_else(|| format!("{wrapper}[{}]", mp.inner));
    let signature = signature(ep, &mp, &return_type, is_async);
    let docstring = raw_docstring(ep, &mp.path, &mp.query, &mp.header, &mp.body, &return_type);
    let body = raw_body(ep, is_async, &mp.inner, imports);
    format!("{signature}\n{docstring}\n{body}")
}

/// The success branch of a paginated raw method: parse the page, read its items
/// and next cursor, and return a pager whose `get_next` calls this same method
/// with the cursor advanced. The sync form binds a `lambda`; the async form needs
/// a nested `async def`, because a coroutine cannot be produced by a lambda.
fn pager_success_branch(
    ep: &Endpoint,
    pagination: &EndpointPagination,
    inner: &str,
    is_async: bool,
) -> Vec<String> {
    let pager = if is_async { "AsyncPager" } else { "SyncPager" };
    let mut lines = vec![
        "                _parsed_response = typing.cast(".to_string(),
        format!("                    {inner},"),
        "                    parse_obj_as(".to_string(),
        format!("                        type_={inner},"),
        "                        object_=_response.json(),".to_string(),
        "                    ),".to_string(),
        "                )".to_string(),
        format!(
            "                _items = _parsed_response.{}",
            pagination.results
        ),
    ];
    let (indent, next_value) = match &pagination.advance {
        PageAdvance::Cursor { container, leaf } => {
            // The cursor's container is optional on the response model, so the
            // whole advance is guarded on it; a cursor declared at the top level
            // needs no guard.
            // A guarded advance starts from no next page, which the guard then
            // overwrites; an unguarded one assigns both outright.
            let (indent, holder) = if container.is_empty() {
                ("                ", "_parsed_response".to_string())
            } else {
                let holder = format!("_parsed_response.{}", container.join("."));
                lines.push("                _has_next = False".to_string());
                lines.push("                _get_next = None".to_string());
                lines.push(format!("                if {holder} is not None:"));
                ("                    ", holder)
            };
            lines.push(format!("{indent}_parsed_next = {holder}.{leaf}"));
            lines.push(format!(
                "{indent}_has_next = _parsed_next is not None and _parsed_next != \"\""
            ));
            (indent, "_parsed_next".to_string())
        }
        PageAdvance::Offset => {
            // The offset form reads no cursor: a page with items has a next one,
            // one offset on (`x-fern-pagination: {offset: …}`).
            lines.push("                _has_next = len(_items or []) > 0".to_string());
            (
                "                ",
                format!("{} + 1", pagination.cursor_param),
            )
        }
    };
    // The recursive call: every argument the method took, with the cursor replaced
    // by the one just parsed, or the offset by the next one.
    let mut args: Vec<String> = Vec::new();
    for param in &ep.path_params {
        args.push(param.py_name.clone());
    }
    for param in &ep.query_params {
        let value = if param.py_name == pagination.cursor_param {
            next_value.as_str()
        } else {
            param.py_name.as_str()
        };
        args.push(format!("{}={value}", param.py_name));
    }
    for param in &ep.header_params {
        args.push(format!("{}={}", param.py_name, param.py_name));
    }
    args.push("request_options=request_options".to_string());
    if is_async {
        lines.push(String::new());
        lines.push(format!("{indent}async def _get_next():"));
        lines.push(format!("{indent}    return await self.{}(", ep.method_name));
        for arg in &args {
            lines.push(format!("{indent}        {arg},"));
        }
        lines.push(format!("{indent}    )"));
        lines.push(String::new());
    } else {
        lines.push(format!(
            "{indent}_get_next = lambda: self.{}(",
            ep.method_name
        ));
        for arg in &args {
            lines.push(format!("{indent}    {arg},"));
        }
        lines.push(format!("{indent})"));
    }
    lines.push(format!(
        "                return {pager}(has_next=_has_next, items=_items, get_next=_get_next, response=_parsed_response)"
    ));
    lines
}

/// The pager type as written in a docstring, where no import is registered — the
/// signature that names it has already registered one.
fn pager_doc_type(pagination: &EndpointPagination, inner: &str, is_async: bool) -> String {
    let pager = if is_async { "AsyncPager" } else { "SyncPager" };
    let item = match &pagination.item_type {
        TypeRef::Named(item) => item.clone(),
        // A scalar names no import, so a scratch registry renders it.
        primitive @ TypeRef::Primitive(_) => raw_type_str(primitive, &mut Imports::default()),
        _ => return inner.to_string(),
    };
    format!("{pager}[{item}, {inner}]")
}

/// The `SyncPager[Item, Response]` / `AsyncPager[Item, Response]` a paginated
/// operation returns in place of the buffered `HttpResponse[Response]`, or `None`
/// when the operation declares no pagination contract.
fn pager_return_type(
    ep: &Endpoint,
    inner: &str,
    is_async: bool,
    imports: &mut Imports,
) -> Option<String> {
    let pagination = ep.pagination.as_ref()?;
    let pager = if is_async { "AsyncPager" } else { "SyncPager" };
    imports.add_core("pagination", pager);
    let item = raw_type_str(&pagination.item_type, imports);
    Some(format!("{pager}[{item}, {inner}]"))
}

/// A parameter as it appears in a method docstring / signature: its Python name,
/// the annotation (the type shown in the signature and docstring), an optional
/// `= <default>` RHS, and an optional description.
struct DocParam {
    name: String,
    annotation: String,
    default: Option<String>,
    description: Option<String>,
}

/// The endpoint's keyword parameters in Fern's declaration order: every *required*
/// argument (no default) first, then the optional ones, each group preserving the
/// query → header → body grouping. Fern lists a required argument (e.g. a mandatory
/// body field) ahead of an optional query parameter, rather than grouping strictly
/// by kind.
fn ordered_keyword_params<'a>(
    query: &'a [DocParam],
    header: &'a [DocParam],
    body: &'a [DocParam],
) -> Vec<&'a DocParam> {
    let (required, optional): (Vec<&DocParam>, Vec<&DocParam>) = query
        .iter()
        .chain(header)
        .chain(body)
        .partition(|dp| dp.default.is_none());
    required.into_iter().chain(optional).collect()
}

/// The method docstring (indent 8): an optional summary line, a `Parameters`
/// section (path params, query params, the request body, then `request_options`),
/// and a `Returns` section.
fn raw_docstring(
    ep: &Endpoint,
    param_types: &[(String, String, Option<String>)],
    query_params: &[DocParam],
    header_params: &[DocParam],
    body_params: &[DocParam],
    return_type: &str,
) -> String {
    let mut lines: Vec<String> = vec!["        \"\"\"".to_string()];
    if let Some(summary) = &ep.docstring {
        // A multi-line summary carries the 8-space docstring indent on every line.
        for line in summary.split('\n') {
            lines.push(format!("        {}", python_doc_line(line)));
        }
        lines.push(String::new());
    }
    lines.push("        Parameters".to_string());
    lines.push("        ----------".to_string());
    for (name, ty, desc) in param_types {
        push_path_param(&mut lines, name, ty, desc);
    }
    // Query params, header params, then the request body/fields, each: a
    // `name : annotation` line, an optional indented description, and a trailing
    // blank line.
    let mut push_param = |dp: &DocParam| {
        lines.push(format!("        {} : {}", dp.name, dp.annotation));
        if let Some(desc) = &dp.description {
            push_param_doc(&mut lines, desc);
        }
        lines.push(String::new());
    };
    for dp in ordered_keyword_params(query_params, header_params, body_params) {
        push_param(dp);
    }
    lines.push("        request_options : typing.Optional[RequestOptions]".to_string());
    lines.push("            Request-specific configuration.".to_string());
    lines.push(String::new());
    lines.push("        Returns".to_string());
    lines.push("        -------".to_string());
    lines.push(format!("        {return_type}"));
    // A concrete response type is followed by the response description (indented,
    // blank when the spec gives none) before the closing quotes; a `None` return
    // closes immediately.
    if ep.response.is_some() {
        push_return_doc(&mut lines, ep.response_doc.as_deref());
    }
    lines.push("        \"\"\"".to_string());
    if ep.module.is_empty() && ep.openapi_31 {
        lines
            .iter_mut()
            .filter(|line| line.is_empty())
            .for_each(|line| {
                *line = "        ".to_string();
            });
    }
    lines.join("\n")
}

/// Render one path parameter's docstring block: its `name : ty` line, then the
/// description slot, then a trailing blank line. Fern fills the slot when the spec
/// gives a non-empty description, emits a *blank* slot when it declares an empty one
/// (`Some("")`, e.g. bunq's path params — two blank lines total), and omits the slot
/// entirely when it declares none (`None` — one blank line).
/// See [`crate::ir`]'s `path_param_doc`.
fn push_path_param(lines: &mut Vec<String>, name: &str, ty: &str, desc: &Option<String>) {
    lines.push(format!("        {name} : {ty}"));
    match desc {
        Some(d) if !d.is_empty() => push_param_doc(lines, d),
        Some(_) => lines.push(String::new()),
        None => {}
    }
    lines.push(String::new());
}

fn push_param_doc(lines: &mut Vec<String>, description: &str) {
    lines.extend(description.split('\n').map(|line| {
        format!(
            "            {}",
            python_doc_line(&line.replace('\t', "    "))
        )
    }));
}

/// One line of Python docstring prose. A backslash is escaped for the source
/// literal, and a tab is expanded to four spaces: `ruff format` normalizes
/// docstring indentation, so SteamInputDB's tab-indented second description line
/// reaches its golden's `client.py` as spaces while `reference.md` — which no
/// formatter touches — keeps the tabs the document wrote.
fn python_doc_line(line: &str) -> String {
    line.replace('\\', "\\\\").replace('\t', "    ")
}

/// Push the `Returns` description under the return type: each line of the
/// response description indented 12 spaces, a single blank line when the spec
/// declares a blank one, and nothing when the response has no `description` key
/// at all — matching Fern.
fn push_return_doc(lines: &mut Vec<String>, response_doc: Option<&str>) {
    match response_doc {
        Some("") => lines.push(String::new()),
        Some(doc) => lines.extend(
            doc.split('\n')
                .map(|line| format!("            {}", python_doc_line(line))),
        ),
        None => {}
    }
}

/// The method body (indent 8): the `httpx_client.request(...)` call and the
/// response-handling block.
fn raw_body(ep: &Endpoint, is_async: bool, inner: &str, imports: &mut Imports) -> String {
    imports.add_plain("typing");
    imports.add_from("json.decoder", "JSONDecodeError");
    let api_error = imports.add_core_local("api_error", "ApiError");
    if !ep.path_params.is_empty() || !ep.client_path_params.is_empty() {
        imports.add_core("jsonable_encoder", "encode_path_param");
    }
    imports.add_core("parse_error", "ParsingError");
    imports.add_from("pydantic", "ValidationError");

    let await_ = if is_async { "await " } else { "" };
    let wrapper = if is_async {
        imports.core_local("http_response", "AsyncHttpResponse")
    } else {
        imports.core_local("http_response", "HttpResponse")
    };
    let mut lines: Vec<String> = offset_default_preamble(ep);
    lines.extend(retries_disabled_preamble(ep));
    lines.push(format!(
        "        _response = {await_}self._client_wrapper.httpx_client.request("
    ));
    if ep.path != "/" {
        lines.push(format!("            {},", url_arg(ep, imports)));
    }
    lines.extend(environment_base_url(ep));
    lines.push(format!("            method=\"{}\",", ep.http_method));
    append_request_call_args(&mut lines, ep, imports);
    lines.extend(["        )".to_string(), "        try:".to_string()]);
    if matches!(ep.response, Some(TypeRef::Optional(_)))
        || ep.response_may_be_empty
            && (matches!(ep.response, Some(TypeRef::Primitive(Prim::Any)))
                || ep.response_names_unknown)
    {
        lines.extend([
            "            if _response is None or not _response.text.strip():".to_string(),
            format!("                return {wrapper}(response=_response, data=None)"),
        ]);
    }
    lines.push("            if 200 <= _response.status_code < 300:".to_string());
    if ep.text_response {
        lines.push(format!(
            "                return {wrapper}(response=_response, data=_response.text)"
        ));
    } else if let Some(pagination) = ep.pagination.as_ref() {
        imports.add_core("pydantic_utilities", "parse_obj_as");
        lines.extend(pager_success_branch(ep, pagination, inner, is_async));
    } else if ep.response.is_some() {
        imports.add_core("pydantic_utilities", "parse_obj_as");
        lines.extend([
            "                _data = typing.cast(".to_string(),
            format!("                    {inner},"),
            "                    parse_obj_as(".to_string(),
            format!("                        type_={inner},"),
            "                        object_=_response.json(),".to_string(),
            "                    ),".to_string(),
            "                )".to_string(),
            format!("                return {wrapper}(response=_response, data=_data)"),
        ]);
    } else {
        lines.push(format!(
            "                return {wrapper}(response=_response, data=None)"
        ));
    }
    // One `raise <Error>` branch per declared error response, keyed on its status
    // code, parsing the error body into the generated exception.
    for err in &ep.errors {
        imports.add_core("pydantic_utilities", "parse_obj_as");
        let module = naming::module_name(&err.class_name);
        imports.add_from(&imports.error_import_path(&module), &err.class_name);
        let body = raw_type_str(&err.body_type, imports);
        lines.extend(error_raise_branch(err, &body, "            "));
    }
    lines.extend([
        "            _response_json = _response.json()".to_string(),
        "        except JSONDecodeError:".to_string(),
        format!("            raise {api_error}(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)"),
    ]);
    lines.extend([
        "        except ValidationError as e:".to_string(),
        "            raise ParsingError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e)".to_string(),
    ]);
    lines.push(format!("        raise {api_error}(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)"));
    lines.join("\n")
}

/// A request's `base_url` argument in a multi-URL environment: the URL of the
/// environment field the operation reads (`get_environment().archive`).
fn environment_base_url(ep: &Endpoint) -> Option<String> {
    ep.extensions.environment_field.as_ref().map(|field| {
        format!("            base_url=self._client_wrapper.get_environment().{field},")
    })
}

/// An offset-paginated operation's first statement: its offset argument
/// defaulted to the first page (`page = page if page is not None else 1`), as
/// Fern writes it, then a blank line.
fn offset_default_preamble(ep: &Endpoint) -> Vec<String> {
    match &ep.pagination {
        Some(pagination) if matches!(pagination.advance, PageAdvance::Offset) => vec![
            format!(
                "        {0} = {0} if {0} is not None else 1",
                pagination.cursor_param
            ),
            String::new(),
        ],
        _ => Vec::new(),
    }
}

/// The request options an operation with `x-fern-retries: {disabled: true}`
/// sends in place of the caller's: theirs with `max_retries` set to 0, written
/// ahead of the request call; nothing for any other operation.
fn retries_disabled_preamble(ep: &Endpoint) -> Vec<String> {
    if !ep.extensions.retries_disabled {
        return Vec::new();
    }
    vec![
        "        _request_options_with_retries_disabled: typing.Optional[RequestOptions] = (".to_string(),
        "            {**request_options, \"max_retries\": 0} if request_options is not None else {\"max_retries\": 0}".to_string(),
        "        )".to_string(),
    ]
}

/// Append an endpoint's httpx call arguments (indent 12) to `lines` — the query
/// `params` dict, the serialized request body, the `headers` dict,
/// `request_options`, and the `omit`/`force_multipart` sentinels. Shared by the
/// buffered `.request(...)` path ([`raw_body`]) and the streaming `.stream(...)`
/// path ([`raw_stream_body`]) so both serialize a body identically.
fn append_request_call_args(lines: &mut Vec<String>, ep: &Endpoint, imports: &mut Imports) {
    // Query parameters map wire name to the Python argument in a `params` dict. An
    // object/union-typed parameter serializes through the convert wrapper (Fern
    // respects its field aliases), exactly as an inlined body field does.
    if !ep.query_params.is_empty() {
        lines.push("            params={".to_string());
        for qp in &ep.query_params {
            if qp.comma_separated {
                lines.push(format!(
                    "                \"{}\": \",\".join(map(str, {})) if isinstance({}, (list, tuple, set)) else {},",
                    qp.wire_name, qp.py_name, qp.py_name, qp.py_name
                ));
            } else if qp.convert {
                imports.add_core("serialization", "convert_and_respect_annotation_metadata");
                // Fern converts collections per element: a query
                // `Sequence[Model]` passes `annotation=Model`, while a direct
                // object parameter passes its model unchanged.
                let annotation_type = match &qp.type_ref {
                    TypeRef::List(inner) | TypeRef::Set(inner) => inner.as_ref(),
                    other => other,
                };
                let annotation = raw_type_str_ctx(annotation_type, imports, true);
                let call = Doc::group(
                    format!(
                        "                \"{}\": convert_and_respect_annotation_metadata(",
                        qp.wire_name
                    ),
                    vec![
                        Doc::atom(format!("object_={}", qp.py_name)),
                        Doc::atom(format!("annotation={annotation}")),
                        Doc::atom("direction=\"write\""),
                    ],
                    ")",
                );
                lines.push(format!("{},", call.flat()));
            } else if type_serializes_as(&qp.type_ref, Prim::Date)
                || qp.aliased_datetime == Some(Prim::Date)
            {
                let value = if qp.argument_required() {
                    format!("str({})", qp.py_name)
                } else {
                    format!(
                        "str({}) if {} is not None else None",
                        qp.py_name, qp.py_name
                    )
                };
                lines.push(format!("                \"{}\": {value},", qp.wire_name));
            } else if type_serializes_as(&qp.type_ref, Prim::Datetime)
                || qp.aliased_datetime == Some(Prim::Datetime)
            {
                imports.add_core("datetime_utils", "serialize_datetime");
                let value = if qp.argument_required() {
                    format!("serialize_datetime({})", qp.py_name)
                } else {
                    format!(
                        "serialize_datetime({}) if {} is not None else None",
                        qp.py_name, qp.py_name
                    )
                };
                lines.push(format!("                \"{}\": {value},", qp.wire_name));
            } else {
                lines.push(format!(
                    "                \"{}\": {},",
                    qp.wire_name, qp.py_name
                ));
            }
        }
        lines.push("            },".to_string());
    }
    // The request body serializes as `json=request`, through the convert wrapper
    // for types carrying field aliases, or — for an inlined object — as a
    // `json={...}` dict mapping each field's wire name to its argument (each value
    // itself convert-wrapped when the field carries aliases).
    match &ep.request_body {
        None => {}
        Some(RequestBody::Single(s)) if s.convert => {
            imports.add_core("serialization", "convert_and_respect_annotation_metadata");
            let annotation_type = if s.required {
                s.type_ref.clone()
            } else {
                TypeRef::Optional(Box::new(s.type_ref.clone()))
            };
            let annotation = raw_type_str_ctx(&annotation_type, imports, true);
            // A urlencoded single body is sent as form data.
            let key = if s.content_type_override.as_deref()
                == Some("application/x-www-form-urlencoded")
            {
                "data"
            } else {
                "json"
            };
            let call = Doc::group(
                format!("            {key}=convert_and_respect_annotation_metadata("),
                vec![
                    Doc::atom("object_=request"),
                    Doc::atom(format!("annotation={annotation}")),
                    Doc::atom("direction=\"write\""),
                ],
                ")",
            );
            lines.push(format!("{},", call.flat()));
        }
        Some(RequestBody::Single(_)) => lines.push("            json=request,".to_string()),
        Some(RequestBody::Inline(fields)) => {
            lines.push("            json={".to_string());
            for f in fields {
                // The field a stream condition fixes is written last, whatever
                // position the schema declares it in.
                if ep
                    .stream_condition
                    .as_ref()
                    .is_some_and(|(wire, _)| *wire == f.wire_name)
                {
                    continue;
                }
                let value_name = body_field_value_name(f, &ep.query_params);
                if f.convert {
                    imports.add_core("serialization", "convert_and_respect_annotation_metadata");
                    let annotation_type = if f.nullable {
                        TypeRef::Optional(Box::new(f.type_ref.clone()))
                    } else {
                        f.type_ref.clone()
                    };
                    let annotation = raw_type_str_ctx(&annotation_type, imports, true);
                    let call = Doc::group(
                        format!(
                            "                \"{}\": convert_and_respect_annotation_metadata(",
                            f.wire_name
                        ),
                        vec![
                            Doc::atom(format!("object_={value_name}")),
                            Doc::atom(format!("annotation={annotation}")),
                            Doc::atom("direction=\"write\""),
                        ],
                        ")",
                    );
                    lines.push(format!("{},", call.flat()));
                } else {
                    lines.push(format!(
                        "                \"{}\": {value_name},",
                        f.wire_name
                    ));
                }
            }
            if let Some((wire, value)) = ep.stream_condition.as_ref() {
                let literal = if *value { "True" } else { "False" };
                lines.push(format!("                \"{wire}\": {literal},"));
            }
            lines.push("            },".to_string());
        }
        // A raw bytes body is streamed via `content=` rather than serialized.
        Some(RequestBody::Bytes { .. }) => lines.push("            content=request,".to_string()),
        // A form body: non-file fields into `data={...}`, file fields into
        // `files={...}` (in document order within each group).
        Some(RequestBody::Form(form)) => {
            let mut data = String::new();
            let mut files = String::new();
            let json_shadowed = form.fields.iter().any(|field| field.py_name == "json")
                || ep.path_params.iter().any(|param| param.py_name == "json")
                || ep.query_params.iter().any(|param| param.py_name == "json")
                || ep.header_params.iter().any(|param| param.py_name == "json");
            for f in &form.fields {
                if let Some(content_type) = &f.form_content_type {
                    if f.form_json {
                        imports.add_core("jsonable_encoder", "jsonable_encoder");
                        let array_part = matches!(&f.type_ref, TypeRef::List(_))
                            || matches!(&f.type_ref, TypeRef::Optional(inner) if matches!(inner.as_ref(), TypeRef::List(_)));
                        let encoded = if array_part && !is_json_like_media_type(content_type) {
                            format!("jsonable_encoder({})", f.py_name)
                        } else {
                            let module = imports.json_module(json_shadowed);
                            format!("{module}.dumps(jsonable_encoder({}))", f.py_name)
                        };
                        let tuple = format!("(None, {encoded}, \"{content_type}\")");
                        if f.optional {
                            files.push_str(&format!(
                                "                **(\n                    {{\"{}\": {tuple}}}\n                    if {} is not OMIT\n                    else {{}}\n                ),\n",
                                f.wire_name, f.py_name
                            ));
                        } else {
                            files.push_str(&format!(
                                "                \"{}\": {tuple},\n",
                                f.wire_name
                            ));
                        }
                    } else {
                        let value = format!(
                            "core.with_content_type(file={}, default_content_type=\"{content_type}\")",
                            f.py_name
                        );
                        if f.optional {
                            files.push_str(&format!(
                                "                **(\n                    {{\n                        \"{}\": {value}\n                    }}\n                    if {} is not None\n                    else {{}}\n                ),\n",
                                f.wire_name, f.py_name
                            ));
                        } else {
                            files.push_str(&format!(
                                "                \"{}\": {value},\n",
                                f.wire_name
                            ));
                        }
                    }
                    continue;
                }
                let repeated =
                    form.multipart && !f.is_file && !f.form_json && is_list_type(&f.type_ref);
                let value = if f.form_json {
                    let module = imports.json_module(json_shadowed);
                    imports.add_core("jsonable_encoder", "jsonable_encoder");
                    let encoded = format!("{module}.dumps(jsonable_encoder({}))", f.py_name);
                    if f.optional {
                        format!("{encoded} if {} is not OMIT else OMIT", f.py_name)
                    } else {
                        encoded
                    }
                } else if repeated {
                    // A list of form primitives (the IR leaves it out of
                    // `form_json`) is passed through `jsonable_encoder` alone, so
                    // httpx repeats the field per item.
                    imports.add_core("jsonable_encoder", "jsonable_encoder");
                    let encoded = format!("jsonable_encoder({})", f.py_name);
                    if f.optional {
                        format!("{encoded} if {} is not OMIT else OMIT", f.py_name)
                    } else {
                        encoded
                    }
                } else {
                    f.py_name.clone()
                };
                // A form field whose type is a named model serializes through the
                // annotation converter, exactly as the same field would in a JSON
                // body (`mosip-esignet`'s urlencoded `claims`, a `$ref` to `Claim`).
                let entry = if f.convert && !f.is_file && !f.form_json && !repeated {
                    imports.add_core("serialization", "convert_and_respect_annotation_metadata");
                    let annotation_type = if f.nullable {
                        TypeRef::Optional(Box::new(f.type_ref.clone()))
                    } else {
                        f.type_ref.clone()
                    };
                    let annotation = raw_type_str_ctx(&annotation_type, imports, true);
                    let call = Doc::group(
                        format!(
                            "                \"{}\": convert_and_respect_annotation_metadata(",
                            f.wire_name
                        ),
                        vec![
                            Doc::atom(format!("object_={value}")),
                            Doc::atom(format!("annotation={annotation}")),
                            Doc::atom("direction=\"write\""),
                        ],
                        ")",
                    );
                    format!("{},\n", call.flat())
                } else if f.is_file && f.optional {
                    // An optional file field is spread in conditionally rather
                    // than passed as a `None` part: DaniWeb's `csv` is the
                    // corpus's first multipart file property outside `required`.
                    format!(
                        "                **({{\"{}\": {value}}} if {value} is not None else {{}}),\n",
                        f.wire_name
                    )
                } else {
                    format!("                \"{}\": {value},\n", f.wire_name)
                };
                if f.is_file {
                    files.push_str(&entry);
                } else {
                    data.push_str(&entry);
                }
            }
            if !data.is_empty() {
                lines.push(format!("            data={{\n{data}            }},"));
            } else if !files.is_empty() {
                lines.push("            data={},".to_string());
            }
            if !files.is_empty() {
                lines.push(format!("            files={{\n{files}            }},"));
            } else if form.multipart {
                lines.push("            files={},".to_string());
            }
        }
    }

    fn is_list_type(t: &TypeRef) -> bool {
        match t {
            TypeRef::List(_) | TypeRef::Set(_) => true,
            TypeRef::Optional(inner) => is_list_type(inner),
            _ => false,
        }
    }
    fn type_serializes_as(t: &TypeRef, primitive: Prim) -> bool {
        match t {
            TypeRef::Primitive(value) => *value == primitive,
            TypeRef::Optional(inner) => type_serializes_as(inner, primitive),
            _ => false,
        }
    }
    // The `headers` dict carries a `content-type`: `application/octet-stream` for a
    // raw bytes body, else `application/json` when the body intrinsically needs it
    // or is accompanied by path/header parameters. Header parameters follow,
    // rendered as `str(x) if x is not None else None`.
    // A *resource envelope*: a documented body schema that is mostly `readOnly`
    // and declares exactly one required member — the k8s Container Service
    // Provider's `Container`, six of whose seven properties are `readOnly` around
    // the single required `spec`. Fern keeps the explicit `content-type` for that
    // shape, so it escapes both drops an ordinary echo body takes: the
    // response-heavy one below, which applies only to an *undocumented* schema
    // (apideck's `add`, netbox's `groups_create`), and the request/response
    // same-`$ref` one, which drops the header for Airflow's `post_pool` (no
    // required member) and the Petstore's `place_order` (not response-heavy).
    let resource_envelope = ep.body_schema_is_response_heavy
        && ep.body_schema_documented
        && matches!(&ep.request_body, Some(RequestBody::Inline(fields))
            if fields.iter().filter(|field| field.spec_required).count() == 1);
    let content_type: Option<String> = match &ep.request_body {
        Some(_) if ep.body_content_type_override.is_some() => ep.body_content_type_override.clone(),
        Some(RequestBody::Bytes { content_type }) => Some(content_type.clone()),
        // A multipart form uses `force_multipart=True` (no content-type header);
        // a urlencoded form carries its own content-type.
        Some(RequestBody::Form(form)) => {
            (!form.multipart).then_some("application/x-www-form-urlencoded".to_string())
        }
        Some(RequestBody::Single(body))
            if body.content_type_override.is_some()
                && body.content_type_override.as_deref() != Some("*/*") =>
        {
            body.content_type_override.clone()
        }
        // A JSON body carries the `content-type` header when it accompanies a
        // path/header param, is *undocumented*, or is *always sent whole* (every field
        // required, so no `OMIT`). Fern drops the explicit header only for a
        // documented body with at least one optional field — which may serialize to
        // nothing — or a legacy codegen-named body that rides no header-forcing
        // param, leaving the content-type to httpx. An all-required body
        // (`CREATE_SessionServer`) keeps it. A `components.requestBodies` body drops
        // it only while its schema survives in the public type layer; one whose
        // schema exists solely to be flattened here (exa-gate's `KeyBatch`) keeps it.
        // The security scheme does not enter into it: measured at fern-python-sdk
        // 5.41.3, a Basic-auth operation's titled `$ref` body keeps the header (mi_music's
        // `DidVolume` and fourteen more), and otoroshi's untitled, surviving
        // `Group` loses it to the surviving-schema drop below, not to its scheme.
        // Nor does an `allOf` or an empty `requestBody.description`: a single-use
        // `allOf` body is a dropped schema like any other, and an empty description
        // is a description. A documented, response-heavy body with one required member — a resource
        // envelope — keeps the header through both of those drops; see
        // `resource_envelope` above.
        Some(body)
            if !body.is_wildcard_media()
                && !ep.body_collapses_to_type_reference
                && (ep.body_media_has_example
                    && ep.body_schema_dropped
                    && ep.body_schema_shape == BodySchemaShape::Ref
                    || ep.reference_body_example.is_some()
                        // Inline named examples keep their header. A surviving
                        // referenced schema still follows the schema drop below.
                        && (ep.body_schema_shape != BodySchemaShape::Ref || ep.body_schema_dropped)
                        && !ep.body_schema_is_success_response
                        && matches!(body, RequestBody::Inline(_))
                    || !ep.header_params.is_empty()
                    || !ep.path_params.is_empty()
                    || !ep.query_params.is_empty()
                    || (body.content_type_header()
                        // A body schema more than one operation posts, which is
                        // neither `required: true` nor sent whole, drops the
                        // header two ways. It drops it when the body says nothing
                        // about itself anywhere — no `description` on the
                        // `requestBody` and none on the schema: the Open
                        // Integration Hub's `MutableSecret` and `MutableAuthClient`
                        // are each the body of a create and an update. And it drops
                        // it when the `requestBody` *is* described, the operation
                        // sends nothing but that body, and the body declares
                        // required members — some of them, since all of them is the
                        // sent-whole case above: braintrust's `CreateView`,
                        // `AclItem` and twelve more are each the body of two
                        // argument-free operations under a described `requestBody`,
                        // and a description on the *schema* does not save
                        // `AclItem`. Both halves of that second drop are load
                        // bearing across the corpus. A query parameter beside the
                        // body keeps the header — Palo Alto's `IkeCryptoProfiles`
                        // and `IpsecCryptoProfiles` are shared, described and
                        // partly required, and each rides a `SubTenantName` — and
                        // so does a body with no required member at all, which is
                        // what leaves the echo `Message` of the same-`$ref` case
                        // (one optional field) its header. Every other way to be
                        // shared keeps it:
                        // bodies declaring `required: true` or sent whole retain
                        // the header. Adyen's `GrantInfo` is posted by one operation.
                        && !(ep.body_schema_shared
                            && !ep.body_declared_required
                            && !body.all_fields_required()
                            && !ep.body_schema_dropped
                            && (ep.body_description_missing && !ep.body_schema_documented
                                || !ep.body_description_missing
                                    && ep.query_params.is_empty()
                                    && matches!(body, RequestBody::Inline(fields)
                                        if fields.iter().any(|field| field.spec_required))))
                        && !ep.body_codegen_named
                        // An inline union body that neither names itself nor
                        // declares a discriminator drops the header, however it is
                        // documented and whatever its auth: the Vonage Messages
                        // API's described, required `sendMessage` `oneOf`,
                        // Flowdapt's `create_config` and Otoroshi's
                        // `create_global_auth_module` all leave the content type to
                        // httpx, where Letta's titled `anyOf` (`add_mcp_server`)
                        // and its discriminated `createTemplateNoProject` keep it.
                        && ep.body_schema_shape != BodySchemaShape::InlinePlainUnion
                        // …and so does one whose schema the `requestBodies` entry
                        // writes inline, which names no type at all: the Vonage
                        // Conversation API's `Conversation` body.
                        && (!ep.body_component_ref
                            || ep.body_schema_dropped
                            || ep.body_schema_shape != BodySchemaShape::Ref)
                        // A referenced request schema that SURVIVES in the public
                        // type layer — Fern kept the model because something else
                        // (a response, another body) needs it — is a documented
                        // type the request merely flattens, and Fern leaves its
                        // content type to httpx. AGCO posts 28 such bodies
                        // (`UpdateSystem.Models.Bundle`, `BuildSystem.Shared.DTO.Job`,
                        // …), every one of them without the header, against the
                        // seven whose schema Fern dropped (`API.Models.Credentials`,
                        // `DealerDB.Models.LicenseActivationCreate`, …), every one
                        // of them with it. A query parameter beside the body keeps
                        // it either way — AGCO's `POST /api/v2/GlobalImages` rides
                        // an `overridePublisherOrDate` and is the document's one
                        // surviving-schema body that carries the header — which is
                        // the same query-parameter exception the shared-body drop
                        // above already records for Palo Alto's crypto profiles.
                        // A schema declaring its own `title` escapes the drop.
                        // The `title` is what Fern's importer names the request
                        // model from, and every measured surviving-schema body
                        // splits on it: SFTPGo's `Admin`, `APIKey`, `Role`,
                        // `Share`, `AdminProfile` and `UserProfile`,
                        // Audiobookshelf's `EmailSettings` and `Podcast`, LORIS's
                        // `QueryObject` and all 28 of AGCO's are untitled and lose
                        // the header, while letta's titled `CreateBlock`/`BlockUpdate`
                        // carry one and keep it. A
                        // body offering SEVERAL media types drops it either way,
                        // which is what AGCO measured first and what no titled
                        // source has since contradicted. A `stream-condition`
                        // request escapes the drop too: Fern augments that body
                        // with the condition property itself (`"stream": True`),
                        // so it is no longer just the referenced schema.
                        // A documented resource envelope escapes this drop too,
                        // exactly as it escapes the same-`$ref` one below:
                        // corpus row 113's `Container` is untitled, and while its
                        // own `createContainer` also rides an `id` query parameter,
                        // the shape that measurement pinned is the envelope.
                        // Every measured witness is a FLATTENED object body, so
                        // the drop is scoped to one: a `$ref` to a scalar alias
                        // (an integer enum, a `format: uri` string) rides as a
                        // single `request` argument and keeps its header.
                        && !(matches!(body, RequestBody::Inline(_))
                            && ep.body_schema_shape == BodySchemaShape::Ref
                            && !ep.body_schema_dropped
                            && ep.query_params.is_empty()
                            && ep.stream_condition.is_none()
                            && !resource_envelope
                            && (ep.body_media_alternatives || !ep.body_schema_titled))
                        // A query parameter beside the body keeps the header
                        // through both drops below, the same exception the shared
                        // and surviving-schema drops above already record: SFTPGo's
                        // `loaddata_from_request_body` posts an open, unnamed body
                        // beside two, and Fern keeps its content type. A titled
                        // schema escapes the same-`$ref` drop as it escapes the
                        // surviving-schema one above: measured at Fern 5.20.0
                        // (not re-measured at the certified pair), a
                        // titled `Message` echoed back by an unauthenticated
                        // operation keeps the header that Airflow's untitled
                        // `Pool` loses.
                        && !(ep.query_params.is_empty()
                            && matches!(body, RequestBody::Inline(_))
                            && ep.body_response_same_ref
                            && !ep.body_schema_titled
                            && !resource_envelope)
                        // A `stream-condition` body escapes this drop as it
                        // escapes the surviving-schema one above, and for the
                        // same reason: Fern sends the condition beside it.
                        && !(ep.query_params.is_empty()
                            && ep.stream_condition.is_none()
                            && matches!(body, RequestBody::Inline(fields)
                            if ep.body_schema_shape == BodySchemaShape::Ref
                                && (!ep.body_schema_dropped
                                    && ep.body_schema_metadata_missing
                                    && ep.body_schema_is_open
                                    && !ep.body_schema_is_success_response
                                    || ep.body_schema_is_response_heavy
                                        && ep.body_schema_is_open
                                        && ep.body_description_missing
                                        && !ep.body_schema_documented
                                        && fields.iter().filter(|field| field.spec_required).count() == 1)))
                        && (!(ep.body_schema_has_example
                                && ep.body_schema_documented
                                && !ep.body_schema_example_wrapped
                            || ep.body_schema_example_wrapped
                                && matches!(body, RequestBody::Inline(fields)
                                    if !fields.is_empty()
                                        && fields.len() > 1
                                        && fields.iter().all(|field| field.optional)
                                        && ep.body_schema_example_covers_all_fields
                                        && fields.iter().any(|field| field.convert)))
                            || body.all_fields_required()))) =>
        {
            Some(
                ep.body_json_media_type
                    .clone()
                    .unwrap_or_else(|| "application/json".to_string()),
            )
        }
        _ => None,
    };
    if content_type.is_some()
        || !ep.header_params.is_empty()
        || !ep.constant_headers.is_empty()
        || !ep.extensions.idempotency_headers.is_empty()
    {
        lines.push("            headers={".to_string());
        if let Some(value) = content_type {
            lines.push(format!("                \"content-type\": \"{value}\","));
        }
        // Constants and arguments together, in the order the operation declares
        // them; a header the order does not name (an endpoint built by hand)
        // follows, constants first.
        let mut headers: Vec<(&str, String)> = ep
            .constant_headers
            .iter()
            .map(|(name, value)| {
                (
                    name.as_str(),
                    format!("                \"{name}\": {value:?},"),
                )
            })
            .collect();
        for header_param in &ep.header_params {
            let value = if header_param.enum_value {
                format!("{}.value", header_param.py_name)
            } else {
                format!("str({})", header_param.py_name)
            };
            headers.push((
                header_param.wire_name.as_str(),
                format!(
                    "                \"{}\": {value} if {} is not None else None,",
                    header_param.wire_name, header_param.py_name
                ),
            ));
        }
        headers.sort_by_key(|(wire, _)| {
            ep.header_order
                .iter()
                .position(|declared| declared == wire)
                .unwrap_or(usize::MAX)
        });
        lines.extend(headers.into_iter().map(|(_, line)| line));
        // The idempotency headers follow the operation's own, in the order the
        // document lists them.
        lines.extend(
            ep.extensions
                .idempotency_headers
                .iter()
                .map(|header_param| {
                    format!(
                        "                \"{}\": str({1}) if {1} is not None else None,",
                        header_param.wire_name, header_param.py_name
                    )
                }),
        );
        lines.push("            },".to_string());
    }
    lines.push(if ep.extensions.retries_disabled {
        "            request_options=_request_options_with_retries_disabled,".to_string()
    } else {
        "            request_options=request_options,".to_string()
    });
    // A request body passes the `OMIT` sentinel so unset optionals drop out.
    if ep.request_body.is_some() {
        lines.push("            omit=OMIT,".to_string());
    }
    // A multipart form forces the request to serialize as `multipart/form-data`.
    if matches!(&ep.request_body, Some(RequestBody::Form(f)) if f.multipart) {
        lines.push("            force_multipart=True,".to_string());
    }
}

fn body_field_value_name<'a>(field: &'a BodyField, query: &[QueryParam]) -> &'a str {
    let original = field
        .collision_prefix
        .as_ref()
        .and_then(|prefix| field.py_name.strip_prefix(&format!("{prefix}_")))
        .unwrap_or(&field.py_name);
    // Keep the renamed signature, but serialize its body argument when a query
    // parameter shares the body's wire key (body-query-parameter-value).
    if query
        .iter()
        .any(|parameter| parameter.py_name == original && parameter.wire_name == field.wire_name)
    {
        &field.py_name
    } else {
        original
    }
}

/// The chunk type Fern yields from an OpenAPI-sourced SSE stream: the
/// `text/event-stream` media type's own schema, or `typing.Any` when it declares
/// none — Fern's importer does not resolve `x-fern-streaming`'s
/// `chunk-schema-ref`, so no extension can supply one.
fn sse_chunk(ep: &Endpoint, imports: &mut Imports) -> String {
    ep.stream_chunk
        .as_ref()
        .map_or_else(|| "typing.Any".to_string(), |ty| raw_type_str(ty, imports))
}

/// The `_sse.data` value that ends a Server-Sent-Events stream: the declared
/// terminator, or `None` — an event carrying no data — when none is declared.
fn sse_end(terminator: Option<&str>) -> String {
    terminator.map_or_else(
        || "None".to_string(),
        |end| format!("\"{}\"", escape_py_str(end)),
    )
}

/// Build one streaming raw-client method (sync or async): a context-managed
/// `httpx_client.stream(...)` that decodes Server-Sent Events into an iterator of
/// chunks over the `core.http_sse` runtime, matching Fern's shape (issue #43).
fn raw_stream_method(ep: &Endpoint, is_async: bool, imports: &mut Imports) -> String {
    imports.add_plain("contextlib");
    imports.add_plain("typing");
    imports.add_from("json.decoder", "JSONDecodeError");
    imports.add_core("api_error", "ApiError");
    imports.add_core("parse_error", "ParsingError");
    imports.add_from("pydantic", "ValidationError");
    if !ep.path_params.is_empty() || !ep.client_path_params.is_empty() {
        imports.add_core("jsonable_encoder", "encode_path_param");
    }

    let (wrapper, iter_t, aiter, decorator, async_kw, async_with, for_kw, iter_sse, read) =
        if is_async {
            (
                "AsyncHttpResponse",
                "typing.AsyncIterator",
                "typing.AsyncIterator",
                "@contextlib.asynccontextmanager",
                "async ",
                "async with",
                "async for",
                "aiter_sse",
                "await _response.aread()",
            )
        } else {
            (
                "HttpResponse",
                "typing.Iterator",
                "typing.Iterator",
                "@contextlib.contextmanager",
                "",
                "with",
                "for",
                "iter_sse",
                "_response.read()",
            )
        };
    let mp = method_params(ep, imports);
    let chunk = sse_chunk(ep, imports);
    // A streaming operation still declares error statuses, and Fern raises them
    // from inside the stream body once the response has been read, exactly as the
    // buffered path does.
    let error_branches = raw_error_branches(ep, imports);
    let error_branches = if error_branches.is_empty() {
        String::new()
    } else {
        format!("\n{error_branches}")
    };
    let data_type = format!("{aiter}[{chunk}]");
    let return_type = format!("{iter_t}[{wrapper}[{data_type}]]");
    let sig = signature(ep, &mp, &return_type, is_async);
    let docstring = raw_stream_docstring(ep, &mp, &return_type);

    let mut call: Vec<String> = retries_disabled_preamble(ep);
    call.push(format!(
        "        {async_with} self._client_wrapper.httpx_client.stream("
    ));
    if ep.path != "/" {
        call.push(format!("            {},", url_arg(ep, imports)));
    }
    call.extend(environment_base_url(ep));
    call.push(format!("            method=\"{}\",", ep.http_method));
    append_request_call_args(&mut call, ep, imports);
    call.push("        ) as _response:".to_string());

    let inner_return = format!("{wrapper}(response=_response, data=_iter())");
    let iter_body = match ep.stream_protocol.as_ref().unwrap_or(&StreamProtocol::Sse {
        terminator: None,
        events: Vec::new(),
    }) {
        StreamProtocol::Sse { terminator, events } if !events.is_empty() => {
            imports.add_plain("json");
            imports.add_from("logging", "warning");
            imports.add_core("http_sse._api", "EventSource");
            imports.add_core("pydantic_utilities", "parse_obj_as");
            // Each event's `event` field picks the model its JSON `data` parses
            // as; an event no branch names is skipped. Like the single-model
            // arm below, an event with empty `data` (a comment- or retry-only
            // frame) is skipped before any parse — measured at
            // fern-python-sdk 5.41.3 (exa-gate, torrentarr, truefoundry-trueforge).
            // Every dispatched model is `{event, data}` (see
            // `sse_event_dispatch`), which 5.41.3 reads as the SSE envelope
            // itself — `data` plus at most `id` and `retry` beside the
            // discriminant — so it parses `{"data": …}` rather than the bare
            // payload (the hand-written `event-stream-event-dispatch` fixture).
            let branches: Vec<String> = events
                .iter()
                .enumerate()
                .map(|(index, dispatched)| {
                    let model = raw_type_str(&TypeRef::Named(dispatched.model.clone()), imports);
                    let keyword = if index == 0 { "if" } else { "elif" };
                    let event = escape_py_str(&dispatched.event);
                    format!(
                        "                                {keyword} _sse.event == \"{event}\":
                                    try:
                                        yield typing.cast(
                                            {model},
                                            parse_obj_as(
                                                type_={model},
                                                object_={{\"data\": json.loads(_sse.data)}},
                                            ),
                                        )
                                    except Exception as e:
                                        warning(f\"Failed to parse SSE event '{event}': {{e}}, sse: {{_sse!r}}\")"
                    )
                })
                .collect();
            format!(
                "                            _event_source = EventSource(_response)
                            {for_kw} _sse in _event_source.{iter_sse}():
                                if _sse.data == {end}:
                                    return
                                if len(_sse.data) == 0:
                                    continue
{branches}",
                end = sse_end(terminator.as_deref()),
                branches = branches.join("\n"),
            )
        }
        StreamProtocol::Sse { terminator, .. } => {
            imports.add_from("logging", "error");
            imports.add_from("logging", "warning");
            imports.add_core("http_sse._api", "EventSource");
            imports.add_core("pydantic_utilities", "parse_sse_obj");
            let end = sse_end(terminator.as_deref());
            format!(
                "                            _event_source = EventSource(_response)
                            {for_kw} _sse in _event_source.{iter_sse}():
                                if _sse.data == {end}:
                                    return
                                if len(_sse.data) == 0:
                                    continue
                                try:
                                    yield typing.cast(
                                        {chunk},
                                        parse_sse_obj(
                                            sse=_sse,
                                            type_={chunk},
                                        ),
                                    )
                                except JSONDecodeError as e:
                                    warning(f\"Skipping SSE event with invalid JSON: {{e}}, sse: {{_sse!r}}\")
                                except (TypeError, ValueError, KeyError, AttributeError) as e:
                                    warning(
                                        f\"Skipping SSE event due to model construction error: {{type(e).__name__}}: {{e}}, sse: {{_sse!r}}\"
                                    )
                                except Exception as e:
                                    error(
                                        f\"Unexpected error processing SSE event: {{type(e).__name__}}: {{e}}, sse: {{_sse!r}}\"
                                    )"
            )
        }
        StreamProtocol::JsonLines => {
            imports.add_plain("json");
            imports.add_core("pydantic_utilities", "parse_obj_as");
            // One chunk per non-empty line; Fern skips a line that does not parse.
            let iter_lines = if is_async {
                "aiter_lines"
            } else {
                "iter_lines"
            };
            format!(
                "                            {for_kw} _text in _response.{iter_lines}():
                                try:
                                    if len(_text) == 0:
                                        continue
                                    yield typing.cast(
                                        {chunk},
                                        parse_obj_as(
                                            type_={chunk},
                                            object_=json.loads(_text),
                                        ),
                                    )
                                except Exception:
                                    pass"
            )
        }
    };
    let body = format!(
        "\
{call}

            {async_kw}def _stream() -> {wrapper}[{data_type}]:
                try:
                    if 200 <= _response.status_code < 300:

                        {async_kw}def _iter():
{iter_body}
                            return

                        return {inner_return}
                    {read}{error_branches}
                    _response_json = _response.json()
                except JSONDecodeError:
                    raise ApiError(
                        status_code=_response.status_code, headers=dict(_response.headers), body=_response.text
                    )
                except ValidationError as e:
                    raise ParsingError(
                        status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
                    )
                raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

            yield {yield_expr}",
        call = call.join("\n"),
        yield_expr = if is_async {
            "await _stream()"
        } else {
            "_stream()"
        },
    );
    format!("    {decorator}\n{sig}\n{docstring}\n{body}")
}

/// The streaming raw method docstring (indent 8): `Parameters` then a `Yields`
/// section (Fern uses `Yields`, not `Returns`, for a streaming method) carrying the
/// return type and the response description.
fn raw_stream_docstring(ep: &Endpoint, mp: &MethodParams, return_type: &str) -> String {
    let mut lines: Vec<String> = vec!["        \"\"\"".to_string()];
    if let Some(summary) = &ep.docstring {
        // A multi-line summary carries the 8-space docstring indent on every line,
        // as in the buffered raw method.
        for line in summary.split('\n') {
            lines.push(format!("        {}", python_doc_line(line)));
        }
        lines.push(String::new());
    }
    lines.push("        Parameters".to_string());
    lines.push("        ----------".to_string());
    for (name, ty, desc) in &mp.path {
        push_path_param(&mut lines, name, ty, desc);
    }
    let mut push_param = |dp: &DocParam| {
        lines.push(format!("        {} : {}", dp.name, dp.annotation));
        if let Some(desc) = &dp.description {
            push_param_doc(&mut lines, desc);
        }
        lines.push(String::new());
    };
    ordered_keyword_params(&mp.query, &mp.header, &mp.body)
        .into_iter()
        .for_each(&mut push_param);
    lines.push("        request_options : typing.Optional[RequestOptions]".to_string());
    lines.push("            Request-specific configuration.".to_string());
    lines.push(String::new());
    lines.push("        Yields".to_string());
    lines.push("        ------".to_string());
    lines.push(format!("        {return_type}"));
    push_return_doc(&mut lines, ep.response_doc.as_deref());
    lines.push("        \"\"\"".to_string());
    lines.join("\n")
}

/// Build one binary-download raw-client method (sync or async): a
/// context-managed `httpx_client.stream(...)` that yields response bytes.
fn raw_binary_stream_method(ep: &Endpoint, is_async: bool, imports: &mut Imports) -> String {
    imports.add_plain("contextlib");
    imports.add_plain("typing");
    imports.add_from("json.decoder", "JSONDecodeError");
    imports.add_core("api_error", "ApiError");
    if !ep.errors.is_empty() {
        imports.add_core("pydantic_utilities", "parse_obj_as");
    }
    imports.add_core("parse_error", "ParsingError");
    imports.add_from("pydantic", "ValidationError");
    if !ep.path_params.is_empty() || !ep.client_path_params.is_empty() {
        imports.add_core("jsonable_encoder", "encode_path_param");
    }

    let (wrapper, iter_t, data_iter, decorator, async_kw, async_with, read, iter_bytes, yield_expr) =
        if is_async {
            (
                "AsyncHttpResponse",
                "typing.AsyncIterator",
                "typing.AsyncIterator",
                "@contextlib.asynccontextmanager",
                "async ",
                "async with",
                "await _response.aread()",
                "aiter_bytes",
                "await _stream()",
            )
        } else {
            (
                "HttpResponse",
                "typing.Iterator",
                "typing.Iterator",
                "@contextlib.contextmanager",
                "",
                "with",
                "_response.read()",
                "iter_bytes",
                "_stream()",
            )
        };
    let mp = method_params(ep, imports);
    let data_type = format!("{data_iter}[bytes]");
    let return_type = format!("{iter_t}[{wrapper}[{data_type}]]");
    let sig = signature(ep, &mp, &return_type, is_async);
    let docstring = raw_binary_stream_docstring(ep, &mp, &return_type);

    let mut call: Vec<String> = retries_disabled_preamble(ep);
    call.push(format!(
        "        {async_with} self._client_wrapper.httpx_client.stream("
    ));
    if ep.path != "/" {
        call.push(format!("            {},", url_arg(ep, imports)));
    }
    call.extend(environment_base_url(ep));
    call.push(format!("            method=\"{}\",", ep.http_method));
    append_request_call_args(&mut call, ep, imports);
    call.push("        ) as _response:".to_string());

    let data_expr = if is_async {
        format!("(_chunk async for _chunk in _response.{iter_bytes}(chunk_size=_chunk_size))")
    } else {
        format!("(_chunk for _chunk in _response.{iter_bytes}(chunk_size=_chunk_size))")
    };
    let success_return = if is_async {
        format!("return {wrapper}(\n                            response=_response,\n                            data={data_expr},\n                        )")
    } else {
        format!("return {wrapper}(\n                            response=_response, data={data_expr}\n                        )")
    };
    let error_branches = raw_error_branches(ep, imports);
    let error_branches = if error_branches.is_empty() {
        String::new()
    } else {
        format!("\n{error_branches}")
    };
    let validation_error = "\n                except ValidationError as e:\n                    raise ParsingError(\n                        status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e\n                    )";
    let body = format!(
        "\
{call}

            {async_kw}def _stream() -> {wrapper}[{data_type}]:
                try:
                    if 200 <= _response.status_code < 300:
                        _chunk_size = request_options.get(\"chunk_size\", None) if request_options is not None else None
                        {success_return}
                    {read}{error_branches}
                    _response_json = _response.json()
                except JSONDecodeError:
                    raise ApiError(
                        status_code=_response.status_code, headers=dict(_response.headers), body=_response.text
                    ){validation_error}
                raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

            yield {yield_expr}",
        call = call.join("\n"),
    );
    format!("    {decorator}\n{sig}\n{docstring}\n{body}")
}

fn raw_binary_stream_docstring(ep: &Endpoint, mp: &MethodParams, return_type: &str) -> String {
    let mut lines: Vec<String> = vec!["        \"\"\"".to_string()];
    if let Some(summary) = &ep.docstring {
        for line in summary.split('\n') {
            lines.push(format!("        {}", python_doc_line(line)));
        }
        lines.push(String::new());
    }
    lines.push("        Parameters".to_string());
    lines.push("        ----------".to_string());
    for (name, ty, desc) in &mp.path {
        push_path_param(&mut lines, name, ty, desc);
    }
    let mut push_param = |dp: &DocParam| {
        lines.push(format!("        {} : {}", dp.name, dp.annotation));
        if let Some(desc) = &dp.description {
            push_param_doc(&mut lines, desc);
        }
        lines.push(String::new());
    };
    ordered_keyword_params(&mp.query, &mp.header, &mp.body)
        .into_iter()
        .for_each(&mut push_param);
    lines.push("        request_options : typing.Optional[RequestOptions]".to_string());
    lines.push(
        "            Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.".to_string(),
    );
    lines.push(String::new());
    lines.push("        Returns".to_string());
    lines.push("        -------".to_string());
    lines.push(format!("        {return_type}"));
    push_return_doc(&mut lines, ep.response_doc.as_deref());
    lines.push("        \"\"\"".to_string());
    lines.join("\n")
}

fn raw_error_branches(ep: &Endpoint, imports: &mut Imports) -> String {
    let mut lines = Vec::new();
    for err in &ep.errors {
        imports.add_core("pydantic_utilities", "parse_obj_as");
        let module = naming::module_name(&err.class_name);
        imports.add_from(&imports.error_import_path(&module), &err.class_name);
        let body = raw_type_str(&err.body_type, imports);
        lines.extend(error_raise_branch(err, &body, "                    "));
    }
    lines.join("\n")
}

/// One declared error's `raise` branch at `indent`: the status test, then the
/// exception built from the parsed body. A `4XX`/`5XX` range tests the whole
/// range and hands the exception the actual status, as fern-python-sdk 5.41.3
/// writes it.
fn error_raise_branch(err: &crate::ir::ErrorResponse, body: &str, indent: &str) -> Vec<String> {
    let code = err.status.code();
    let test = if err.status.is_range() {
        format!("if {code} <= _response.status_code < {}:", code + 100)
    } else {
        format!("if _response.status_code == {code}:")
    };
    let mut lines = vec![
        format!("{indent}{test}"),
        format!("{indent}    raise {}(", err.class_name),
        format!("{indent}        headers=dict(_response.headers),"),
        format!("{indent}        body=typing.cast("),
        format!("{indent}            {body},"),
        format!("{indent}            parse_obj_as("),
        format!("{indent}                type_={body},"),
        format!("{indent}                object_=_response.json(),"),
        format!("{indent}            ),"),
        format!("{indent}        ),"),
    ];
    if err.status.is_range() {
        lines.push(format!(
            "{indent}        status_code=_response.status_code,"
        ));
    }
    lines.push(format!("{indent}    )"));
    lines
}

/// Assemble the root `client.py`: the `FernApi`/`AsyncFernApi` classes that
/// aggregate every tag client. Bearer-auth-coupled today (a `token` argument);
/// generalize when more auth schemes are modeled.
struct RootClientFileCtx<'a> {
    /// The document's unquoted YAML timestamps; see [`ExampleCtx::yaml_unquoted_timestamps`].
    yaml_unquoted_timestamps: Option<&'a std::collections::BTreeSet<String>>,
    pkg: &'a str,
    client_name: &'a str,
    modules: &'a [&'a String],
    root_endpoints: &'a [&'a Endpoint],
    types: &'a [TypeDecl],
    enum_type: EnumType,
    tag_decls: &'a [TagTypeDecl],
    tag_map: &'a BTreeMap<String, String>,
    auth: &'a Auth,
    environment: Option<&'a crate::ir::Environment>,
    global_headers: &'a [GlobalHeader],
    client_path_parameters: &'a [ClientPathParameter],
    /// The fallback for an unset `max_retries` (Fern's `default_max_retries`).
    default_max_retries: u32,
}

fn root_client_file(
    env: &Environment<'static>,
    cx: RootClientFileCtx<'_>,
) -> Result<GeneratedFile> {
    let RootClientFileCtx {
        yaml_unquoted_timestamps,
        pkg,
        client_name,
        modules,
        root_endpoints,
        types,
        enum_type,
        tag_decls,
        tag_map,
        auth,
        environment,
        global_headers,
        client_path_parameters,
        default_max_retries,
    } = cx;
    let mut imports = Imports::at(RefLoc::PackageRoot, tag_map);
    if global_headers
        .iter()
        .any(|header| matches!(header.py_type(), HeaderType::Date))
    {
        imports.add_plain_as("datetime", "dt");
    }
    imports.add_plain("typing");
    imports.add_plain("httpx");
    // A credential read from the environment defaults to `os.getenv`, and a
    // required one is reported missing with an `ApiError`.
    if auth_credentials(auth)
        .iter()
        .any(|credential| credential.env.is_some())
        || global_headers
            .iter()
            .any(|header| header_env(header).is_some())
    {
        imports.add_plain("os");
        imports.add_core("api_error", "ApiError");
    }
    imports.add_core("client_wrapper", "AsyncClientWrapper");
    imports.add_core("client_wrapper", "SyncClientWrapper");
    imports.add_core("logging", "LogConfig");
    imports.add_core("logging", "Logger");
    if !root_endpoints.is_empty() {
        imports.add_core("request_options", "RequestOptions");
        imports.add_from(".raw_client", &format!("Raw{client_name}"));
        imports.add_from(".raw_client", &format!("AsyncRaw{client_name}"));
    }
    if let Some(e) = environment {
        imports.add_from(".environment", &e.enum_name);
    }
    let mut type_checking = if modules.is_empty() {
        String::new()
    } else {
        "if typing.TYPE_CHECKING:\n".to_string()
    };
    // Fern emits the sub-client imports alphabetically by module (`gadgets` before
    // `widgets`), even though the properties below stay in declaration order. This
    // block is never executed, but — unlike the `__init__.py` aggregators — client.py
    // is not isort-canonicalized by the e2e, so the order must match Fern exactly.
    let mut import_modules: Vec<&&String> = modules.iter().collect();
    import_modules.sort();
    for m in import_modules {
        let pascal = naming::to_pascal_case(m);
        // The two imported names are ordered the way ruff's isort orders
        // members — case-insensitively — so `Async{X}Client` usually comes
        // first, but not when the tag sorts `{X}Client` ahead
        // (`ApikeyauthClient`, and MockServer's `AsyncapiClient`).
        let mut names = [format!("Async{pascal}Client"), format!("{pascal}Client")];
        names.sort_by_key(|name| name.to_lowercase());
        if names
            .iter()
            .any(|name| root_sub_client_alias(m, name, client_name).is_some())
        {
            // An aliased import is never combined with its sibling: Fern writes
            // one `from … import X as alias` statement per name.
            for name in &names {
                type_checking.push_str(&format!(
                    "    from .{m}.client import {}\n",
                    root_sub_client_import(m, name, client_name)
                ));
            }
        } else {
            type_checking.push_str(&format!(
                "    from .{m}.client import {}, {}\n",
                names[0], names[1]
            ));
        }
    }
    let cfg = RootClientCfg {
        auth,
        environment,
        global_headers,
        client_path_parameters,
        default_max_retries,
    };
    let root_methods = root_client_methods(
        env,
        yaml_unquoted_timestamps,
        pkg,
        client_name,
        root_endpoints,
        types,
        enum_type,
        tag_decls,
        tag_map,
        auth,
        global_headers,
        client_path_parameters,
        environment.is_some(),
        false,
        &mut imports,
    )?;
    let async_root_methods = root_client_methods(
        env,
        yaml_unquoted_timestamps,
        pkg,
        client_name,
        root_endpoints,
        types,
        enum_type,
        tag_decls,
        tag_map,
        auth,
        global_headers,
        client_path_parameters,
        environment.is_some(),
        true,
        &mut imports,
    )?;
    let mut body = String::new();
    // Fern uses postponed annotations for sub-client properties. A root-only SDK
    // has no forward-referenced sub-client types and omits the future import.
    if !modules.is_empty() {
        body.push_str("from __future__ import annotations\n\n");
    }
    body.push_str(&imports.render());
    body.push_str("\n\n");
    body.push_str(&type_checking);
    if root_endpoints
        .iter()
        .any(|endpoint| endpoint.request_body.is_some())
    {
        body.push_str("\nOMIT = typing.cast(typing.Any, ...)\n");
    }
    body.push_str("\n\n");
    body.push_str(&root_client_class(
        env,
        client_name,
        pkg,
        false,
        modules,
        &root_methods,
        &cfg,
    )?);
    body.push_str(
        "\n\n\ndef _make_default_async_client(\n    timeout: typing.Optional[float],\n    follow_redirects: typing.Optional[bool],\n) -> httpx.AsyncClient:\n    try:\n        import httpx_aiohttp\n    except ImportError:\n        pass\n    else:\n        if follow_redirects is not None:\n            return httpx_aiohttp.HttpxAiohttpClient(timeout=timeout, follow_redirects=follow_redirects)\n        return httpx_aiohttp.HttpxAiohttpClient(timeout=timeout)\n\n    if follow_redirects is not None:\n        return httpx.AsyncClient(timeout=timeout, follow_redirects=follow_redirects)\n    return httpx.AsyncClient(timeout=timeout)",
    );
    body.push_str("\n\n\n");
    body.push_str(&root_client_class(
        env,
        client_name,
        pkg,
        true,
        modules,
        &async_root_methods,
        &cfg,
    )?);
    // When environments are in play, a module-level `_get_base_url` resolves the
    // explicit `base_url` or falls back to the selected environment's URL.
    if let Some(e) = environment.filter(|e| e.urls.is_empty()) {
        body.push_str(&format!(
            "\n\n\ndef _get_base_url(*, base_url: typing.Optional[str] = None, environment: {enum}) -> str:\n    if base_url is not None:\n        return base_url\n    elif environment is not None:\n        return environment.value\n    else:\n        raise Exception(\"Please pass in either base_url or environment to construct the client\")",
            enum = e.enum_name,
        ));
    }

    let contents = render(
        env,
        "file.py",
        "client.py",
        context! { header => HEADER, body => body },
    )?;
    Ok(GeneratedFile {
        path: PathBuf::from(format!("src/{pkg}/client.py")),
        contents,
    })
}

/// The credential/environment/global-header configuration shared by both root
/// client classes, bundled to keep [`root_client_class`] within the argument limit.
struct RootClientCfg<'a> {
    auth: &'a Auth,
    environment: Option<&'a crate::ir::Environment>,
    global_headers: &'a [GlobalHeader],
    client_path_parameters: &'a [ClientPathParameter],
    default_max_retries: u32,
}

#[allow(
    clippy::too_many_arguments,
    reason = "root method emission mirrors ClientCtx plus root imports"
)]
fn root_client_methods(
    _env: &Environment<'static>,
    yaml_unquoted_timestamps: Option<&std::collections::BTreeSet<String>>,
    pkg: &str,
    client_name: &str,
    endpoints: &[&Endpoint],
    types: &[TypeDecl],
    enum_type: EnumType,
    tag_decls: &[TagTypeDecl],
    tag_map: &BTreeMap<String, String>,
    auth: &Auth,
    global_headers: &[GlobalHeader],
    client_path_parameters: &[ClientPathParameter],
    has_environment: bool,
    is_async: bool,
    imports: &mut Imports,
) -> Result<Vec<String>> {
    if endpoints.is_empty() {
        return Ok(Vec::new());
    }
    let cx = ClientCtx {
        yaml_unquoted_timestamps,
        pkg,
        client_name,
        module: "",
        types,
        enum_type,
        tag_decls,
        auth,
        has_environment,
        tag_types: tag_map,
        global_headers,
        client_path_parameters,
        sdk_first_party: true,
        children: &[],
    };
    let mut method_imports = Imports::at(RefLoc::PackageRoot, tag_map);
    let methods = endpoints
        .iter()
        .map(|ep| client_method(&cx, ep, is_async, &mut method_imports))
        .collect();
    for (module, names) in method_imports.from {
        for name in names {
            imports.add_from(&module, &name);
        }
    }
    for (module, alias) in method_imports.plain {
        match alias {
            Some(alias) => imports.add_plain_as(&module, &alias),
            None => imports.add_plain(&module),
        }
    }
    imports.json_module_alias |= method_imports.json_module_alias;
    Ok(methods)
}

/// One root client class: the class docstring, the `__init__` wiring up the
/// client wrapper and the per-tag lazy slots, then one cached `@property` per tag.
fn root_client_class(
    env: &Environment<'static>,
    client_name: &str,
    pkg: &str,
    is_async: bool,
    modules: &[&String],
    root_methods: &[String],
    cfg: &RootClientCfg,
) -> Result<String> {
    let auth = cfg.auth;
    let environment = cfg.environment;
    let global_headers = cfg.global_headers;
    let (class_name, wrapper, httpx_type) = if is_async {
        (
            format!("Async{client_name}"),
            "AsyncClientWrapper",
            "httpx.AsyncClient",
        )
    } else {
        (client_name.to_string(), "SyncClientWrapper", "httpx.Client")
    };
    let leading_names: std::collections::HashSet<&str> = cfg
        .global_headers
        .iter()
        .filter(|h| h.default().is_none())
        .map(|h| h.py_name.as_str())
        .collect();
    let a = auth_client_parts(auth, &leading_names);
    // With an environment, `base_url` becomes optional, an `environment` parameter
    // (defaulting to the first server) is added, and the wrapper's `base_url` is
    // resolved through `_get_base_url`.
    let e = environment.map(EnvClientParts::new);
    // A multi-URL environment (operation-level servers) has no single base URL
    // to override: the client takes only the environment, and the wrapper
    // carries it whole.
    let multi_url = environment.is_some_and(|environment| !environment.urls.is_empty());
    let base_url_doc = if multi_url {
        String::new()
    } else {
        format!(
            "    base_url : {}\n        The base url to use for requests from the client.\n\n",
            if e.is_some() {
                "typing.Optional[str]"
            } else {
                "str"
            }
        )
    };
    let env_doc = e.as_ref().map_or_else(String::new, |e| e.doc.clone());
    let server_variable_doc = e
        .as_ref()
        .map_or_else(String::new, |e| e.server_variable_doc.clone());
    let base_url_ctor = e.as_ref().map_or_else(
        || "        base_url: str,\n".to_string(),
        |e| e.ctor.clone(),
    );
    let server_variable_init = e
        .as_ref()
        .map_or_else(String::new, |e| e.server_variable_init.clone());
    let wrapper_location = if multi_url {
        "environment=environment".to_string()
    } else {
        e.as_ref().map_or_else(
            || "base_url=base_url".to_string(),
            |_| "base_url=_get_base_url(base_url=base_url, environment=environment)".to_string(),
        )
    };
    // A defaulted header trails `logging` instead and is in no example (see
    // [`GlobalHeader::default`]); the `353-string-default-*` authored probes
    // byte-match the resulting `client.py` end to end.
    let (trailing, global_headers): (Vec<GlobalHeader>, Vec<GlobalHeader>) = global_headers
        .iter()
        .cloned()
        .partition(|h| h.default().is_some());
    let global_headers = global_headers.as_slice();
    let tr_doc: String = trailing
        .iter()
        .map(|h| format!("    {} : typing.Optional[str]\n", h.py_name))
        .collect();
    let tr_ctor: String = trailing
        .iter()
        .map(|h| format!("        {}: typing.Optional[str] = None,\n", h.py_name))
        .collect();
    let tr_wrapper: String = trailing
        .iter()
        .map(|h| format!("            {0}={0},\n", h.py_name))
        .collect();
    // Promoted global headers: a docstring/ctor/example/wrapper line each, emitted
    // right after `base_url` and before the auth credential. A required header is a
    // mandatory `str`; an optional one is `Optional[str] = None` — matching the
    // client wrapper.
    // A lifted base-path parameter leads the promoted headers on every line.
    let client_path_parameters = cfg.client_path_parameters;
    let client_param_doc: String = client_path_parameters
        .iter()
        .map(|parameter| {
            let ty = if parameter.default.is_some() {
                "typing.Optional[str]"
            } else {
                "str"
            };
            format!("    {} : {ty}\n", parameter.py_name)
        })
        .chain(global_headers.iter().map(|h| {
            let ty = if h.required() && header_env(h).is_none() {
                global_header_annotation(h)
            } else {
                format!("typing.Optional[{}]", global_header_annotation(h))
            };
            format!("    {} : {ty}\n", h.py_name)
        }))
        .collect();
    let client_param_ctor: String = client_path_parameters
        .iter()
        .map(|parameter| client_path_parameter_param(parameter, true))
        .chain(
            distinct_global_header_params(global_headers)
                .into_iter()
                .map(|h| {
                    // A header read from the environment defaults to `None`, as a
                    // credential does ([`env_fallback`]).
                    if h.required() && header_env(h).is_none() {
                        format!("        {}: {},\n", h.py_name, global_header_annotation(h))
                    } else {
                        format!(
                            "        {}: typing.Optional[{}] = None,\n",
                            h.py_name,
                            global_header_annotation(h)
                        )
                    }
                }),
        )
        .collect();
    // Two promoted headers sharing a parameter pass it once (the
    // `repeated-credential-example-keyword` departure): Fern repeats it.
    let client_param_example: String = client_path_parameters
        .iter()
        .map(|parameter| format!("        {},\n", client_path_parameter_example(parameter)))
        .chain(
            distinct_global_header_params(global_headers)
                .into_iter()
                .map(|header| format!("        {},\n", global_header_example(header))),
        )
        .collect();
    let client_param_wrapper: String = client_path_parameters
        .iter()
        .map(|parameter| &parameter.py_name)
        .chain(
            distinct_global_header_params(global_headers)
                .into_iter()
                .map(|h| &h.py_name),
        )
        .map(|name| format!("            {name}={name},\n"))
        .collect();
    let module_views: Vec<RootModuleView> = modules
        .iter()
        .map(|m| {
            let class = tag_client_name(m, is_async);
            let import = root_sub_client_import(m, &class, client_name);
            let cls = root_sub_client_alias(m, &class, client_name).unwrap_or(class);
            // The lazy `from .{attr}.client import {import}` sits at 12 spaces of indent.
            // Fern's printer wraps this single-name import into the parenthesized,
            // trailing-comma form once the flat line reaches 107 columns; ruff (run at
            // `line-length = 120`) then preserves that shape via its magic trailing
            // comma, and leaves the shorter flat lines untouched. crozier must make the
            // same call itself — ruff will not split a single-name import for it. The
            // boundary is exact: corpus row 123 wraps at 107 and the corpus's widest
            // flat import is 106.
            let wrap =
                12 + "from .".len() + m.len() + ".client import ".len() + import.len() >= 107;
            RootModuleView {
                attr: (*m).clone(),
                cls,
                import,
                wrap,
            }
        })
        .collect();
    let has_async_token = is_async && matches!(auth, Auth::Bearer { .. });
    let view = RootClientView {
        class_name: class_name.to_string(),
        pkg: pkg.to_string(),
        httpx_type: httpx_type.to_string(),
        wrapper: wrapper.to_string(),
        ctor_param: a.ctor_param,
        wrapper_arg: a.wrapper_arg,
        doc_param: a.doc_param,
        example_line: a.example_line,
        auth_check: distinct_global_header_params(global_headers)
            .into_iter()
            .filter_map(|h| Some(env_fallback(&h.py_name, header_env(h)?, h.required())))
            .collect::<String>()
            + &a.check,
        base_url_doc,
        env_doc,
        server_variable_doc,
        base_url_ctor,
        server_variable_init,
        wrapper_location,
        example_base_url: if e.is_some() {
            String::new()
        } else {
            "        base_url=\"https://yourhost.com/path/to/api\",\n".to_string()
        },
        client_param_doc,
        client_param_ctor,
        client_param_example,
        client_example_imports: if global_headers.iter().any(|header| header.py_type() == HeaderType::Date) { "    import datetime\n\n" } else { "" }.to_string(),
        client_param_wrapper,
        tr_doc,
        tr_ctor,
        tr_wrapper,
        raw_client_cls: if root_methods.is_empty() {
            String::new()
        } else if is_async {
            format!("AsyncRaw{client_name}")
        } else {
            format!("Raw{client_name}")
        },
        root_methods: root_methods.to_vec(),
        modules: module_views,
        is_async,
        async_token_doc: if has_async_token {
            "    async_token : typing.Optional[typing.Callable[[], typing.Awaitable[str]]]\n        An async callable that returns a bearer token. Use this when token acquisition involves async I/O (e.g., refreshing tokens via an async HTTP client). When provided, this is used instead of the synchronous token for async requests.\n\n"
        } else {
            ""
        }
        .to_string(),
        async_token_ctor: if has_async_token {
            "        async_token: typing.Optional[typing.Callable[[], typing.Awaitable[str]]] = None,\n"
        } else {
            ""
        }
        .to_string(),
        async_token_wrapper: if has_async_token {
            "            async_token=async_token,\n"
        } else {
            ""
        }
        .to_string(),
        default_max_retries: cfg.default_max_retries,
    };
    // The caller controls the separation between the sync/async classes and the
    // file's final newline, so drop the template's trailing newline.
    let rendered = render(
        env,
        "root_client.py",
        "client.py",
        minijinja::Value::from_serialize(&view),
    )?;
    Ok(rendered.trim_end_matches('\n').to_string())
}

/// One root client's per-tag slot: the attribute name (`dispatch_bays`) and its
/// client class (`DispatchBaysClient`/`AsyncDispatchBaysClient`).
#[derive(Serialize)]
struct RootModuleView {
    attr: String,
    /// The name the sub-client class is bound to in the root `client.py`: its own
    /// name, or the alias [`root_sub_client_alias`] gives it.
    cls: String,
    /// What the lazy import names: the class, with ` as {alias}` when aliased.
    import: String,
    /// Whether the lazy import line overflows Fern's 107-column threshold and must be
    /// emitted in the parenthesized multi-line form (a single-name import ruff won't
    /// split, so its magic trailing comma just preserves whichever shape we emit).
    wrap: bool,
}

/// The view model for `root_client.py.j2` (the `FernApi`/`AsyncFernApi` class).
#[derive(Serialize)]
struct RootClientView {
    class_name: String,
    pkg: String,
    httpx_type: String,
    wrapper: String,
    ctor_param: String,
    wrapper_arg: String,
    doc_param: String,
    example_line: String,
    /// The checks a required credential read from the environment gets.
    auth_check: String,
    /// The `base_url` docstring block (`str`, or `typing.Optional[str]` with an
    /// environment; empty with a multi-URL environment, which takes none).
    base_url_doc: String,
    /// The `environment` docstring `Parameters` block (empty without environments).
    env_doc: String,
    /// Root-client parameter documentation for variables in the first server URL.
    server_variable_doc: String,
    /// The `base_url` (and `environment`) constructor parameter line(s).
    base_url_ctor: String,
    /// Constructor statements that apply explicitly supplied server URL variables.
    server_variable_init: String,
    /// What the client wrapper is constructed from: `base_url=…` (`base_url`, or a
    /// `_get_base_url(...)` call with environments), or `environment=environment`
    /// for a multi-URL environment.
    wrapper_location: String,
    /// The `Examples` client instantiation's `base_url` line (empty with
    /// environments, which drop it).
    example_base_url: String,
    /// Lifted base-path and promoted global-header lines: the docstring `Parameters`
    /// entries, the constructor parameters, the `Examples` arguments, and the
    /// client-wrapper call arguments — all placed after `base_url`.
    client_param_doc: String,
    client_param_ctor: String,
    client_param_example: String,
    /// Imports needed by the constructor's worked example.
    client_example_imports: String,
    client_param_wrapper: String,
    /// Defaulted global-header lines (empty without any): the docstring
    /// `Parameters` entries, the constructor parameters and the client-wrapper call
    /// arguments, all placed after `logging`.
    tr_doc: String,
    tr_ctor: String,
    tr_wrapper: String,
    raw_client_cls: String,
    root_methods: Vec<String>,
    modules: Vec<RootModuleView>,
    /// Selects the aiohttp-autodetecting default for the async root client.
    is_async: bool,
    /// Bearer-only asynchronous token provider fragments, present on the async client.
    async_token_doc: String,
    async_token_ctor: String,
    async_token_wrapper: String,
    /// The `max_retries` fallback and its documented default (Fern's
    /// `default_max_retries`).
    default_max_retries: u32,
}

/// The environment-varying fragments of the root client: the docstring block, the
/// constructor parameter lines. Assembled once and reused across the sync/async
/// classes.
struct EnvClientParts {
    doc: String,
    ctor: String,
    server_variable_doc: String,
    server_variable_init: String,
}

impl EnvClientParts {
    fn new(e: &crate::ir::Environment) -> Self {
        let enum_name = &e.enum_name;
        let default = e.default_ref();
        let server_variable_doc = e
            .variables
            .iter()
            .map(|variable| {
                format!(
                    "    {} : typing.Optional[str]\n        Server URL variable for '{}'. Defaults to '{}'.\n\n",
                    variable.py_name,
                    variable.wire_name,
                    python_doc_line(&variable.default)
                )
            })
            .collect();
        let variable_ctor: String = e
            .variables
            .iter()
            .map(|variable| {
                format!(
                    "        {}: typing.Optional[str] = None,\n",
                    variable.py_name
                )
            })
            .collect();
        let format_args = e
            .variables
            .iter()
            .map(|variable| format!("{}=_{}", variable.wire_name, variable.py_name))
            .collect::<Vec<_>>()
            .join(", ");
        let server_variable_init = if e.variables.is_empty() {
            String::new()
        } else {
            let condition = e
                .variables
                .iter()
                .map(|variable| format!("{} is not None", variable.py_name))
                .collect::<Vec<_>>()
                .join(" or ");
            let defaults: String = e
                .variables
                .iter()
                .map(|variable| {
                    format!(
                        "            _{name} = {name} if {name} is not None else \"{default}\"\n",
                        name = variable.py_name,
                        default = escape_py_str(&variable.default),
                    )
                })
                .collect();
            // The override formats the template of the environment the caller
            // chose, the first templated one when it has none, and never replaces
            // a `base_url` the caller passed (`openfigi.com`,
            // `amazonaws.com-cloudformation`, `free5gc-pdu-session`; measured at
            // fern-python-sdk 5.41.3).
            let first = e
                .url_templates
                .first()
                .map_or(e.url_template.as_str(), |(_, template)| template.as_str());
            let templates: String = e
                .url_templates
                .iter()
                .map(|(member, template)| {
                    format!(
                        "                {enum_name}.{member}: \"{}\",\n",
                        escape_py_str(template)
                    )
                })
                .collect();
            format!(
                "        if {condition}:\n{defaults}            _environment_url_templates = {{\n{templates}            }}\n            _url_template = _environment_url_templates.get(environment, \"{first}\")\n            if base_url is None:\n                base_url = _url_template.format({format_args})\n",
                first = escape_py_str(first),
            )
        };
        // A multi-URL environment replaces `base_url`.
        let base_url_ctor = match e.urls.is_empty() {
            true => "        base_url: typing.Optional[str] = None,\n",
            false => "",
        };
        EnvClientParts {
            // Fern's docstring here leaks the import statement onto the description
            // line and pads the block with blank lines; reproduced verbatim.
            doc: format!(
                "    environment : {enum_name}\n        The environment to use for requests from the client. from .environment import {enum_name}\n\n\n\n        Defaults to {default}\n\n\n\n"
            ),
            ctor: format!(
                "{base_url_ctor}        environment: {enum_name} = {default},\n{variable_ctor}"
            ),
            server_variable_doc,
            server_variable_init,
        }
    }
}

/// The alias a root `client.py` imports a top-level sub-client class under, when
/// that class is named like one of the root client classes the same file defines
/// (`client_class_name: EcosystemClient` over an `ecosystem` resource). Fern then
/// imports it as `{module}_client_{Class}`, so the root class does not shadow it
/// and the sub-client property keeps the sub-client's type; `None` otherwise.
fn root_sub_client_alias(module: &str, class: &str, client_name: &str) -> Option<String> {
    (class == client_name || class.strip_prefix("Async") == Some(client_name))
        .then(|| format!("{module}_client_{class}"))
}

/// The import clause naming `class` in a root `client.py`: `{class}`, or
/// `{class} as {alias}` when [`root_sub_client_alias`] aliases it.
fn root_sub_client_import(module: &str, class: &str, client_name: &str) -> String {
    match root_sub_client_alias(module, class, client_name) {
        Some(alias) => format!("{class} as {alias}"),
        None => class.to_string(),
    }
}

/// The tag client class name for a module (`dispatch_bays` → `DispatchBaysClient`,
/// or `AsyncDispatchBaysClient`).
fn tag_client_name(module: &str, is_async: bool) -> String {
    let pascal = naming::to_pascal_case(module_stem(module));
    if is_async {
        format!("Async{pascal}Client")
    } else {
        format!("{pascal}Client")
    }
}

/// Shared context for emitting one per-tag `client.py`: the import package name,
/// the root client name, the module, and the type table the example generator
/// consults. Bundled so the client helpers stay within clippy's argument limit.
struct ClientCtx<'a> {
    /// The document's unquoted YAML timestamps; see [`ExampleCtx::yaml_unquoted_timestamps`].
    yaml_unquoted_timestamps: Option<&'a std::collections::BTreeSet<String>>,
    pkg: &'a str,
    client_name: &'a str,
    module: &'a str,
    types: &'a [TypeDecl],
    /// How string enums render, for the example generator.
    enum_type: EnumType,
    /// Hoisted tag-scoped types, for the example generator's constructors.
    tag_decls: &'a [TagTypeDecl],
    auth: &'a Auth,
    has_environment: bool,
    tag_types: &'a BTreeMap<String, String>,
    global_headers: &'a [GlobalHeader],
    client_path_parameters: &'a [ClientPathParameter],
    /// Whether Fern's isort pass files the SDK's own imports as first-party; see
    /// [`ExampleCtx::sdk_first_party`].
    sdk_first_party: bool,
    /// Nested sub-client modules this client exposes as lazy properties, in the
    /// order Fern writes them (see [`module_children`]). Empty for a leaf client.
    children: &'a [String],
}

/// Assemble a per-tag `client.py`: the sync and async high-level clients that
/// wrap the raw client, return `_response.data`, and carry a worked `Examples`
/// docstring.
fn client_file(
    env: &Environment<'static>,
    cx: &ClientCtx,
    endpoints: &[&Endpoint],
) -> Result<GeneratedFile> {
    let stem = naming::to_pascal_case(module_stem(cx.module));
    let mut imports = Imports::at(RefLoc::Client(cx.module.to_string()), cx.tag_types);
    imports.add_plain("typing");
    imports.add_core("client_wrapper", "AsyncClientWrapper");
    imports.add_core("client_wrapper", "SyncClientWrapper");
    // A parent client carries no methods of its own, so nothing there takes a
    // `request_options` argument and Fern does not import the type.
    if !endpoints.is_empty() {
        imports.add_core("request_options", "RequestOptions");
    }
    imports.add_from(".raw_client", &format!("Raw{stem}Client"));
    imports.add_from(".raw_client", &format!("AsyncRaw{stem}Client"));

    let sync = client_class(env, cx, endpoints, false, &mut imports)?;
    let async_class = client_class(env, cx, endpoints, true, &mut imports)?;
    // The `OMIT` sentinel is declared above the classes whenever a request body
    // is present, exactly as in the raw client.
    let omit = if endpoints.iter().any(|e| e.request_body.is_some()) {
        "OMIT = typing.cast(typing.Any, ...)\n\n\n"
    } else {
        ""
    };
    // A client that exposes nested sub-clients annotates them with postponed
    // annotations and imports their classes under `TYPE_CHECKING` only, the way
    // the root client already does — the runtime import lives inside each
    // property so the package's import graph stays acyclic.
    let mut type_checking = String::new();
    if !cx.children.is_empty() {
        type_checking.push_str("\n\nif typing.TYPE_CHECKING:");
        let mut sorted: Vec<&String> = cx.children.iter().collect();
        sorted.sort();
        for child in sorted {
            let attr = module_stem(child);
            let mut names = [tag_client_name(child, true), tag_client_name(child, false)];
            names.sort_by_key(|name| name.to_lowercase());
            type_checking.push_str(&format!(
                "\n    from .{attr}.client import {}, {}",
                names[0], names[1]
            ));
        }
    }
    let future = if cx.children.is_empty() {
        ""
    } else {
        "from __future__ import annotations\n\n"
    };
    let body = format!("{omit}{sync}\n\n\n{async_class}");
    let mut contents = render(
        env,
        "raw_client.py",
        cx.module,
        context! {
            header => HEADER,
            imports => format!("{future}{}{type_checking}", imports.render()),
            body => body,
        },
    )?;
    // The `TYPE_CHECKING` block is followed by one blank line when `OMIT` comes
    // next, not the two a class gets: TrueForge's `internal` client both nests
    // sub-clients and posts a body, and its golden writes the pair that way.
    if !type_checking.is_empty() && !omit.is_empty() {
        contents = contents.replacen("\n\n\nOMIT = ", "\n\nOMIT = ", 1);
    }
    Ok(GeneratedFile {
        path: PathBuf::from(format!("src/{}/{}/client.py", cx.pkg, cx.module)),
        contents,
    })
}

/// One high-level client class: `__init__`, the `with_raw_response` property, and
/// one wrapper method per operation. The class/wrapper/raw-client names are
/// derived from the module and `is_async`.
fn client_class(
    env: &Environment<'static>,
    cx: &ClientCtx,
    endpoints: &[&Endpoint],
    is_async: bool,
    imports: &mut Imports,
) -> Result<String> {
    let stem = naming::to_pascal_case(module_stem(cx.module));
    let (class_name, wrapper, raw_client_cls) = if is_async {
        (
            format!("Async{stem}Client"),
            "AsyncClientWrapper",
            format!("AsyncRaw{stem}Client"),
        )
    } else {
        (
            format!("{stem}Client"),
            "SyncClientWrapper",
            format!("Raw{stem}Client"),
        )
    };
    // Each method is computed in Rust (signature, docstring, delegation body); the
    // template lays out the class skeleton and separates the methods.
    let methods: Vec<String> = endpoints
        .iter()
        .map(|ep| client_method(cx, ep, is_async, imports))
        .collect();
    let children: Vec<_> = cx
        .children
        .iter()
        .map(|child| {
            let attr = module_stem(child);
            context! {
                attr => attr,
                cls => tag_client_name(child, is_async),
            }
        })
        .collect();
    let view = context! {
        class_name => class_name,
        wrapper => wrapper,
        raw_client_cls => raw_client_cls,
        methods => methods,
        children => children,
    };
    let rendered = render(env, "client_class.py", &class_name, view)?;
    Ok(rendered.trim_end_matches('\n').to_string())
}

/// One high-level wrapper method: the signature, the docstring (with a worked
/// example), and a body delegating to the raw client and returning
/// `_response.data`.
fn client_method(cx: &ClientCtx, ep: &Endpoint, is_async: bool, imports: &mut Imports) -> String {
    if ep.streaming {
        return client_stream_method(cx, ep, is_async, imports);
    }
    if ep.binary_response {
        return client_binary_stream_method(cx, ep, is_async, imports);
    }
    let mp = method_params(ep, imports);
    let return_type = if ep.http_method == "HEAD" {
        "typing.Dict[str, str]".to_string()
    } else {
        pager_return_type(ep, &mp.inner, is_async, imports).unwrap_or_else(|| mp.inner.clone())
    };
    let sig = signature(ep, &mp, &return_type, is_async);
    let docstring = client_docstring(cx, ep, &mp, is_async);

    // Delegation: path params positionally, the rest as keywords, then
    // `request_options`, laid out with ruff's right-hand-split (line length 120).
    let await_ = if is_async { "await " } else { "" };
    let mut call_args: Vec<Doc> = Vec::new();
    for (name, _, _) in &mp.path {
        call_args.push(Doc::atom(name.clone()));
    }
    for dp in ordered_keyword_params(&mp.query, &mp.header, &mp.body) {
        call_args.push(Doc::atom(format!("{}={}", dp.name, dp.name)));
    }
    call_args.push(Doc::atom("request_options=request_options"));
    let open = format!("{await_}self._raw_client.{}(", ep.method_name);
    // A paginated raw method already returns the pager, so the high-level one
    // returns it straight through rather than unwrapping a buffered response.
    if ep.pagination.is_some() {
        let call = format!("        return {}", Doc::group(open, call_args, ")").flat());
        return format!("{sig}\n{docstring}\n{call}");
    }
    let call = format!(
        "        _response = {}",
        Doc::group(open, call_args, ")").flat()
    );
    let result = if ep.http_method == "HEAD" {
        "_response.headers"
    } else {
        "_response.data"
    };
    format!("{sig}\n{docstring}\n{call}\n        return {result}")
}

/// The high-level streaming method (sync or async): a plain generator that opens
/// the context-managed raw stream and yields each decoded chunk. Matches Fern's
/// `Iterator`/`AsyncIterator` wrapper over the raw client's SSE stream (issue #43).
fn client_stream_method(
    cx: &ClientCtx,
    ep: &Endpoint,
    is_async: bool,
    imports: &mut Imports,
) -> String {
    let mp = method_params(ep, imports);
    let iter_t = if is_async {
        "typing.AsyncIterator"
    } else {
        "typing.Iterator"
    };
    let chunk = sse_chunk(ep, imports);
    let return_type = format!("{iter_t}[{chunk}]");
    let sig = signature(ep, &mp, &return_type, is_async);
    let docstring = client_stream_docstring(cx, ep, &mp, is_async, &chunk);

    // Delegation call arguments, in signature order (path positionally, the rest as
    // keywords, then `request_options`) — identical to the buffered high-level method.
    let mut call_args: Vec<Doc> = Vec::new();
    for (name, _, _) in &mp.path {
        call_args.push(Doc::atom(name.clone()));
    }
    for dp in ordered_keyword_params(&mp.query, &mp.header, &mp.body) {
        call_args.push(Doc::atom(format!("{}={}", dp.name, dp.name)));
    }
    call_args.push(Doc::atom("request_options=request_options"));
    let open = format!("self._raw_client.{}(", ep.method_name);
    let raw_call = Doc::group(open, call_args, ")").flat();
    let body = if is_async {
        format!(
            "        async with {raw_call} as r:\n            async for _chunk in r.data:\n                yield _chunk"
        )
    } else {
        format!("        with {raw_call} as r:\n            yield from r.data")
    };
    format!("{sig}\n{docstring}\n{body}")
}

/// The streaming high-level method docstring (indent 8): `Parameters`, a `Yields`
/// section (matching Fern's streaming methods), then a worked `Examples` block that
/// iterates the returned stream.
fn client_stream_docstring(
    cx: &ClientCtx,
    ep: &Endpoint,
    mp: &MethodParams,
    is_async: bool,
    chunk: &str,
) -> String {
    let iter_t = if is_async {
        "typing.AsyncIterator"
    } else {
        "typing.Iterator"
    };
    let mut lines: Vec<String> = vec!["        \"\"\"".to_string()];
    if let Some(summary) = &ep.docstring {
        // The summary block every other high-level method carries.
        for line in summary.split('\n') {
            lines.push(format!("        {}", python_doc_line(line)));
        }
        lines.push(String::new());
    }
    lines.push("        Parameters".to_string());
    lines.push("        ----------".to_string());
    for (name, ty, desc) in &mp.path {
        push_path_param(&mut lines, name, ty, desc);
    }
    let mut push_param = |dp: &DocParam| {
        lines.push(format!("        {} : {}", dp.name, dp.annotation));
        if let Some(desc) = &dp.description {
            push_param_doc(&mut lines, desc);
        }
        lines.push(String::new());
    };
    ordered_keyword_params(&mp.query, &mp.header, &mp.body)
        .into_iter()
        .for_each(&mut push_param);
    lines.push("        request_options : typing.Optional[RequestOptions]".to_string());
    lines.push("            Request-specific configuration.".to_string());
    lines.push(String::new());
    lines.push("        Yields".to_string());
    lines.push("        ------".to_string());
    lines.push(format!("        {iter_t}[{chunk}]"));
    push_return_doc(&mut lines, ep.response_doc.as_deref());

    let mut ctx = ExampleCtx {
        types: cx.types,
        enum_type: cx.enum_type,
        yaml_unquoted_timestamps: cx.yaml_unquoted_timestamps,
        tag_decls: cx.tag_decls,
        referenced: BTreeSet::new(),
        referenced_doc_order: Vec::new(),
        referenced_tag: BTreeSet::new(),
        referenced_tag_doc_order: Vec::new(),
        uses_datetime: false,
        datetime_precedes_tag_import: false,
        auth: cx.auth,
        has_environment: cx.has_environment,
        global_headers: cx.global_headers,
        client_path_parameters: cx.client_path_parameters,
        building: Default::default(),
        expanding_aliases: Vec::new(),
        documentation: false,
        reference: false,
        field_examples_ignored: false,
        untyped_arguments: BTreeSet::new(),
        sdk_first_party: cx.sdk_first_party,
    };
    if let Some(ex_lines) = build_example(ep, is_async, cx.module, cx.pkg, cx.client_name, &mut ctx)
    {
        lines.push(String::new());
        lines.push("        Examples".to_string());
        lines.push("        --------".to_string());
        for l in ex_lines {
            if l.is_empty() {
                lines.push(String::new());
            } else {
                lines.push(format!("        {l}"));
            }
        }
    }
    lines.push("        \"\"\"".to_string());
    if ep.module.is_empty() && ep.openapi_31 {
        lines
            .iter_mut()
            .filter(|line| line.is_empty())
            .for_each(|line| {
                *line = "        ".to_string();
            });
    }
    lines.join("\n")
}

fn client_binary_stream_method(
    cx: &ClientCtx,
    ep: &Endpoint,
    is_async: bool,
    imports: &mut Imports,
) -> String {
    imports.add_plain("typing");
    let mp = method_params(ep, imports);
    let iter_t = if is_async {
        "typing.AsyncIterator"
    } else {
        "typing.Iterator"
    };
    let return_type = format!("{iter_t}[bytes]");
    let sig = signature(ep, &mp, &return_type, is_async);
    let docstring = client_binary_stream_docstring(cx, ep, &mp, is_async);

    let mut call_args: Vec<Doc> = Vec::new();
    for (name, _, _) in &mp.path {
        call_args.push(Doc::atom(name.clone()));
    }
    for dp in ordered_keyword_params(&mp.query, &mp.header, &mp.body) {
        call_args.push(Doc::atom(format!("{}={}", dp.name, dp.name)));
    }
    call_args.push(Doc::atom("request_options=request_options"));
    let open = format!("self._raw_client.{}(", ep.method_name);
    let raw_call = Doc::group(open, call_args, ")").flat();
    let body = if is_async {
        format!("        async with {raw_call} as r:\n            async for _chunk in r.data:\n                yield _chunk")
    } else {
        format!("        with {raw_call} as r:\n            yield from r.data")
    };
    format!("{sig}\n{docstring}\n{body}")
}

fn client_binary_stream_docstring(
    cx: &ClientCtx,
    ep: &Endpoint,
    mp: &MethodParams,
    is_async: bool,
) -> String {
    let iter_t = if is_async {
        "typing.AsyncIterator"
    } else {
        "typing.Iterator"
    };
    let mut lines: Vec<String> = vec!["        \"\"\"".to_string()];
    if let Some(summary) = &ep.docstring {
        for line in summary.split('\n') {
            lines.push(format!("        {}", python_doc_line(line)));
        }
        lines.push(String::new());
    }
    lines.push("        Parameters".to_string());
    lines.push("        ----------".to_string());
    for (name, ty, desc) in &mp.path {
        push_path_param(&mut lines, name, ty, desc);
    }
    let mut push_param = |dp: &DocParam| {
        lines.push(format!("        {} : {}", dp.name, dp.annotation));
        if let Some(desc) = &dp.description {
            push_param_doc(&mut lines, desc);
        }
        lines.push(String::new());
    };
    ordered_keyword_params(&mp.query, &mp.header, &mp.body)
        .into_iter()
        .for_each(&mut push_param);
    lines.push("        request_options : typing.Optional[RequestOptions]".to_string());
    lines.push(
        "            Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.".to_string(),
    );
    lines.push(String::new());
    lines.push("        Returns".to_string());
    lines.push("        -------".to_string());
    lines.push(format!("        {iter_t}[bytes]"));
    push_return_doc(&mut lines, ep.response_doc.as_deref());

    let mut ctx = ExampleCtx {
        types: cx.types,
        enum_type: cx.enum_type,
        yaml_unquoted_timestamps: cx.yaml_unquoted_timestamps,
        tag_decls: cx.tag_decls,
        referenced: BTreeSet::new(),
        referenced_doc_order: Vec::new(),
        referenced_tag: BTreeSet::new(),
        referenced_tag_doc_order: Vec::new(),
        uses_datetime: false,
        datetime_precedes_tag_import: false,
        auth: cx.auth,
        has_environment: cx.has_environment,
        global_headers: cx.global_headers,
        client_path_parameters: cx.client_path_parameters,
        building: Default::default(),
        expanding_aliases: Vec::new(),
        documentation: false,
        reference: false,
        field_examples_ignored: false,
        untyped_arguments: BTreeSet::new(),
        sdk_first_party: cx.sdk_first_party,
    };
    if let Some(ex_lines) = (!root_binary_download_undocumented(ep))
        .then(|| build_example(ep, is_async, cx.module, cx.pkg, cx.client_name, &mut ctx))
        .flatten()
    {
        lines.push(String::new());
        lines.push("        Examples".to_string());
        lines.push("        --------".to_string());
        for l in ex_lines {
            if l.is_empty() {
                lines.push(String::new());
            } else {
                lines.push(format!("        {l}"));
            }
        }
    }
    lines.push("        \"\"\"".to_string());
    lines.join("\n")
}

/// The high-level method docstring (indent 8): the same summary + `Parameters` +
/// `Returns` as the raw client, followed by a worked `Examples` block. A raw
/// bytes body has no example (Fern omits the whole section).
fn client_docstring(cx: &ClientCtx, ep: &Endpoint, mp: &MethodParams, is_async: bool) -> String {
    let mut lines: Vec<String> = vec!["        \"\"\"".to_string()];
    if let Some(summary) = &ep.docstring {
        for line in summary.split('\n') {
            lines.push(format!("        {}", python_doc_line(line)));
        }
        lines.push(String::new());
    }
    lines.push("        Parameters".to_string());
    lines.push("        ----------".to_string());
    for (name, ty, desc) in &mp.path {
        push_path_param(&mut lines, name, ty, desc);
    }
    let mut push_param = |dp: &DocParam| {
        lines.push(format!("        {} : {}", dp.name, dp.annotation));
        if let Some(desc) = &dp.description {
            push_param_doc(&mut lines, desc);
        }
        lines.push(String::new());
    };
    ordered_keyword_params(&mp.query, &mp.header, &mp.body)
        .into_iter()
        .for_each(&mut push_param);
    lines.push("        request_options : typing.Optional[RequestOptions]".to_string());
    lines.push("            Request-specific configuration.".to_string());
    lines.push(String::new());
    lines.push("        Returns".to_string());
    lines.push("        -------".to_string());
    let pager = ep
        .pagination
        .as_ref()
        .map(|pagination| pager_doc_type(pagination, &mp.inner, is_async));
    lines.push(format!(
        "        {}",
        if ep.http_method == "HEAD" && ep.response.is_some() {
            "typing.Dict[str, str]"
        } else {
            pager.as_deref().unwrap_or(&mp.inner)
        }
    ));
    // A concrete response carries its description (indented, blank when the spec
    // gives none) under the return type.
    if ep.response.is_some() {
        push_return_doc(&mut lines, ep.response_doc.as_deref());
    }

    let mut ctx = ExampleCtx {
        types: cx.types,
        enum_type: cx.enum_type,
        yaml_unquoted_timestamps: cx.yaml_unquoted_timestamps,
        tag_decls: cx.tag_decls,
        referenced: BTreeSet::new(),
        referenced_doc_order: Vec::new(),
        referenced_tag: BTreeSet::new(),
        referenced_tag_doc_order: Vec::new(),
        uses_datetime: false,
        datetime_precedes_tag_import: false,
        auth: cx.auth,
        has_environment: cx.has_environment,
        global_headers: cx.global_headers,
        client_path_parameters: cx.client_path_parameters,
        building: Default::default(),
        expanding_aliases: Vec::new(),
        documentation: false,
        reference: false,
        field_examples_ignored: false,
        untyped_arguments: BTreeSet::new(),
        sdk_first_party: cx.sdk_first_party,
    };
    // With an example, one blank line separates the `Returns` block from
    // `Examples`; without one, close straight after (like the raw docstring).
    if let Some(ex_lines) = build_example(ep, is_async, cx.module, cx.pkg, cx.client_name, &mut ctx)
    {
        lines.push(String::new());
        lines.push("        Examples".to_string());
        lines.push("        --------".to_string());
        for l in ex_lines {
            if l.is_empty() {
                lines.push(String::new());
            } else {
                lines.push(format!("        {l}"));
            }
        }
    }
    lines.push("        \"\"\"".to_string());
    if ep.module.is_empty() && ep.openapi_31 {
        lines
            .iter_mut()
            .filter(|line| line.is_empty())
            .for_each(|line| {
                *line = "        ".to_string();
            });
    }
    lines.join("\n")
}

/// A synthesized Python example value, formatted the way Fern's snippet formatter
/// (ruff defaults) renders it.
#[derive(Clone)]
enum Example {
    /// A leaf rendered verbatim (`"string"`, `1`, `True`, `{"key": "value"}`).
    Atom(String),
    /// A call `Name(arg, ...)`; each arg is `(Some("kw"), val)` or `(None, val)`
    /// for a positional argument. A non-empty arg list always explodes.
    Call(String, Vec<(Option<String>, Example)>),
    /// A discriminated-union wrapper holding its payload whole, `Name(value=…)`:
    /// it always explodes, and its one argument carries no trailing comma.
    Wrapped(String, Box<Example>),
    /// A list `[items]` — explodes only when an item does.
    List(Vec<Example>),
    /// A list supplied verbatim by the spec — wraps at the snippet line limit and
    /// retains Python's trailing commas when exploded.
    ExplicitList(Vec<Example>),
    /// A list rendered by `reference.md`, which always expands supplied and
    /// synthesized values recursively and omits only the final comma.
    ReferenceList(Vec<Example>),
    /// A dict with fixed string keys — explodes only when a value does.
    Dict(Vec<(String, Example)>),
    /// The same dict as written into `README.md`/`reference.md`. Fern's markdown
    /// writer ends a length-wrapped dict without Python's magic trailing comma
    /// (`mosip-esignet`'s `public_key`), while the docstring writer keeps it
    /// (`free5gc-pdu-session`'s `global_ngenb_id`), so the flavor is the variant.
    DocDict(Vec<(String, Example)>),
    /// A reference-documentation dict. Fern's reference writer expands map
    /// examples even when every value would fit on one line.
    ReferenceDict(Vec<(String, Example)>),
}

impl Example {
    /// Whether this value forces its enclosing line to break: a call with
    /// arguments carries a magic trailing comma; a collection breaks when a child
    /// does.
    fn forces_multiline(&self) -> bool {
        match self {
            Example::Atom(_) => false,
            Example::Call(_, args) => !args.is_empty(),
            Example::Wrapped(..) => true,
            Example::List(items) | Example::ExplicitList(items) => {
                items.iter().any(Example::forces_multiline)
            }
            Example::ReferenceList(items) => !items.is_empty(),
            Example::Dict(pairs) | Example::DocDict(pairs) => {
                pairs.iter().any(|(_, v)| v.forces_multiline())
            }
            Example::ReferenceDict(pairs) => !pairs.is_empty(),
        }
    }

    /// The same tree with every `Dict` in the markdown writers' flavor.
    fn into_documentation_dicts(self) -> Example {
        let convert_pairs = |pairs: Vec<(String, Example)>| {
            pairs
                .into_iter()
                .map(|(key, value)| (key, value.into_documentation_dicts()))
                .collect::<Vec<_>>()
        };
        let convert_items = |items: Vec<Example>| {
            items
                .into_iter()
                .map(Example::into_documentation_dicts)
                .collect::<Vec<_>>()
        };
        match self {
            Example::Atom(value) => Example::Atom(value),
            Example::Call(name, args) => Example::Call(
                name,
                args.into_iter()
                    .map(|(kw, value)| (kw, value.into_documentation_dicts()))
                    .collect(),
            ),
            Example::Wrapped(name, value) => {
                Example::Wrapped(name, Box::new(value.into_documentation_dicts()))
            }
            Example::List(items) => Example::List(convert_items(items)),
            Example::ExplicitList(items) => Example::ExplicitList(convert_items(items)),
            Example::ReferenceList(items) => Example::ReferenceList(convert_items(items)),
            Example::Dict(pairs) | Example::DocDict(pairs) => {
                Example::DocDict(convert_pairs(pairs))
            }
            Example::ReferenceDict(pairs) => Example::ReferenceDict(convert_pairs(pairs)),
        }
    }

    /// The single-line rendering.
    fn flat(&self) -> String {
        match self {
            Example::Atom(s) => s.clone(),
            Example::Call(name, args) => {
                let inner = args
                    .iter()
                    .map(|(kw, v)| match kw {
                        Some(k) => format!("{k}={}", v.flat()),
                        None => v.flat(),
                    })
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("{name}({inner})")
            }
            Example::Wrapped(name, value) => format!("{name}(value={})", value.flat()),
            Example::List(items) | Example::ExplicitList(items) | Example::ReferenceList(items) => {
                format!(
                    "[{}]",
                    items
                        .iter()
                        .map(Example::flat)
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            }
            Example::Dict(pairs) | Example::DocDict(pairs) | Example::ReferenceDict(pairs) => {
                if pairs.is_empty() {
                    return "{}".to_string();
                }
                let inner = pairs
                    .iter()
                    .map(|(k, v)| format!("\"{k}\": {}", v.flat()))
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("{{{inner}}}")
            }
        }
    }

    /// Render the value starting on a line indented by `indent` spaces (the
    /// indent of the line this value begins on). Continuation lines align to
    /// `indent`; exploded children indent by four more.
    ///
    /// The examples are laid out by hand rather than through `ruff format`
    /// (unlike the SDK files) because Fern's committed examples are *not* a ruff
    /// fixed point: ruff reformats a long `await <call>(...)` by parenthesizing
    /// the awaited expression, whereas Fern leaves the long `await …(` line and
    /// only explodes the call arguments. This renderer reproduces Fern's form.
    fn render(&self, indent: usize) -> String {
        self.render_at(indent, indent)
    }

    /// Render with separate physical indentation and starting column. A keyword
    /// value starts after `name=`, which affects Fern's 88-column wrapping without
    /// changing the indentation of any continuation lines.
    fn render_at(&self, indent: usize, column: usize) -> String {
        match self {
            Example::Atom(s) => s.clone(),
            Example::Call(name, args) => {
                if args.is_empty() {
                    return format!("{name}()");
                }
                let pad = " ".repeat(indent);
                let inner_pad = " ".repeat(indent + 4);
                let mut out = format!("{name}(\n");
                for (kw, v) in args {
                    let head = match kw {
                        Some(k) => format!("{inner_pad}{k}="),
                        None => inner_pad.clone(),
                    };
                    out.push_str(&head);
                    let value_column = indent + 4 + kw.as_ref().map_or(0, |key| key.len() + 1);
                    out.push_str(&v.render_at(indent + 4, value_column));
                    out.push_str(",\n");
                }
                out.push_str(&pad);
                out.push(')');
                out
            }
            Example::Wrapped(name, value) => format!(
                "{name}(\n{}value={}\n{})",
                " ".repeat(indent + 4),
                value.render_at(indent + 4, indent + 10),
                " ".repeat(indent)
            ),
            Example::List(items) => {
                // Width is counted in *characters*: Python and `ruff` measure a
                // line that way, so VTEX's `"Alimentação"` example is 88 columns
                // and stays flat where its 90 UTF-8 bytes would have wrapped it.
                if !self.forces_multiline() && column + self.flat().chars().count() <= 80 {
                    return self.flat();
                }
                let pad = " ".repeat(indent);
                let inner_pad = " ".repeat(indent + 4);
                // Several wrapped items keep Python's magic trailing comma, as
                // Groupe PSA's two-item `remote_types` example does; one does not.
                let trailing_comma = if items.len() == 1 { "" } else { "," };
                let body = items
                    .iter()
                    .map(|it| {
                        format!(
                            "{inner_pad}{}{trailing_comma}",
                            it.render_at(indent + 4, indent + 4)
                        )
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                format!("[\n{body}\n{pad}]")
            }
            Example::ExplicitList(items) => {
                let matrix = items.len() >= 3
                    && items
                        .iter()
                        .all(|item| matches!(item, Example::List(_) | Example::ExplicitList(_)));
                if !matrix && !self.forces_multiline() && column + self.flat().chars().count() <= 80
                {
                    return self.flat();
                }
                let pad = " ".repeat(indent);
                let inner_pad = " ".repeat(indent + 4);
                // A one-item list that has to wrap carries no trailing comma —
                // `mosip-esignet`'s single-member `client_auth_methods` and
                // `grant_types` examples wrap exactly that way — while a list of
                // several keeps Python's magic trailing comma.
                let trailing_comma = if items.len() == 1 { "" } else { "," };
                let body = items
                    .iter()
                    .map(|it| {
                        format!(
                            "{inner_pad}{}{trailing_comma}",
                            it.render_at(indent + 4, indent + 4)
                        )
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                format!("[\n{body}\n{pad}]")
            }
            Example::ReferenceList(items) => {
                if items.is_empty() {
                    return "[]".to_string();
                }
                let pad = " ".repeat(indent);
                let inner_pad = " ".repeat(indent + 4);
                let matrix = items.len() >= 3
                    && items
                        .iter()
                        .all(|item| matches!(item, Example::List(_) | Example::ExplicitList(_)));
                let body = items
                    .iter()
                    .enumerate()
                    .map(|(index, item)| {
                        let comma = if index + 1 == items.len() { "" } else { "," };
                        let rendered = if matrix {
                            item.flat()
                        } else {
                            item.render_at(indent + 4, indent + 4)
                        };
                        format!("{inner_pad}{rendered}{comma}")
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                format!("[\n{body}\n{pad}]")
            }
            Example::Dict(pairs) | Example::DocDict(pairs) => {
                let wraps_for_length =
                    !self.forces_multiline() && column + self.flat().chars().count() > 80;
                if !self.forces_multiline() && !wraps_for_length {
                    return self.flat();
                }
                let pad = " ".repeat(indent);
                let inner_pad = " ".repeat(indent + 4);
                // Like a list, a wrapped dict of one entry takes no trailing
                // comma: MockServer's `{"spec": "asyncapi: …"}` wraps bare.
                let last_comma = !matches!(self, Example::DocDict(_)) && pairs.len() > 1;
                // A dict a constructor value breaks separates its entries as a
                // Python literal must: Supabase's `attribute_mapping.keys` maps
                // names to models, and Fern's docstring ends every
                // `CreateProviderBodyAttributeMappingKeysValue(…)` with a comma,
                // where its Markdown writers leave the last one bare. One entry
                // ends bare in either, as a length-wrapped one does.
                let separated = wraps_for_length || self.forces_multiline();
                let body = pairs
                    .iter()
                    .enumerate()
                    .map(|(index, (k, v))| {
                        let comma = if separated && (last_comma || index + 1 < pairs.len()) {
                            ","
                        } else {
                            ""
                        };
                        format!(
                            "{inner_pad}\"{k}\": {}{comma}",
                            v.render_at(indent + 4, indent + k.len() + 8),
                        )
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                format!("{{\n{body}\n{pad}}}")
            }
            Example::ReferenceDict(pairs) => {
                if pairs.is_empty() {
                    return "{}".to_string();
                }
                let pad = " ".repeat(indent);
                let inner_pad = " ".repeat(indent + 4);
                let body = pairs
                    .iter()
                    .enumerate()
                    .map(|(index, (key, value))| {
                        let comma = if index + 1 == pairs.len() { "" } else { "," };
                        format!(
                            "{inner_pad}\"{key}\": {}{comma}",
                            value.render_at(indent + 4, indent + key.len() + 8)
                        )
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                format!("{{\n{body}\n{pad}}}")
            }
        }
    }
}

/// Threads the type table and the imports/datetime a worked example accumulates.
struct ExampleCtx<'a> {
    types: &'a [TypeDecl],
    /// How string enums render: a literal enum's example is its plain string.
    enum_type: EnumType,
    /// The timestamp-like scalars a YAML document writes unquoted, `None` for
    /// JSON. Such a scalar is a date to Fern's parser, not a string, so an example
    /// spelled by one is not one a plain `str` field can take.
    yaml_unquoted_timestamps: Option<&'a std::collections::BTreeSet<String>>,
    /// Hoisted tag-scoped types, consulted so an example can construct one and
    /// import it from its tag package (`from <pkg>.<tag> import ...`).
    tag_decls: &'a [TagTypeDecl],
    /// Generated type names referenced as constructors (drives the example's
    /// `from <pkg> import ...`).
    referenced: BTreeSet<String>,
    /// First-use order retained for Fern's Markdown import writer.
    referenced_doc_order: Vec<String>,
    /// Referenced hoisted tag-types as `(tag module, type name)` — imported from
    /// their tag package on a separate line above the main import.
    referenced_tag: BTreeSet<(String, String)>,
    /// Tag-scoped first-use order retained for Markdown imports.
    referenced_tag_doc_order: Vec<(String, String)>,
    /// Whether any value is a datetime/date (drives an `import datetime`).
    uses_datetime: bool,
    /// Whether that first datetime value was rendered before the first tag-scoped
    /// type. Fern's Markdown writer lists imports in first-use order, so the
    /// stdlib line sits above the tag import in `mosip-esignet`'s snippets (whose
    /// `request_time` precedes the request model) and below it in
    /// `openbankingproject-ch-kundenbeziehung`'s (whose `purpose` enum precedes
    /// the `expiry_date`).
    datetime_precedes_tag_import: bool,
    /// The auth model, for the credential argument in the client instantiation.
    auth: &'a Auth,
    /// Whether the SDK has server environments — with them, the client
    /// instantiation drops the `base_url=...` line.
    has_environment: bool,
    /// Promoted global headers, shown as `tenant="YOUR_TENANT"` example lines
    /// before the auth credential in the client instantiation.
    global_headers: &'a [GlobalHeader],
    client_path_parameters: &'a [ClientPathParameter],
    /// Named types currently being expanded on the active path, so a recursive
    /// schema (a tree node, a recursive union) terminates instead of overflowing
    /// the stack (issue #84): a list of an ancestor type renders empty, matching
    /// Fern's `children=[]`.
    building: std::collections::HashSet<String>,
    /// The aliases whose target is being exampled with an example still
    /// unconsumed, each with that example: a union alias whose matching member
    /// is the alias itself (GitHub's REST description declares one) would
    /// otherwise expand forever.
    expanding_aliases: Vec<(String, String)>,
    /// Whether values are being rendered for README/reference snippets rather
    /// than executable method docstrings.
    documentation: bool,
    /// Whether the active Markdown snippet belongs to `reference.md`, whose
    /// literal and parameter-order writer differs from README examples.
    reference: bool,
    /// Whether a model field's own declared example is passed over, as Fern's IR
    /// fallback generator passes it over when its importer built no example.
    field_examples_ignored: bool,
    /// The keyword arguments whose value is the placeholder of an *unknown*
    /// (`typing.Any`) field, which the Markdown writers leave on one line.
    untyped_arguments: BTreeSet<String>,
    /// Whether Fern's isort pass files the SDK's own imports as first-party,
    /// which it does when ruff can find the package: always in the packaged tree
    /// (`src/<pkg>/`), and in the flat tree only for a package named `fern`,
    /// which ruff resolves inside Fern's `/fern` container (the same split
    /// `scaffolding_files` applies to `tests/test_aiohttp_autodetect.py`). A
    /// docstring example then puts a tag package's import in a group of its own
    /// above the root import; otherwise both are third-party, one sorted group.
    sdk_first_party: bool,
}

/// The slot a string value sits in, which decides its placeholder text: a named
/// field/param uses the name, a map value uses `"value"`, anything else `"string"`.
/// A path parameter is named too, and differs only in how a union is exampled
/// (see [`ExampleCtx::union_value`]).
#[derive(Clone, Copy)]
enum Slot<'a> {
    Named(&'a str),
    Path(&'a str),
    Map,
    Plain,
}

impl<'a> ExampleCtx<'a> {
    fn example_is_scalar(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::Named(name) => match self.find(name) {
                Some(TypeDecl::Alias(alias)) => self.example_is_scalar(&alias.target),
                _ => false,
            },
            _ => example_scalar(t),
        }
    }

    fn example_is_composite(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::List(_) | TypeRef::Set(_) | TypeRef::Dict(_, _) => true,
            TypeRef::Named(name) => match self.find(name) {
                Some(TypeDecl::Alias(alias)) => self.example_is_composite(&alias.target),
                _ => false,
            },
            _ => false,
        }
    }

    fn example_is_object(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::Optional(inner) => self.example_is_object(inner),
            TypeRef::Named(name) => match self.find(name) {
                Some(TypeDecl::Object(_)) => true,
                Some(TypeDecl::Alias(alias)) => self.example_is_object(&alias.target),
                _ => false,
            },
            _ => false,
        }
    }

    fn example_is_temporal(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::Optional(inner) => self.example_is_temporal(inner),
            TypeRef::Named(name) => match self.find(name) {
                Some(TypeDecl::Alias(alias)) => self.example_is_temporal(&alias.target),
                _ => false,
            },
            TypeRef::Primitive(Prim::Datetime | Prim::Date) => true,
            _ => false,
        }
    }

    fn value_from_example(&mut self, t: &TypeRef, example: &str) -> Option<Example> {
        if let TypeRef::Optional(inner) = t {
            return self.value_from_example(inner, example);
        }
        if let TypeRef::Named(name) = t {
            if let Some(TypeDecl::Alias(alias)) = self.find(name) {
                let expanding = (name.clone(), example.to_string());
                if self.expanding_aliases.contains(&expanding) {
                    return None;
                }
                let target = alias.target.clone();
                self.expanding_aliases.push(expanding);
                let value = self.value_from_example(&target, example);
                self.expanding_aliases.pop();
                return value;
            }
        }
        if matches!(t, TypeRef::Primitive(Prim::Datetime | Prim::Date)) {
            let mut value: String = serde_json::from_str(example).ok()?;
            self.note_datetime();
            let constructor = if matches!(t, TypeRef::Primitive(Prim::Datetime)) {
                value = datetime_example(&value);
                // The docstring writer spaces the date from the time; that
                // separator is the only character it changes.
                if !self.documentation {
                    value.replace_range(10..11, " ");
                }
                "datetime.datetime.fromisoformat"
            } else {
                "datetime.date.fromisoformat"
            };
            return Some(Example::Call(
                constructor.to_string(),
                vec![(None, Example::Atom(format!("{value:?}")))],
            ));
        }
        if self.example_is_scalar(t) {
            let literal = match t {
                TypeRef::Primitive(Prim::Float)
                    if !self.documentation && example.parse::<i64>().is_ok() =>
                {
                    format!("{example}.0")
                }
                TypeRef::Primitive(Prim::Bool | Prim::LiteralBool(_)) => match example {
                    "true" => "True".to_string(),
                    "false" => "False".to_string(),
                    _ => example.to_string(),
                },
                TypeRef::Primitive(Prim::Str | Prim::Bytes)
                    if self.reference && example.starts_with('\'') && example.ends_with('\'') =>
                {
                    let inner = &example[1..example.len() - 1];
                    format!("\"{}\"", inner.replace('"', "\\\""))
                }
                TypeRef::Primitive(Prim::Str | Prim::Bytes)
                    if self.documentation && example.starts_with('"') =>
                {
                    example.replace('\'', "\\'")
                }
                // A string holding `"` but no `'` is spelled single-quoted, as
                // Python's `repr` spells it: MockServer's file-store `content`
                // example is `'{"status":"ok"}'` in the golden.
                TypeRef::Primitive(Prim::Str | Prim::Bytes)
                    if !self.documentation && example.starts_with('"') =>
                {
                    serde_json::from_str::<serde_json::Value>(example)
                        .ok()
                        .filter(|value| {
                            value
                                .as_str()
                                .is_some_and(|s| s.contains('"') && !s.contains('\''))
                        })
                        .and_then(|value| crate::ir::example_literal(&value))
                        .unwrap_or_else(|| example.to_string())
                }
                _ => example.to_string(),
            };
            return Some(Example::Atom(literal));
        }
        if example.starts_with('{') && self.resolves_to_any(t) {
            let value: serde_json::Value = serde_json::from_str(example).ok()?;
            // A `null` member is no argument: Primula Tracker's student update
            // declares `{transition_date: …, transition_room_override_id: null}`
            // for its `Union[Any]` body, and the golden passes only the date.
            let value = fern_unknown_example(value)?;
            let rendered = example_from_json_as(value, !self.documentation);
            return Some(if self.reference {
                Example::Atom(rendered.flat())
            } else {
                rendered
            });
        }
        if let TypeRef::List(inner) | TypeRef::Set(inner) = t {
            let value = parse_example_value(example)?;
            // Handed a non-array, Fern's importer examples one item from nothing,
            // and an unknown item from nothing is no item: Short.io's opengraph
            // body is a list of `[name, value]` tuples (`List[List[Any]]`) whose
            // media example is the flat `["title", "My title"]`, and the golden
            // passes `[[], []]`, measured at Fern CLI 5.163.1.
            if !value.is_array() && self.resolves_to_any(inner) {
                return Some(if self.documentation {
                    Example::ReferenceList(Vec::new())
                } else {
                    Example::ExplicitList(Vec::new())
                });
            }
            let values: Vec<serde_json::Value> = serde_json::from_value(value).ok()?;
            let mut unique = Vec::with_capacity(values.len());
            for value in values {
                if matches!(t, TypeRef::List(_))
                    || !self.example_is_temporal(inner)
                    || !unique.contains(&value)
                {
                    unique.push(value);
                }
            }
            let items = unique
                .into_iter()
                .map(|value| {
                    if value.is_null() {
                        return self.value(inner, Slot::Plain);
                    }
                    let literal = value.to_string();
                    self.value_from_example(inner, &literal)
                        .unwrap_or_else(|| example_from_json_as(value, !self.documentation))
                })
                .collect();
            return Some(if self.documentation {
                Example::ReferenceList(items)
            } else {
                Example::ExplicitList(items)
            });
        }
        if let TypeRef::Union(variants) = t {
            let value: serde_json::Value = parse_example_value(example)?;
            if value
                .as_object()
                .is_some_and(|object| object.len() == 1 && object.contains_key("$ref"))
            {
                return variants
                    .iter()
                    .find(|variant| matches!(variant, TypeRef::List(_) | TypeRef::Set(_)))
                    .map(|variant| self.value(variant, Slot::Plain));
            }
            // Fern's importer builds the example for the alternative its
            // heuristic scores highest, keeping only what that alternative
            // declares; the SDK generator then renders the result with the first
            // alternative that accepts it. MockServer's `debugMismatch` body
            // `{method, path}` scores best as `OpenAPIDefinition`, which declares
            // neither, so the golden renders the empty result as `HttpRequest()`.
            let projected = self
                .fern_union_example(variants, &value, &mut Vec::new())
                .filter(|projected| *projected != value);
            let (value, example) = match projected {
                Some(projected) => {
                    let text = projected.to_string();
                    (projected, std::borrow::Cow::Owned(text))
                }
                None => (value, std::borrow::Cow::Borrowed(example)),
            };
            for variant in variants {
                if self.expands_back(variant, &example) {
                    continue;
                }
                if self.example_matches_type(variant, &value) {
                    return self.value_from_example(variant, &example);
                }
            }
            return None;
        }
        if let TypeRef::Named(name) = t {
            if let Some(TypeDecl::Object(object)) = self.find(name) {
                let fields = self.object_fields(object);
                if fields.is_empty() {
                    self.record_ref(name);
                    return Some(Example::Call(name.clone(), Vec::new()));
                }
                let values = parse_example_value(example)?;
                let values = values.as_object()?;
                self.record_ref(name);
                let args = self.example_call_args(fields, values);
                return Some(Example::Call(name.clone(), args));
            }
        }
        if self.example_is_composite(t) && (example.starts_with('[') || example.starts_with('{')) {
            let mut value: serde_json::Value = serde_json::from_str(example).ok()?;
            // A free-form map's values are unknowns, pruned the way Fern prunes
            // one: MockServer's Pact import `body: {orders: []}` is `{}`.
            if let (TypeRef::Dict(_, inner), serde_json::Value::Object(fields)) = (t, &mut value) {
                if self.resolves_to_any(inner) {
                    *fields = std::mem::take(fields)
                        .into_iter()
                        .filter_map(|(key, field)| {
                            fern_unknown_example(field).map(|field| (key, field))
                        })
                        .collect();
                }
            }
            // An empty map carries no example value, so Fern synthesizes one as it
            // does for an empty array (`dot-ai`'s `metadata: {}` is exampled
            // `{"key": "value"}`).
            if matches!(t, TypeRef::Dict(_, _))
                && value.as_object().is_some_and(serde_json::Map::is_empty)
            {
                return None;
            }
            // A map of models constructs each value: MockServer's chaos stage
            // `profiles: {api.example.com: {errorStatus: 500, …}}` is
            // `{"api.example.com": HttpChaosProfile(error_status=500, …)}`.
            if let (TypeRef::Dict(_, inner), serde_json::Value::Object(fields)) = (t, &value) {
                if self.resolves_to_object(inner) {
                    let pairs = fields
                        .iter()
                        .map(|(key, field)| {
                            let rendered = self
                                .value_from_example(inner, &field.to_string())
                                .unwrap_or_else(|| {
                                    example_from_json_as(field.clone(), !self.documentation)
                                });
                            (key.clone(), rendered)
                        })
                        .collect();
                    return Some(if self.reference {
                        Example::ReferenceDict(pairs)
                    } else {
                        Example::Dict(pairs)
                    });
                }
            }
            if let (true, TypeRef::Dict(_, inner)) = (self.reference, t) {
                let serde_json::Value::Object(fields) = value else {
                    return None;
                };
                // `reference.md` writes a free-form value on one line and a
                // typed one by its type: MockServer's HAR `log` is one line,
                // while its WASM test's `Dict[str, List[str]]` headers wrap
                // `["acme"]` as a list.
                let unknown = self.resolves_to_any(inner);
                return Some(Example::ReferenceDict(
                    fields
                        .into_iter()
                        .map(|(key, value)| {
                            let rendered = if unknown {
                                Example::Atom(example_from_json_as(value, false).flat())
                            } else {
                                self.value_from_example(inner, &value.to_string())
                                    .unwrap_or_else(|| example_from_json_as(value, false))
                            };
                            (key, rendered)
                        })
                        .collect(),
                ));
            }
            return Some(example_from_json_as(value, !self.documentation));
        }
        let TypeRef::Named(name) = t else {
            return None;
        };
        match self.find(name)? {
            TypeDecl::Object(_) => None,
            TypeDecl::Alias(alias) => {
                let target = alias.target.clone();
                self.value_from_example(&target, example)
            }
            TypeDecl::Enum(enum_type) => {
                let value = example.strip_prefix('"')?.strip_suffix('"')?;
                let member = enum_type
                    .members
                    .iter()
                    .find(|member| member.value == value)?;
                if self.enum_type == EnumType::Literals {
                    return Some(Example::Atom(literal_enum_value(&member.value)));
                }
                let member_name = member.name.clone();
                self.record_ref(name);
                Some(Example::Atom(format!("{name}.{member_name}")))
            }
            TypeDecl::DiscriminatedUnion(union) => {
                let union = union.clone();
                self.union_value_from_example(&union, example)
            }
        }
    }

    /// A discriminated union's example: the wrapper of the member the example's
    /// discriminant value selects, constructed from the example's values as a
    /// model is. Supabase's `DiskRequestBody` example `{attributes: {type: gp3,
    /// size_gb: 100, iops: 3000, throughput_mibps: 125}}` is
    /// `DiskRequestBodyAttributes_Gp3(iops=3000, size_gb=100,
    /// throughput_mibps=125)` in Fern's example, and LangChain's `post_a2a` parts
    /// construct `…PartsItem_Text(text=…)` and `…PartsItem_Data(data=…)`. Both
    /// are unions of inline alternatives; a member flattened from a `$ref`
    /// keeps the synthesized example, as HelixDB's `QueryRequest_Read` does
    /// beside a declared request example. So do a wrapper holding a union
    /// whole, a union with a shared base, and a member whose tag is an enum of
    /// its own.
    fn union_value_from_example(
        &mut self,
        union: &crate::ir::DiscriminatedUnion,
        example: &str,
    ) -> Option<Example> {
        if !union.base_fields.is_empty() {
            return None;
        }
        let values = parse_example_value(example)?;
        let values = values.as_object()?;
        let discriminant = values.get(&union.discriminant_property)?.as_str()?;
        let member = union
            .members
            .iter()
            .find(|member| member.discriminant == discriminant)?;
        // A tag the synthesized example writes back (see `value`) is not
        // measured from an example, so that member keeps the synthesized one.
        let writes_tag = member.discriminant_index.is_some()
            && member
                .source
                .as_deref()
                .map(|source| naming::child_class_name(source, &union.discriminant_property))
                .is_some_and(|enum_name| matches!(self.find(&enum_name), Some(TypeDecl::Enum(_))));
        if member.wrapped || writes_tag || member.source.is_some() {
            return None;
        }
        let fields = member
            .fields
            .iter()
            .map(|field| ExampleField {
                py_name: field.py_name.clone(),
                wire_name: field.wire_name.clone(),
                type_ref: field.type_ref.clone(),
                requirement: match (field.spec_required, field.optional) {
                    (true, false) => Requirement::Required,
                    (true, true) => Requirement::RequiredNullable,
                    (false, _) => Requirement::Optional,
                },
                example: field.example.clone(),
                parent_example: false,
                deprecated: field.deprecated,
                lengths: (field.min_length, field.max_length),
            })
            .collect();
        self.record_ref(&member.class_name);
        let args = self.example_call_args(fields, values);
        Some(Example::Call(member.class_name.clone(), args))
    }

    /// The keyword arguments of a model constructor built from an example's
    /// `values`, one per field Fern's example writers pass: a required field
    /// whatever the example holds, and an optional one the example gives a value.
    fn example_call_args(
        &mut self,
        fields: Vec<ExampleField>,
        values: &serde_json::Map<String, serde_json::Value>,
    ) -> Vec<(Option<String>, Example)> {
        let documentation = self.documentation;
        fields
            .into_iter()
            // An example value of `{}` selects nothing: Fern renders no
            // argument for an optional model field the example leaves
            // empty (`mosip-esignet`'s `claims.id_token: {}`).
            // Nor does `null`, but in the Markdown writers, which write a
            // required nullable field's `null` as `None`; and an optional
            // field whose schema is deprecated is left out even where the
            // example names it.
            .filter(|field| {
                let value = values.get(&field.wire_name);
                field.requirement == Requirement::Required
                    || field.requirement == Requirement::RequiredNullable
                        && documentation
                        && value.is_some_and(serde_json::Value::is_null)
                    || !field.deprecated
                        && value.is_some_and(|value| {
                            !value.is_null()
                                && !value.as_object().is_some_and(serde_json::Map::is_empty)
                        })
            })
            .filter_map(
                |ExampleField {
                     py_name,
                     wire_name,
                     type_ref,
                     requirement,
                     ..
                 }| {
                    let recorded = self.recorded();
                    let rendered = match values.get(&wire_name) {
                        Some(serde_json::Value::Null)
                            if self.documentation
                                && requirement == Requirement::RequiredNullable =>
                        {
                            Example::Atom("None".to_string())
                        }
                        Some(serde_json::Value::Null) => {
                            self.value(&type_ref, Slot::Named(&wire_name))
                        }
                        Some(serde_json::Value::String(value))
                            if self.example_is_temporal(&type_ref)
                                && value.starts_with("2000-01-23T04:56:07") =>
                        {
                            self.value(&type_ref, Slot::Named(&wire_name))
                        }
                        // A YAML timestamp is a date to Fern's parser, so it
                        // is no example for a field that is not temporal; see
                        // [`yaml_resolves_as_timestamp`].
                        Some(serde_json::Value::String(value))
                            if self
                                .yaml_unquoted_timestamps
                                .is_some_and(|unquoted| unquoted.contains(value))
                                && !self.example_is_temporal(&type_ref)
                                && yaml_resolves_as_timestamp(value) =>
                        {
                            self.value(&type_ref, Slot::Named(&wire_name))
                        }
                        // An empty array carries no example value, so Fern
                        // synthesizes one from the field name rather than
                        // rendering `[]` (`mosip-esignet`'s
                        // `permittedAuthorizeScopes: []`).
                        Some(serde_json::Value::Array(items)) if items.is_empty() => {
                            self.value(&type_ref, Slot::Named(&wire_name))
                        }
                        Some(value) => {
                            let literal = value.to_string();
                            self.value_from_example(&type_ref, &literal)
                                .unwrap_or_else(|| {
                                    example_from_json_as(value.clone(), !self.documentation)
                                })
                        }
                        _ => self.value(&type_ref, Slot::Named(&wire_name)),
                    };
                    // An optional field whose example narrows to an empty model
                    // is no argument (measured at fern-python-sdk 5.41.3):
                    // MockServer's baseline `Expectation` gives `httpRequest`
                    // `{method, path}`, which its `RequestDefinition` union keeps
                    // as an empty `OpenAPIDefinition`, and the golden passes only
                    // `http_response`.
                    if requirement == Requirement::Optional
                        && matches!(&rendered, Example::Call(_, args) if args.is_empty())
                    {
                        // Nor does the model it would have constructed import.
                        self.restore_recorded(recorded);
                        return None;
                    }
                    Some((Some(py_name), rendered))
                },
            )
            .collect()
    }

    /// Whether `t` leads, through aliases alone, back into an alias this example
    /// is already being expanded under: a union member that is the union itself
    /// again, which could only repeat the expansion it sits in.
    fn expands_back(&self, t: &TypeRef, example: &str) -> bool {
        let mut followed: Vec<&str> = Vec::new();
        let mut current = t;
        while let TypeRef::Named(name) = current {
            if followed.contains(&name.as_str()) {
                return false;
            }
            if self
                .expanding_aliases
                .iter()
                .any(|(alias, text)| alias == name && text == example)
            {
                return true;
            }
            let Some(TypeDecl::Alias(alias)) = self.find(name) else {
                return false;
            };
            followed.push(name);
            current = &alias.target;
        }
        false
    }

    /// The example Fern's importer keeps for an undiscriminated union: the
    /// alternative [`Self::fern_example_score`] ranks highest (the first, on a
    /// tie), with `value` narrowed to what that alternative declares. `None`
    /// where the narrowing is not modelled, leaving `value` as it is.
    ///
    /// `visiting` holds the named types entered at this level of `value`: a
    /// union reaching itself again through its aliases before consuming any of
    /// the value is a cycle (GitHub's REST description declares such unions),
    /// which scores nothing and narrows nothing rather than recursing forever.
    fn fern_union_example(
        &self,
        variants: &[TypeRef],
        value: &serde_json::Value,
        visiting: &mut Vec<String>,
    ) -> Option<serde_json::Value> {
        let mut best: Option<(&TypeRef, i64)> = None;
        for variant in variants {
            let score = self.fern_example_score(variant, value, visiting);
            if best.is_none_or(|(_, top)| score > top) {
                best = Some((variant, score));
            }
        }
        self.fern_example_projection(best?.0, value, visiting)
    }

    /// Fern's `calcExampleHeuristic`: how well `value` fits `t`, counting each
    /// declared property the value supplies up and each it omits down.
    fn fern_example_score(
        &self,
        t: &TypeRef,
        value: &serde_json::Value,
        visiting: &mut Vec<String>,
    ) -> i64 {
        match t {
            TypeRef::Optional(inner) => self.fern_example_score(inner, value, visiting),
            TypeRef::Literal(literals) => {
                i64::from(
                    value
                        .as_str()
                        .is_some_and(|text| literals.iter().any(|literal| literal == text)),
                ) * 5
            }
            TypeRef::Named(name) if visiting.contains(name) => 0,
            TypeRef::Named(name) => {
                visiting.push(name.clone());
                let score = match self.find(name) {
                    Some(TypeDecl::Enum(decl)) => {
                        i64::from(value.as_str().is_some_and(|text| {
                            decl.members.iter().any(|member| member.value == text)
                        })) * 5
                    }
                    Some(TypeDecl::Object(object)) => {
                        let Some(values) = value.as_object() else {
                            return 0;
                        };
                        self.object_fields(object)
                            .iter()
                            .map(
                                |ExampleField {
                                     wire_name,
                                     type_ref,
                                     ..
                                 }| {
                                    match values.get(wire_name).filter(|field| !field.is_null()) {
                                        Some(field) => {
                                            1 + self.fern_example_score(
                                                type_ref,
                                                field,
                                                &mut Vec::new(),
                                            )
                                        }
                                        None => -1,
                                    }
                                },
                            )
                            .sum()
                    }
                    Some(TypeDecl::Alias(alias)) => {
                        self.fern_example_score(&alias.target, value, visiting)
                    }
                    _ => 0,
                };
                visiting.pop();
                score
            }
            TypeRef::Union(variants) => variants
                .iter()
                .map(|variant| self.fern_example_score(variant, value, visiting))
                .max()
                .unwrap_or(0),
            TypeRef::List(inner) | TypeRef::Set(inner) => value.as_array().map_or(0, |items| {
                items
                    .iter()
                    .map(|item| self.fern_example_score(inner, item, &mut Vec::new()))
                    .sum()
            }),
            TypeRef::Dict(_, inner) => value.as_object().map_or(0, |values| {
                values
                    .values()
                    .map(|item| self.fern_example_score(inner, item, &mut Vec::new()))
                    .sum()
            }),
            TypeRef::Primitive(Prim::Any) => 0,
            TypeRef::Primitive(_) if value.is_null() => 0,
            TypeRef::Primitive(_) => {
                if self.example_matches_type(t, value) {
                    1
                } else {
                    -1
                }
            }
        }
    }

    /// `value` as Fern's importer builds it for `t`: an object keeps only the
    /// properties it declares, and an array handed a non-array builds one item
    /// with no example — MockServer's `headers: {Host: [...]}` against
    /// `List[KeyToMultiValueZeroItem]` is `[KeyToMultiValueZeroItem()]`.
    fn fern_example_projection(
        &self,
        t: &TypeRef,
        value: &serde_json::Value,
        visiting: &mut Vec<String>,
    ) -> Option<serde_json::Value> {
        match t {
            TypeRef::Optional(inner) if !value.is_null() => {
                self.fern_example_projection(inner, value, visiting)
            }
            TypeRef::Named(name) if visiting.contains(name) => None,
            TypeRef::Named(name) => {
                visiting.push(name.clone());
                let projected = self.fern_named_projection(name, value, visiting);
                visiting.pop();
                projected
            }
            TypeRef::Union(variants) => self.fern_union_example(variants, value, visiting),
            TypeRef::List(inner) | TypeRef::Set(inner) => match value.as_array() {
                Some(items) => items
                    .iter()
                    .map(|item| self.fern_example_projection(inner, item, &mut Vec::new()))
                    .collect::<Option<Vec<_>>>()
                    .map(serde_json::Value::Array),
                None if self.resolves_to_object(inner) => {
                    let item = self.fern_example_projection(
                        inner,
                        &serde_json::Value::Object(serde_json::Map::new()),
                        &mut Vec::new(),
                    )?;
                    Some(serde_json::Value::Array(vec![item]))
                }
                None => None,
            },
            TypeRef::Dict(_, inner) => {
                let values = value.as_object()?;
                values
                    .iter()
                    .map(|(key, item)| {
                        self.fern_example_projection(inner, item, &mut Vec::new())
                            .map(|item| (key.clone(), item))
                    })
                    .collect::<Option<serde_json::Map<_, _>>>()
                    .map(serde_json::Value::Object)
            }
            _ => Some(value.clone()),
        }
    }

    /// [`Self::fern_example_projection`] of the named type `name`.
    fn fern_named_projection(
        &self,
        name: &str,
        value: &serde_json::Value,
        visiting: &mut Vec<String>,
    ) -> Option<serde_json::Value> {
        match self.find(name) {
            Some(TypeDecl::Object(object)) => {
                let fields = self.object_fields(object);
                let empty = serde_json::Map::new();
                let values = value.as_object().unwrap_or(&empty);
                let mut kept = serde_json::Map::new();
                for ExampleField {
                    wire_name,
                    type_ref,
                    requirement,
                    ..
                } in fields
                {
                    let required = requirement == Requirement::Required;
                    match values.get(&wire_name).filter(|field| !field.is_null()) {
                        Some(field) => {
                            let field =
                                self.fern_example_projection(&type_ref, field, &mut Vec::new())?;
                            kept.insert(wire_name, field);
                        }
                        // A required property the example omits is built
                        // with no example — a string is its own name, as
                        // MockServer's `ExpectationId(id="id")` shows.
                        None if required && self.resolves_to_string(&type_ref) => {
                            kept.insert(wire_name.clone(), serde_json::Value::String(wire_name));
                        }
                        None if required => return None,
                        None => {}
                    }
                }
                Some(serde_json::Value::Object(kept))
            }
            Some(TypeDecl::Alias(alias)) => {
                self.fern_example_projection(&alias.target, value, visiting)
            }
            _ => Some(value.clone()),
        }
    }

    fn example_matches_type(&self, t: &TypeRef, value: &serde_json::Value) -> bool {
        self.example_matches_type_through(t, value, &mut Vec::new())
    }

    /// [`Self::example_matches_type`], with the aliases already followed: none
    /// of its arms reads into `value`, so an alias met again is a union that
    /// names itself (GitHub's REST description declares such unions) and takes
    /// nothing more than its other members do.
    fn example_matches_type_through(
        &self,
        t: &TypeRef,
        value: &serde_json::Value,
        aliases: &mut Vec<String>,
    ) -> bool {
        match t {
            TypeRef::Optional(inner) => {
                value.is_null() || self.example_matches_type_through(inner, value, aliases)
            }
            TypeRef::Named(name) => match self.find(name) {
                // A model with no fields takes any example: Milvus's `upsert`
                // example sends `data` as a list, and Fern still renders the
                // union's empty-object member, `PostV1VectorUpsertRequestDataZero()`.
                Some(TypeDecl::Object(object)) if self.object_fields(object).is_empty() => true,
                Some(TypeDecl::Object(object)) => value.as_object().is_some_and(|values| {
                    let fields = self.object_fields(object);
                    fields
                        .iter()
                        .filter(|field| field.requirement == Requirement::Required)
                        .all(|field| values.contains_key(&field.wire_name))
                        && values
                            .keys()
                            .all(|key| fields.iter().any(|field| field.wire_name == *key))
                }),
                Some(TypeDecl::Alias(_)) if aliases.contains(name) => false,
                Some(TypeDecl::Alias(alias)) => {
                    aliases.push(name.clone());
                    let matches = self.example_matches_type_through(&alias.target, value, aliases);
                    aliases.pop();
                    matches
                }
                // An enum takes only its own values: Primula Tracker's
                // `intended_start_date` is `Union[<TBD enum>, datetime.date]`, and
                // the golden constructs its `2030-09-01` example as the date.
                Some(TypeDecl::Enum(decl)) => value
                    .as_str()
                    .is_some_and(|text| decl.members.iter().any(|member| member.value == text)),
                _ => true,
            },
            TypeRef::Union(variants) => variants
                .iter()
                .any(|variant| self.example_matches_type_through(variant, value, aliases)),
            TypeRef::List(_) | TypeRef::Set(_) => value.is_array(),
            TypeRef::Dict(_, _) => value.is_object(),
            TypeRef::Primitive(Prim::Str | Prim::Bytes | Prim::Datetime | Prim::Date)
            | TypeRef::Literal(_) => value.is_string(),
            TypeRef::Primitive(Prim::Int | Prim::Long) => value.as_i64().is_some(),
            TypeRef::Primitive(Prim::Float) => value.is_number(),
            TypeRef::Primitive(Prim::Bool) => value.is_boolean(),
            TypeRef::Primitive(Prim::LiteralBool(expected)) => value.as_bool() == Some(*expected),
            TypeRef::Primitive(Prim::Any) => true,
        }
    }

    /// Look up a generated type by name — a package-root type, else a hoisted
    /// tag-scoped one (so an example can construct an inline-hoisted model).
    fn find(&self, name: &str) -> Option<&'a TypeDecl> {
        self.types.iter().find(|d| d.name() == name).or_else(|| {
            self.tag_decls
                .iter()
                .find_map(|tt| (tt.decl.name() == name).then_some(&tt.decl))
        })
    }

    /// Record the first datetime value, and whether it preceded every tag import.
    fn note_datetime(&mut self) {
        if !self.uses_datetime {
            self.datetime_precedes_tag_import = self.referenced_tag_doc_order.is_empty();
        }
        self.uses_datetime = true;
    }

    /// Record a referenced constructor: a tag-scoped type is tracked with its tag
    /// module (imported separately), a package-root type in the main import set.
    fn record_ref(&mut self, name: &str) {
        if let Some(tt) = self.tag_decls.iter().find(|tt| {
            tt.decl.name() == name
                || matches!(&tt.decl, TypeDecl::DiscriminatedUnion(union)
                    if union.members.iter().any(|member| member.class_name == name))
        }) {
            let entry = (tt.module.clone(), name.to_string());
            if self.referenced_tag.insert(entry.clone()) {
                self.referenced_tag_doc_order.push(entry);
            }
        } else {
            let name = name.to_string();
            if self.referenced.insert(name.clone()) {
                self.referenced_doc_order.push(name);
            }
        }
    }

    /// Whether a type reaches, through optionals/aliases/unions, a named type
    /// currently being expanded — i.e. constructing an example for it would
    /// re-enter an ancestor and loop. Drives the empty-list break for recursive
    /// schemas (issue #84).
    fn resolves_to_building(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::Named(n) => {
                self.building.contains(n)
                    || matches!(self.find(n), Some(TypeDecl::Alias(a)) if self.resolves_to_building(&a.target))
            }
            TypeRef::Optional(inner) => self.resolves_to_building(inner),
            TypeRef::Union(variants) => variants.iter().any(|v| self.resolves_to_building(v)),
            _ => false,
        }
    }

    /// Whether a type resolves (through aliases/optionals) to a generated model.
    fn resolves_to_object(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::Optional(inner) => self.resolves_to_object(inner),
            TypeRef::Named(n) => match self.find(n) {
                Some(TypeDecl::Object(_)) => true,
                Some(TypeDecl::Alias(a)) => self.resolves_to_object(&a.target),
                _ => false,
            },
            _ => false,
        }
    }

    /// Whether a type resolves (through aliases/optionals) to an unknown value —
    /// which Fern cannot example, so a map of it renders `{}`.
    fn resolves_to_any(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::Primitive(Prim::Any) => true,
            TypeRef::Optional(inner) => self.resolves_to_any(inner),
            TypeRef::Named(n) => match self.find(n) {
                Some(TypeDecl::Alias(a)) => self.resolves_to_any(&a.target),
                _ => false,
            },
            _ => false,
        }
    }

    /// Whether the type is, after alias resolution, a union whose every member
    /// is unknown (`Union[typing.Any]`).
    fn resolves_to_unknown_union(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::Union(variants) => {
                !variants.is_empty() && variants.iter().all(|variant| self.resolves_to_any(variant))
            }
            TypeRef::Named(name) => match self.find(name) {
                Some(TypeDecl::Alias(alias)) => self.resolves_to_unknown_union(&alias.target),
                _ => false,
            },
            _ => false,
        }
    }

    /// Whether the type is, after alias resolution, a union whose first
    /// alternative is unknown, so that its example is the unknown's placeholder.
    fn resolves_to_union_led_by_unknown(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::Union(variants) => variants
                .first()
                .is_some_and(|first| matches!(first, TypeRef::Primitive(Prim::Any))),
            TypeRef::Named(name) => match self.find(name) {
                Some(TypeDecl::Alias(alias)) => {
                    self.resolves_to_union_led_by_unknown(&alias.target)
                }
                _ => false,
            },
            _ => false,
        }
    }

    /// Whether the type is, after alias resolution, a map whose value slot is
    /// *directly* free-form (`Dict[str, Any]`) — the shape [`Self::value`]
    /// renders as Fern's `{"key": "value"}` placeholder.
    fn resolves_to_unknown_map(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::Optional(inner) => self.resolves_to_unknown_map(inner),
            TypeRef::Named(name) => match self.find(name) {
                Some(TypeDecl::Alias(alias)) => self.resolves_to_unknown_map(&alias.target),
                _ => false,
            },
            other => is_unknown_map(other),
        }
    }

    /// Whether Fern can synthesize no example for this type at all. Its
    /// `unknown` kind supplies none, and an object one of whose *required*
    /// fields is a bare unknown inherits that: oSPARC's
    /// `ProjectInputUpdate.value` declares only a title and a description, and
    /// the golden's worked call for the array body that holds it is
    /// `request=[]` — where otoroshi's `PatchItem`, whose `value` is optional,
    /// is exampled field by field.
    fn example_has_no_value(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::Optional(inner) => self.example_has_no_value(inner),
            TypeRef::Named(name) => match self.find(name) {
                Some(TypeDecl::Object(object)) => object.fields.iter().any(|field| {
                    field.spec_required
                        && !field.optional
                        && matches!(field.type_ref, TypeRef::Primitive(Prim::Any))
                }),
                Some(TypeDecl::Alias(alias)) => self.example_has_no_value(&alias.target),
                _ => false,
            },
            _ => false,
        }
    }

    fn resolves_to_string(&self, t: &TypeRef) -> bool {
        match t {
            TypeRef::Primitive(Prim::Str) => true,
            TypeRef::Optional(inner) => self.resolves_to_string(inner),
            TypeRef::Named(name) => match self.find(name) {
                Some(TypeDecl::Alias(alias)) => self.resolves_to_string(&alias.target),
                _ => false,
            },
            _ => false,
        }
    }

    /// Synthesize an example value for a type in a given slot.
    fn value(&mut self, t: &TypeRef, slot: Slot) -> Example {
        match t {
            TypeRef::Primitive(Prim::Str) => {
                let s = match slot {
                    Slot::Named(name) | Slot::Path(name) => name,
                    Slot::Map => "value",
                    Slot::Plain => "string",
                };
                Example::Atom(format!("\"{s}\""))
            }
            TypeRef::Primitive(Prim::Bytes) => Example::Atom(
                if self.reference {
                    "\"string\""
                } else {
                    "b\"string\""
                }
                .to_string(),
            ),
            TypeRef::Primitive(Prim::Int) => Example::Atom("1".to_string()),
            TypeRef::Primitive(Prim::Long) => Example::Atom("1000000".to_string()),
            TypeRef::Primitive(Prim::Float) => Example::Atom("1.1".to_string()),
            TypeRef::Primitive(Prim::Bool) => Example::Atom("True".to_string()),
            TypeRef::Primitive(Prim::LiteralBool(value)) => {
                Example::Atom(if *value { "True" } else { "False" }.to_string())
            }
            TypeRef::Primitive(Prim::Datetime) => {
                self.note_datetime();
                Example::Call(
                    "datetime.datetime.fromisoformat".to_string(),
                    vec![(
                        None,
                        Example::Atom(
                            if self.documentation {
                                "\"2024-01-15T09:30:00+00:00\""
                            } else {
                                "\"2024-01-15 09:30:00+00:00\""
                            }
                            .to_string(),
                        ),
                    )],
                )
            }
            TypeRef::Primitive(Prim::Date) => {
                self.note_datetime();
                Example::Call(
                    "datetime.date.fromisoformat".to_string(),
                    vec![(None, Example::Atom("\"2023-01-15\"".to_string()))],
                )
            }
            TypeRef::Primitive(Prim::Any) => Example::Dict(vec![(
                "key".to_string(),
                Example::Atom("\"value\"".to_string()),
            )]),
            TypeRef::Optional(inner) => self.value(inner, slot),
            TypeRef::List(inner) | TypeRef::Set(inner) => {
                // A list whose element is a type currently being expanded would
                // recurse forever (a tree node, a recursive union); Fern renders it
                // empty (`children=[]`), which also terminates the walk (issue #84).
                // The element inherits the list's slot, so a `List[str]` field's
                // example uses the field name (`all_field=["all_field"]`).
                if self.resolves_to_building(inner)
                    || self.resolves_to_any(inner)
                    || self.example_has_no_value(inner)
                {
                    if self.documentation {
                        Example::ReferenceList(vec![])
                    } else {
                        Example::List(vec![])
                    }
                } else {
                    // Fern's IR fallback writes two items where its importer's
                    // example writes one.
                    let items =
                        vec![self.value(inner, slot); 1 + usize::from(self.field_examples_ignored)];
                    if self.documentation {
                        Example::ReferenceList(items)
                    } else {
                        Example::List(items)
                    }
                }
            }
            TypeRef::Dict(_, v) => {
                // A *directly* free-form value (`Optional[Any]`/`Any` — a bare-object
                // alias like bunq's `AttachmentPublic`) takes the `{"key": "value"}`
                // example Fern gives a bare `Any`. A value that only *resolves* to Any
                // through a named alias (a documented-unknown map) renders empty.
                let directly_any = matches!(v.as_ref(), TypeRef::Primitive(Prim::Any))
                    || matches!(
                        v.as_ref(),
                        TypeRef::Optional(inner) if matches!(inner.as_ref(), TypeRef::Primitive(Prim::Any))
                    );
                if directly_any {
                    let pairs = vec![("key".to_string(), Example::Atom("\"value\"".to_string()))];
                    if self.reference {
                        Example::ReferenceDict(pairs)
                    } else {
                        Example::Dict(pairs)
                    }
                } else if self.resolves_to_any(v) || matches!(v.as_ref(), TypeRef::Optional(_)) {
                    // An *optional* map value has no value to show — VolView's
                    // `values: Dict[str, Optional[RunTaskRequestValuesValue]]`
                    // (its `oneOf` carries a `null` member) is exampled `{}` —
                    // where a required one takes the `{"key": ...}` pair
                    // `squareup.com`'s `team_members` shows.
                    if self.reference {
                        Example::ReferenceDict(vec![])
                    } else {
                        Example::Dict(vec![])
                    }
                } else {
                    let val = self.value(v, Slot::Map);
                    let pairs = vec![("key".to_string(), val)];
                    if self.reference {
                        Example::ReferenceDict(pairs)
                    } else {
                        Example::Dict(pairs)
                    }
                }
            }
            TypeRef::Union(variants) => self.union_value(variants, slot),
            TypeRef::Literal(values) => {
                Example::Atom(format!("\"{}\"", values.first().map_or("", String::as_str)))
            }
            TypeRef::Named(name) => self.named_value(name, slot),
        }
    }

    /// The example for a union: the first alternative that names concrete values,
    /// else its first alternative. A free-form scalar can only supply a
    /// placeholder, so Fern reaches past helios' `Union[Uint, BlockTag, Hash32]`
    /// to `BlockTag`'s `"earliest"`. In a path parameter — helios' `block_id` —
    /// that is the plain string the union's own annotation accepts; anywhere else
    /// it is the enum member a `BlockTag` argument would take.
    fn union_value(&mut self, variants: &[TypeRef], slot: Slot<'_>) -> Example {
        if let Some(TypeRef::Named(name)) = variants.first() {
            if matches!(self.find(name), Some(TypeDecl::Enum(_))) {
                let name = name.clone();
                return self.named_value(&name, slot);
            }
        }
        // A later enum alternative is exampled by its first member (marimo's
        // `totalRows`, `Union[float, MarimoTableDataTotalRowsOne]`, is
        // `MarimoTableDataTotalRowsOne.TOO_MANY`), except in a path, where the
        // value below is its plain string. So it is where a `str` alternative
        // before the enum already holds that value: a query parameter's
        // `anyOf: [string, $ref Weave]` is exampled `"plain"`, not `Weave.PLAIN`.
        if !matches!(slot, Slot::Path(_)) {
            if let Some((index, name)) =
                variants
                    .iter()
                    .enumerate()
                    .skip(1)
                    .find_map(|(index, variant)| match variant {
                        TypeRef::Named(name)
                            if matches!(self.find(name), Some(TypeDecl::Enum(_))) =>
                        {
                            Some((index, name.clone()))
                        }
                        _ => None,
                    })
            {
                if !variants[..index].contains(&TypeRef::Primitive(Prim::Str)) {
                    return self.named_value(&name, slot);
                }
            }
        }
        if let Some(value) = variants
            .iter()
            .skip(1)
            .find_map(|variant| self.enumerated_value(variant))
        {
            return Example::Atom(format!("{value:?}"));
        }
        match variants.first() {
            Some(first) => {
                let first = first.clone();
                self.value(&first, slot)
            }
            None => Example::Dict(vec![(
                "key".to_string(),
                Example::Atom("\"value\"".to_string()),
            )]),
        }
    }

    /// The first value an alternative spells out, when it spells any out.
    fn enumerated_value(&self, t: &TypeRef) -> Option<String> {
        match t {
            TypeRef::Literal(values) => values.first().cloned(),
            TypeRef::Named(name) => match self.find(name) {
                Some(TypeDecl::Enum(declaration)) => declaration
                    .members
                    .first()
                    .map(|member| member.value.clone()),
                _ => None,
            },
            _ => None,
        }
    }

    /// The example for a named type: an object is constructed from its
    /// spec-required fields (base classes first); an alias resolves to its target.
    fn named_value(&mut self, name: &str, slot: Slot) -> Example {
        // A named type already on the active path is a cycle. Recursion normally
        // terminates at the empty-list break (a recursive field is a list), so this
        // is only reached by a direct (non-list) self-reference; break with `None`
        // rather than overflow the stack (issue #84).
        if self.building.contains(name) {
            return Example::Atom("None".to_string());
        }
        let inserted = self.building.insert(name.to_string());
        let result = self.named_value_inner(name, slot);
        if inserted {
            self.building.remove(name);
        }
        result
    }

    /// The body of [`named_value`], wrapped by the cycle-guard bookkeeping.
    fn named_value_inner(&mut self, name: &str, slot: Slot) -> Example {
        match self.find(name) {
            Some(TypeDecl::Object(obj)) => {
                self.record_ref(name);
                // Include inherited fields (pydantic construction takes them too),
                // base classes first, then the object's own fields.
                let fields = self.object_fields(obj);
                // A required-but-nullable field is shown as a top-level body
                // field is (see `build_example_inner`): by its declared example,
                // else as `None` in the Markdown writers alone. An unknown-typed
                // one too: marimo-plugins' chat message item documents
                // `content=None` and `metadata=None` (`anyOf: [{}, null]`) in
                // `reference.md`, and neither in the docstring.
                let documentation = self.documentation;
                let fallback = self.field_examples_ignored;
                let nullable_shown = |field: &ExampleField| {
                    field.requirement == Requirement::RequiredNullable
                        && !fallback
                        && (field.example.is_some() || documentation)
                };
                let args = fields
                    .into_iter()
                    .filter(|field| {
                        field.requirement == Requirement::Required
                            || nullable_shown(field)
                            || field.parent_example && !field.deprecated
                    })
                    .map(|field| {
                        if is_any_type(&field.type_ref) {
                            self.untyped_arguments.insert(field.py_name.clone());
                        }
                        let value = if nullable_shown(&field) && field.example.is_none() {
                            Example::Atom("None".to_string())
                        } else {
                            self.field_example(
                                &field.type_ref,
                                &field.wire_name,
                                field.example.as_deref(),
                                field.lengths,
                            )
                        };
                        (Some(field.py_name), value)
                    })
                    .collect::<Vec<_>>();
                Example::Call(name.to_string(), args)
            }
            // A string alias is sized by its own lengths in the IR fallback:
            // nexmo-messages' `to` is typed by `ToNumber` (`minLength: 7`) and
            // its fallback call passes `to="apricot"`.
            Some(TypeDecl::Alias(a)) => {
                let target = a.target.clone();
                let lengths = (a.min_length, a.max_length);
                self.sized_fallback(&target, lengths)
                    .unwrap_or_else(|| self.value(&target, slot))
            }
            // A literal enum's example is its first value as a plain string, which
            // needs no import. The Markdown writers escape its `'` as they do a
            // string example's (`"You\'ll find it"`); docstrings keep it bare.
            Some(TypeDecl::Enum(e)) if self.enum_type == EnumType::Literals => {
                match e.example_member() {
                    Some(m) if self.documentation => {
                        Example::Atom(literal_enum_value(&m.value).replace('\'', "\\'"))
                    }
                    Some(m) => Example::Atom(literal_enum_value(&m.value)),
                    None => Example::Atom("None".to_string()),
                }
            }
            // An enum's example is member access on its first member
            // (`TypesWeatherReport.SUNNY`), importing the enum by name.
            Some(TypeDecl::Enum(e)) => {
                self.record_ref(name);
                match e.example_member() {
                    Some(m) => Example::Atom(format!("{name}.{}", m.name)),
                    None => Example::Atom("None".to_string()),
                }
            }
            Some(TypeDecl::DiscriminatedUnion(u)) => match u.members.first() {
                // A wrapper holding a union whole passes its payload as `value`,
                // the one argument Fern's docstring writer leaves without a
                // trailing comma (the hand-written `kitchen-nested-mapping-target`
                // fixture's `Course_Grill(value=SteakOrder(…))`).
                Some(m) if m.wrapped => {
                    self.record_ref(&m.class_name);
                    let payload = m.fields.first().map(|field| field.type_ref.clone());
                    let value = payload.map_or_else(
                        || Example::Atom("None".to_string()),
                        |payload| self.value(&payload, slot),
                    );
                    Example::Wrapped(m.class_name.clone(), Box::new(value))
                }
                Some(m) => {
                    self.record_ref(&m.class_name);
                    // The discriminant field carries a default (`= "circle"`), so
                    // Fern's example omits it and sets only the required fields —
                    // *unless* the variant's own declaration of the tag hoisted an
                    // enum type of its own, which happens when that declaration
                    // lists more than the one value the variant is. The Open
                    // Integration Hub's four auth-client variants each declare all
                    // four `type` values, so `MutableOa1TwoLeggedClientType` exists
                    // and the example passes it, back in the schema position the
                    // model moved the tag out of.
                    // A required-but-nullable field is shown as a top-level body
                    // field is (see `build_example_inner`): by its declared
                    // example, else as `None` in the Markdown writers alone —
                    // osparc-simcore-webserver's `RegisterFunctionRequest_Project`
                    // passes `default_inputs=None` in `reference.md` only.
                    let documentation = self.documentation;
                    let fallback = self.field_examples_ignored;
                    let shown = |f: &Field| {
                        f.spec_required
                            && (!f.optional
                                || !fallback
                                    && !is_any_type(&f.type_ref)
                                    && (f.example.is_some() || documentation))
                    };
                    let mut args = Vec::new();
                    for (py_name, wire_name, ty, example, empty_only, null, lengths) in m
                        .fields
                        .iter()
                        .filter(|f| shown(f))
                        .map(|f| {
                            (
                                f.py_name.clone(),
                                f.wire_name.clone(),
                                f.type_ref.clone(),
                                f.example.clone(),
                                f.admits_only_empty_object,
                                f.optional && f.example.is_none(),
                                (f.min_length, f.max_length),
                            )
                        })
                        .collect::<Vec<_>>()
                    {
                        let value = if null {
                            Example::Atom("None".to_string())
                        } else if empty_only {
                            self.empty_object()
                        } else {
                            self.field_example(&ty, &wire_name, example.as_deref(), lengths)
                        };
                        args.push((Some(py_name), value));
                    }
                    if let Some(index) = m.discriminant_index {
                        let enum_name = m.source.as_deref().map(|source| {
                            naming::child_class_name(source, &u.discriminant_property)
                        });
                        // The tag is a member access on its enum, imported by name,
                        // or — for a literal enum — the discriminant string alone.
                        let member =
                            enum_name
                                .as_deref()
                                .and_then(|enum_name| match self.find(enum_name) {
                                    Some(TypeDecl::Enum(declared)) => declared
                                        .members
                                        .iter()
                                        .find(|value| value.value == m.discriminant)
                                        .map(|value| {
                                            if self.enum_type == EnumType::Literals {
                                                (literal_enum_value(&value.value), false)
                                            } else {
                                                (format!("{enum_name}.{}", value.name), true)
                                            }
                                        }),
                                    _ => None,
                                });
                        if let (Some(enum_name), Some((member, imported))) = (enum_name, member) {
                            // The Python docstring writer puts the tag back in
                            // its schema position; the Markdown writers (README
                            // and `reference.md`) append it after every other
                            // argument.
                            let at = if self.documentation {
                                args.len()
                            } else {
                                m.fields.iter().take(index).filter(|f| shown(f)).count()
                            };
                            if imported {
                                self.record_ref(&enum_name);
                            }
                            args.insert(
                                at.min(args.len()),
                                (Some(discriminant_field_name(u)), Example::Atom(member)),
                            );
                        }
                    }
                    Example::Call(m.class_name.clone(), args)
                }
                None => Example::Dict(vec![(
                    "key".to_string(),
                    Example::Atom("\"value\"".to_string()),
                )]),
            },
            None => Example::Dict(vec![(
                "key".to_string(),
                Example::Atom("\"value\"".to_string()),
            )]),
        }
    }

    /// Run `render` and then forget every import it registered — the constructor
    /// names, the tag-scoped ones and the `datetime` flag alike.
    ///
    /// The docstring writer needs to know whether an object-typed path parameter's
    /// model *can* be constructed while documenting the plain name placeholder
    /// instead of the model, so it renders the value and throws it away. Without
    /// this the discarded render still imports the model, and the docstring's
    /// `from fern import FernApi` gains a `ProbeParam` nothing in it names.
    fn without_recording<T>(&mut self, render: impl FnOnce(&mut Self) -> T) -> T {
        let recorded = self.recorded();
        let out = render(self);
        self.restore_recorded(recorded);
        out
    }

    /// Every import an example has registered so far, to roll back to.
    fn recorded(&self) -> Recorded {
        Recorded {
            referenced: self.referenced.clone(),
            referenced_doc_order: self.referenced_doc_order.clone(),
            referenced_tag: self.referenced_tag.clone(),
            referenced_tag_doc_order: self.referenced_tag_doc_order.clone(),
            uses_datetime: self.uses_datetime,
            datetime_precedes_tag_import: self.datetime_precedes_tag_import,
        }
    }

    /// Forget every import registered since `recorded` was taken.
    fn restore_recorded(&mut self, recorded: Recorded) {
        self.referenced = recorded.referenced;
        self.referenced_doc_order = recorded.referenced_doc_order;
        self.referenced_tag = recorded.referenced_tag;
        self.referenced_tag_doc_order = recorded.referenced_tag_doc_order;
        self.uses_datetime = recorded.uses_datetime;
        self.datetime_precedes_tag_import = recorded.datetime_precedes_tag_import;
    }

    /// The example Fern renders for an **object-typed path parameter**, or `None`
    /// where Fern renders the endpoint no example at all.
    ///
    /// A path parameter over an object schema is a shape no registered source
    /// declares, so it is measured on
    /// `docs/openapi-surface/probes/parameter-style-simple-path-object.yml` and
    /// the field-kind variants recorded beside it in `docs/fern-limitations.md`'s
    /// Round 6. Fern renders the model over its **required** fields alone, and
    /// each value comes from the field's *type* rather than from its name —
    /// `role="string"`, not the `role="role"` a request body's field takes. Two
    /// kinds render empty rather than populated (an array is `[]`, a map `{}`),
    /// and a required field the writer cannot render drops the whole example:
    /// Fern emits no `Examples` docstring, no README call and a bare
    /// `client.probe(...)` in `reference.md`. Which kinds those are is
    /// [`path_field_render`], the one place the rule is stated.
    fn path_object_value(&mut self, t: &TypeRef) -> Option<Example> {
        let obj = path_object_decl(t, self.types, self.tag_decls)?;
        let fields = path_object_required_fields(obj, self.types, self.tag_decls);
        let name = obj.name.clone();
        self.record_ref(&name);
        let mut args = Vec::new();
        for (py_name, type_ref) in fields {
            let rendered = match path_field_render(&type_ref, self.types, self.tag_decls)? {
                PathFieldRender::EmptyList => Example::List(Vec::new()),
                PathFieldRender::EmptyDict => Example::Dict(Vec::new()),
                PathFieldRender::Plain => self.value(&type_ref, Slot::Plain),
            };
            args.push((Some(py_name), rendered));
        }
        Some(Example::Call(name, args))
    }

    /// The example of a value whose schema admits only the empty object: `{}`,
    /// where Fern writes its `{"key": "value"}` placeholder, which the schema
    /// rejects (the `closed-empty-object-example` departure).
    fn empty_object(&self) -> Example {
        if self.reference {
            Example::ReferenceDict(Vec::new())
        } else {
            Example::Dict(Vec::new())
        }
    }

    /// One field's example value: its declared `example` where that literal is
    /// what Fern shows for the field's type, and the synthesized placeholder
    /// otherwise. A union wrapper's fields are exampled the same way an object's
    /// are — NDW's `AreaRequest_Municipality` opens with the `GM0344` its
    /// `MunicipalityAreaRequest.id` declares.
    /// A string sized by its declared lengths, as Fern's IR fallback example
    /// writes it, where that fallback writes the example and the lengths call
    /// for one ([`crate::ir::fallback_string_sample`]); `None` elsewhere.
    fn sized_fallback(&self, ty: &TypeRef, lengths: (Option<u64>, Option<u64>)) -> Option<Example> {
        if !self.field_examples_ignored || *ty != TypeRef::Primitive(Prim::Str) {
            return None;
        }
        crate::ir::fallback_string_sample(lengths.0, lengths.1)
            .map(|sample| Example::Atom(format!("\"{sample}\"")))
    }

    fn field_example(
        &mut self,
        ty: &TypeRef,
        wire: &str,
        example: Option<&str>,
        lengths: (Option<u64>, Option<u64>),
    ) -> Example {
        // A field typed by a map to unknown (`Dict[str, Any]`) takes Fern's fixed
        // `{"key": "value"}` placeholder whatever the schema declares: HelixDB's
        // `OperationTree` is `type: object` + `additionalProperties: true` and
        // carries two `examples`, and the golden's worked call for the `root`
        // field that names it shows the placeholder. A value the *enclosing*
        // object's own example spells out is a different question and reaches
        // this map through `value_from_example`, which is why the suppression is
        // here rather than there — `mosip-esignet`'s `Purpose.title` keeps the
        // `{"@none": "Title"}` its request example gives it.
        // Without an importer example Fern's IR fallback reads no field's own
        // example: NPQ's `accept_an_npq_application` documents `type="type"`
        // over the `npq-application-accept` its schema declares.
        let example =
            example.filter(|_| !self.field_examples_ignored && !self.resolves_to_unknown_map(ty));
        let literal = match example {
            // A date/date-time field's declared example is used like any other:
            // EN 18222's `last_updated` is typed by the `Timestamp` alias and
            // exampled from that alias's own `2026-06-08T15:30:00Z`, not from the
            // placeholder.
            Some(example)
                if self.example_is_scalar(ty)
                    || example.starts_with(['{', '['])
                    || self.example_is_temporal(ty) =>
            {
                self.value_from_example(ty, example)
            }
            _ => None,
        };
        literal
            .or_else(|| self.sized_fallback(ty, lengths))
            .unwrap_or_else(|| self.value(ty, Slot::Named(wire)))
    }

    /// An object's fields including those inherited from its base classes (bases
    /// first, in declaration order).
    fn object_fields(&self, obj: &ObjectType) -> Vec<ExampleField> {
        let mut out = Vec::new();
        for base in &obj.bases {
            if let Some(TypeDecl::Object(b)) = self.find(base) {
                out.extend(self.object_fields(b));
            }
        }
        // In a docstring a flattened parent's fields lead the model's own, as
        // an extended parent's do: TAMS's `FlowVideo` docstring example opens
        // with `FlowCore`'s `id` and `source_id` though the model writes
        // `codec` first, where `reference.md` keeps the model's order. A model
        // still extending a base keeps its own order: the Vonage Messages API's
        // `SendMessageRequestZero` extends `ChannelOptionsSms` and
        // `BaseMessageType`, and its example writes the flattened `Text`'s
        // `message_type` after its own `text`.
        let leads = |f: &&Field| {
            !self.documentation && obj.bases.is_empty() && f.inherited_from_flattened_parent
        };
        let inherited_first = obj
            .fields
            .iter()
            .filter(leads)
            .chain(obj.fields.iter().filter(|f| !leads(f)));
        for f in inherited_first {
            // An example spells a field Fern renames only on the model under its
            // plain name: VisKit's `ThreePieceIn.copy` is the model field `copy_`,
            // and Fern's example constructs `ThreePieceIn(copy="copy", …)`.
            let py_name = if f.py_name == naming::model_field_name(&f.wire_name) {
                naming::field_name(&f.wire_name)
            } else {
                f.py_name.clone()
            };
            out.push(ExampleField {
                py_name,
                wire_name: f.wire_name.clone(),
                type_ref: f.type_ref.clone(),
                requirement: match (f.spec_required, f.optional) {
                    (true, false) => Requirement::Required,
                    (true, true) => Requirement::RequiredNullable,
                    (false, _) => Requirement::Optional,
                },
                example: f.example.clone(),
                parent_example: obj.example_fields.contains(&f.wire_name),
                deprecated: f.deprecated,
                lengths: (f.min_length, f.max_length),
            });
        }
        out
    }
}

/// The imports an [`ExampleCtx`] has registered, as [`ExampleCtx::recorded`]
/// captures them.
struct Recorded {
    referenced: BTreeSet<String>,
    referenced_doc_order: Vec<String>,
    referenced_tag: BTreeSet<(String, String)>,
    referenced_tag_doc_order: Vec<(String, String)>,
    uses_datetime: bool,
    datetime_precedes_tag_import: bool,
}

/// Whether a model field's property is required, as example synthesis reads it.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Requirement {
    /// Not in the schema's `required` set.
    Optional,
    /// In `required`, and not `Optional` in Python.
    Required,
    /// In `required` but `Optional` in Python. Fern's importer reads such a
    /// property as optional, so its example omits it — ramu-shogi's
    /// `evalCp: {type: [integer, null]}` and its `$ref`s to `type: [object,
    /// null]` components are required and absent — except that the Markdown
    /// writers write an example's `null` for it as `None`.
    RequiredNullable,
}

/// One field of a model, as example synthesis reads it: the field and its marks.
struct ExampleField {
    /// The keyword the model constructor takes.
    py_name: String,
    /// The JSON property name an example's value is keyed by.
    wire_name: String,
    /// The field's type.
    type_ref: TypeRef,
    /// Whether the property is required, and how Python types it.
    requirement: Requirement,
    /// The field's own example literal.
    example: Option<String>,
    /// Whether the model's schema-level example names the field.
    parent_example: bool,
    /// [`crate::ir::Field::deprecated`].
    deprecated: bool,
    /// [`crate::ir::Field::min_length`] and [`crate::ir::Field::max_length`].
    lengths: (Option<u64>, Option<u64>),
}

/// How one required field of an **object-typed path parameter** renders inside
/// Fern's documentation example, or `None` where Fern documents the endpoint no
/// example at all.
///
/// The kinds are the ones measured, one probe variant each, against
/// `fernapi/fern-python-sdk` 5.20.0 (`docs/fern-limitations.md`, Round 6; the
/// variants are not re-measured at the certified pair): a
/// string, an integer, a `date-time` and an enum member render from the type;
/// an array renders `[]` and a map `{}`, populated in neither case; and a
/// required field that is itself a generated model drops the example entirely.
/// Every other kind is unmeasured and drops it too, which invents no bytes.
enum PathFieldRender {
    /// Rendered from the type, as [`Slot::Plain`] renders it.
    Plain,
    /// `[]` — an array field is never populated here.
    EmptyList,
    /// `{}` — nor is a map.
    EmptyDict,
}

fn path_field_render(
    t: &TypeRef,
    types: &[TypeDecl],
    tag_decls: &[TagTypeDecl],
) -> Option<PathFieldRender> {
    match t {
        TypeRef::Optional(inner) => path_field_render(inner, types, tag_decls),
        TypeRef::List(_) | TypeRef::Set(_) => Some(PathFieldRender::EmptyList),
        TypeRef::Dict(_, _) => Some(PathFieldRender::EmptyDict),
        TypeRef::Primitive(
            Prim::Str
            | Prim::Int
            | Prim::Long
            | Prim::Float
            | Prim::Bool
            | Prim::LiteralBool(_)
            | Prim::Datetime
            | Prim::Date,
        ) => Some(PathFieldRender::Plain),
        TypeRef::Named(name) => match lookup_decl(types, tag_decls, name) {
            Some(TypeDecl::Enum(_)) => Some(PathFieldRender::Plain),
            Some(TypeDecl::Alias(alias)) => path_field_render(&alias.target, types, tag_decls),
            _ => None,
        },
        _ => None,
    }
}

/// One declaration by name, over the package-root types and the tag-scoped ones —
/// the lookup [`ExampleCtx::find`] performs, as a free function so the
/// example writer and the structural predicates that gate it share one answer.
fn lookup_decl<'a>(
    types: &'a [TypeDecl],
    tag_decls: &'a [TagTypeDecl],
    name: &str,
) -> Option<&'a TypeDecl> {
    types.iter().find(|d| d.name() == name).or_else(|| {
        tag_decls
            .iter()
            .find_map(|tt| (tt.decl.name() == name).then_some(&tt.decl))
    })
}

/// The generated object a path parameter's type resolves to, through `Optional`
/// and named aliases; `None` when the parameter is not object-typed at all.
fn path_object_decl<'a>(
    t: &TypeRef,
    types: &'a [TypeDecl],
    tag_decls: &'a [TagTypeDecl],
) -> Option<&'a ObjectType> {
    match t {
        TypeRef::Optional(inner) => path_object_decl(inner, types, tag_decls),
        TypeRef::Named(name) => match lookup_decl(types, tag_decls, name) {
            Some(TypeDecl::Object(obj)) => Some(obj),
            Some(TypeDecl::Alias(alias)) => path_object_decl(&alias.target, types, tag_decls),
            _ => None,
        },
        _ => None,
    }
}

/// An object's required fields as `(python name, type)`, inherited ones first, in
/// the order a constructed example passes them.
fn path_object_required_fields(
    obj: &ObjectType,
    types: &[TypeDecl],
    tag_decls: &[TagTypeDecl],
) -> Vec<(String, TypeRef)> {
    let mut out = Vec::new();
    for base in &obj.bases {
        if let Some(TypeDecl::Object(parent)) = lookup_decl(types, tag_decls, base) {
            out.extend(path_object_required_fields(parent, types, tag_decls));
        }
    }
    out.extend(
        obj.fields
            .iter()
            .filter(|field| field.spec_required)
            .map(|field| (field.py_name.clone(), field.type_ref.clone())),
    );
    out
}

/// Whether Fern documents a worked example for this endpoint's path parameters —
/// false only where one is object-typed and [`path_field_render`] cannot render a
/// required field of it. The example writer reaches the same answer by rendering;
/// this is what the *structural* callers (README endpoint selection, the
/// `reference.md` snippet gap) ask before there is a value to look at.
fn path_object_documented(ep: &Endpoint, types: &[TypeDecl], tag_decls: &[TagTypeDecl]) -> bool {
    ep.path_params.iter().all(|pp| {
        path_object_decl(&pp.type_ref, types, tag_decls).is_none_or(|obj| {
            path_object_required_fields(obj, types, tag_decls)
                .iter()
                .all(|(_, type_ref)| path_field_render(type_ref, types, tag_decls).is_some())
        })
    })
}

/// A map whose value slot is *directly* free-form (`Dict[str, Any]`) — the shape
/// [`ExampleBuilder::value`] renders as Fern's `{"key": "value"}` placeholder.
fn is_unknown_map(t: &TypeRef) -> bool {
    let TypeRef::Dict(_, value) = t else {
        return false;
    };
    matches!(value.as_ref(), TypeRef::Primitive(Prim::Any))
        || matches!(value.as_ref(), TypeRef::Optional(inner) if matches!(inner.as_ref(), TypeRef::Primitive(Prim::Any)))
}

/// A free-form (`Any`) example as Fern's importer keeps it: a `null` and an
/// empty array carry no value, so they leave the map or list holding them, at
/// any depth — MockServer's HAR import drops each entry's `"headers": []`.
fn fern_unknown_example(value: serde_json::Value) -> Option<serde_json::Value> {
    match value {
        serde_json::Value::Null => None,
        serde_json::Value::Array(items) if items.is_empty() => None,
        serde_json::Value::Array(items) => Some(serde_json::Value::Array(
            items.into_iter().filter_map(fern_unknown_example).collect(),
        )),
        serde_json::Value::Object(fields) => Some(serde_json::Value::Object(
            fields
                .into_iter()
                .filter_map(|(key, field)| fern_unknown_example(field).map(|field| (key, field)))
                .collect(),
        )),
        scalar => Some(scalar),
    }
}

/// A JSON example value as an [`Example`], spelling a string that holds `"`
/// but no `'` the way Python's `repr` does when `python_repr` is set — a method docstring's
/// example does (MockServer's nested `"body": '{"orders":[]}'`), while
/// `reference.md` keeps the escaped JSON spelling.
fn example_from_json_as(value: serde_json::Value, python_repr: bool) -> Example {
    match value {
        serde_json::Value::Array(items) => Example::ExplicitList(
            items
                .into_iter()
                .map(|item| example_from_json_as(item, python_repr))
                .collect(),
        ),
        serde_json::Value::Object(fields) => Example::Dict(
            fields
                .into_iter()
                .map(|(key, value)| (key, example_from_json_as(value, python_repr)))
                .collect(),
        ),
        serde_json::Value::String(value)
            if python_repr && value.contains('"') && !value.contains('\'') =>
        {
            Example::Atom(
                crate::ir::example_literal(&serde_json::Value::String(value.clone()))
                    .unwrap_or_else(|| format!("{value:?}")),
            )
        }
        serde_json::Value::String(value) => Example::Atom(format!("{value:?}")),
        serde_json::Value::Number(value) => Example::Atom(value.to_string()),
        serde_json::Value::Bool(value) => {
            Example::Atom(if value { "True" } else { "False" }.to_string())
        }
        serde_json::Value::Null => Example::Atom("None".to_string()),
    }
}

/// Build the worked `Examples` block for a method as logical (indent-0) lines,
/// or `None` when the request is not exampleable (a raw bytes body).
/// Whether a spec `example` is shown verbatim for a field of this type. Fern uses
/// the literal only for a plain scalar (a `str`/number/`bool`); a named type (enum,
/// object, union) gets its own synthesized example (an enum member, a constructor),
/// and a `datetime`/`date`/container is built through a call, so the raw literal
/// would be wrong there.
fn example_scalar(t: &TypeRef) -> bool {
    match t {
        TypeRef::Optional(inner) => example_scalar(inner),
        TypeRef::Primitive(p) => {
            matches!(
                p,
                Prim::Str
                    | Prim::Bytes
                    | Prim::Int
                    | Prim::Long
                    | Prim::Float
                    | Prim::Bool
                    | Prim::LiteralBool(_)
            )
        }
        _ => false,
    }
}

/// Whether a type is the unknown `Any` value (optionally wrapped). A required field
/// of this type is still shown in a worked example, unlike a required-but-nullable
/// concrete field, so it is exempt from the nullable exclusion.
fn is_any_type(t: &TypeRef) -> bool {
    match t {
        TypeRef::Optional(inner) => is_any_type(inner),
        TypeRef::Primitive(Prim::Any) => true,
        _ => false,
    }
}

fn build_example(
    ep: &Endpoint,
    is_async: bool,
    module: &str,
    pkg: &str,
    client_name: &str,
    ctx: &mut ExampleCtx,
) -> Option<Vec<String>> {
    build_example_inner(
        ep,
        is_async,
        module,
        pkg,
        client_name,
        ctx,
        None,
        false,
        None,
        false,
    )
}

#[allow(
    clippy::too_many_arguments,
    reason = "documentation example emission also selects the README/reference writer"
)]
fn build_documentation_example(
    ep: &Endpoint,
    is_async: bool,
    module: &str,
    pkg: &str,
    client_name: &str,
    ctx: &mut ExampleCtx,
    environment: Option<&crate::ir::Environment>,
    reference: bool,
) -> Option<Vec<String>> {
    build_example_inner(
        ep,
        is_async,
        module,
        pkg,
        client_name,
        ctx,
        reference
            .then_some(ep.reference_body_example.as_ref())
            .flatten(),
        true,
        environment,
        reference,
    )
}

/// The rendered value of a required query parameter in a header-first worked
/// example — the ordering a wildcard or form request body takes. The value is the
/// one the ordinary ordering renders: a literal the type cannot hold is not an
/// example Fern shows, so an enum parameter whose example names no member falls
/// back to the enum's own synthesized value rather than to the raw literal.
fn header_first_query_example(ctx: &mut ExampleCtx, qp: &QueryParam) -> Example {
    if let Some(example) = qp.example.as_ref().filter(|_| qp.example_is_scalar) {
        return ctx
            .value_from_example(&qp.type_ref, example)
            .unwrap_or_else(|| ctx.value(&qp.type_ref, Slot::Named(&qp.wire_name)));
    }
    if let TypeRef::List(inner) = &qp.type_ref {
        return Example::List(vec![ctx.value(inner, Slot::Named(&qp.wire_name))]);
    }
    ctx.value(&qp.type_ref, Slot::Named(&qp.wire_name))
}

#[allow(
    clippy::too_many_arguments,
    reason = "example emission needs endpoint, client, value context, and documentation-mode inputs"
)]
fn build_example_inner(
    ep: &Endpoint,
    is_async: bool,
    module: &str,
    pkg: &str,
    client_name: &str,
    ctx: &mut ExampleCtx,
    body_example: Option<&serde_json::Value>,
    documentation: bool,
    environment: Option<&crate::ir::Environment>,
    reference: bool,
) -> Option<Vec<String>> {
    if matches!(ep.request_body, Some(RequestBody::Bytes { .. }))
        || !endpoint_has_worked_example(ep)
    {
        return None;
    }

    ctx.documentation = documentation;
    if !documentation
        && ctx
            .global_headers
            .iter()
            .any(|header| header.py_type() == HeaderType::Date)
    {
        ctx.uses_datetime = true;
    }
    ctx.reference = reference;
    ctx.field_examples_ignored = ep.importer_example_missing;
    // A `*/*` binary download documents its required arguments only: an optional
    // parameter is shown by its declared example, and Fern reads none for it.
    // apideck-file-storage's `filesDownload` takes a required `id` beside an
    // optional `fields` exampled `id,updated_at`, and documents
    // `download(id="id")` (measured at fern-python-sdk 5.41.3, where 5.20.0
    // documented no argument at all).
    let declared_examples = !ep.wildcard_binary_response;
    // A path parameter whose schema declares no type is one Fern's example
    // generator cannot sample, and the endpoint is then exampled by its other
    // writer: the parameter by its own name, and a free-form map body as one
    // `"string"` entry of an unknown value. Measured on the one golden that
    // declares such a parameter: Deep Search's `update_project_knowledge_graph_metadata`
    // documents `bag_key="bag_key", request={"string": {"key": "value"}}`, while
    // `create_project_knowledge_graph` posts the same `{type: object}` body beside
    // a typed path parameter and documents `request={"key": "value"}`.
    let untyped_path_parameter = ep
        .path_params
        .iter()
        .any(|param| matches!(param.type_ref, TypeRef::Primitive(Prim::Any)));
    // The example call's keyword arguments, in signature order: path params,
    // required query/header params, then the request body (a single `request`, or
    // each required inlined field).
    let mut args: Vec<(Option<String>, Example)> = Vec::new();
    let header_slot = |wire: &str| {
        if ep.text_response || ep.binary_response {
            let mut words = naming::split_words(wire).into_iter();
            let mut value = words.next().unwrap_or_default();
            for word in words {
                value.push_str(&naming::to_pascal_case(&word));
            }
            value
        } else {
            wire.to_string()
        }
    };
    for pp in &ep.path_params {
        if (reference || documentation) && matches!(pp.type_ref, TypeRef::List(_) | TypeRef::Set(_))
        {
            continue;
        }
        let v = if matches!(
            pp.type_ref,
            TypeRef::List(_) | TypeRef::Set(_) | TypeRef::Primitive(Prim::Any)
        ) {
            // Fern's path placeholder stays a string even for the unusual array
            // path parameters accepted by its OpenAPI importer, and for an
            // untyped one (see `untyped_path_parameter` above).
            Example::Atom(format!("{:?}", pp.wire_name))
        } else if path_object_decl(&pp.type_ref, ctx.types, ctx.tag_decls).is_some() {
            // An object-typed path parameter is documented two different ways
            // by Fern's two example writers, and the difference is measured on
            // `parameter-style-simple-path-object.yml`: the Markdown writers
            // construct the model, while the docstring writer keeps the plain
            // name placeholder a scalar parameter would take. Where the model
            // cannot be constructed at all the endpoint documents nothing, in
            // either writer — see [`ExampleCtx::path_object_value`].
            if documentation {
                ctx.path_object_value(&pp.type_ref)?
            } else {
                // The value is rendered and discarded: what the docstring
                // takes from it is only whether Fern documents the endpoint
                // at all, so the imports it would register are rolled back.
                ctx.without_recording(|ctx| ctx.path_object_value(&pp.type_ref))?;
                Example::Atom(format!("{:?}", pp.wire_name))
            }
        } else {
            pp.example
                .as_ref()
                // Fern's example generator substitutes the parameter's own
                // name for a declared example on any endpoint whose success
                // body is not JSON — a binary download, and equally a
                // `text/*` one. Audiobookshelf declares one `id` parameter on
                // `/api/authors/{id}` and Fern splits its own path item on
                // this: `getAuthorById` and `updateAuthorById` answer
                // `application/json` and document
                // `id="e4bb1afb-4a4f-4dd6-8be0-e615d233185b"`, while
                // `deleteAuthorById` answers `text/plain` and documents
                // `id="id"`.
                .filter(|_| {
                    !ep.binary_response && !ep.text_response && ctx.example_is_scalar(&pp.type_ref)
                })
                .and_then(|example| ctx.value_from_example(&pp.type_ref, example))
                .unwrap_or_else(|| ctx.value(&pp.type_ref, Slot::Path(&pp.wire_name)))
        };
        args.push((Some(pp.py_name.clone()), v));
    }
    // Fern normally follows required/optional grouping for parameters. Two request
    // bodies are imported with header examples before query examples instead: a
    // wildcard (`*/*`) body, and a form body (`multipart/form-data` or
    // urlencoded) — api.video's `POST /upload` shows its optional `Content-Range`
    // header ahead of its required `token` query parameter. Both keep the ordinary
    // query-then-header order in the Markdown writers, so the flag is read
    // alongside `reference` rather than on its own.
    let header_first_request = ep
        .request_body
        .as_ref()
        .is_some_and(|body| body.is_wildcard_media() || matches!(body, RequestBody::Form(_)));
    let parameters_start = args.len();
    let mut omits_required_query_arrays = ep.text_response
        || ep.query_params.iter().any(|qp| {
            qp.required
                && (matches!(qp.type_ref, TypeRef::Dict(..)) || ctx.example_is_object(&qp.type_ref))
        });
    // So does one whose synthesized value is the unknown map placeholder
    // `{"key": "value"}`: rice-shower's `time_start` is an `anyOf` led by an
    // untyped `type: Date` member, and Fern leaves `channels` out beside it.
    for qp in ep.query_params.iter().filter(|qp| {
        qp.required
            && qp.example.is_none()
            && !matches!(qp.type_ref, TypeRef::List(_) | TypeRef::Set(_))
    }) {
        if omits_required_query_arrays {
            break;
        }
        let value =
            ctx.without_recording(|ctx| ctx.value(&qp.type_ref, Slot::Named(&qp.wire_name)));
        omits_required_query_arrays = matches!(
            value,
            Example::Dict(_) | Example::DocDict(_) | Example::ReferenceDict(_)
        );
    }
    for qp in ep
        .query_params
        .iter()
        .filter(|qp| !header_first_request && qp.required)
    {
        // A required array is shown with one sampled item, whatever its item
        // type (`colors=[Color.RED]`, `spots=[Spot()]`), except that Fern leaves
        // every one out of an operation whose success body is `text/*`, or that
        // takes a required object or map query parameter. Measured on variants
        // of one operation: `application/xml`, `+json`, `204` and error-only
        // responses all show it, as does an optional map beside it. The
        // signature makes every query array optional, so either call runs.
        // A declared example is kept where Fern drops it (the
        // `optional-lowered-required-query-array-example` departure): the
        // specification's own value documents the argument the operation
        // requires. A synthesized one is left out, as Fern leaves it.
        if omits_required_query_arrays
            && matches!(qp.type_ref, TypeRef::List(_) | TypeRef::Set(_))
            && !(qp.example.is_some() && qp.example_is_scalar)
        {
            continue;
        }
        let v = if let Some(ex) = qp.example.as_ref().filter(|_| qp.example_is_scalar) {
            // A literal the type cannot hold is not an example Fern renders: an
            // enum parameter whose example names no member takes the enum's own
            // synthesized value (`mosip-esignet`'s `scope: openid profile`, which
            // is two members' values in one string). Every type that *can* hold a
            // literal is scalar, and [`ExampleCtx::value_from_example`] renders
            // one for each of those, so the fallback is only ever the synthesized
            // value.
            ctx.value_from_example(&qp.type_ref, ex)
                .unwrap_or_else(|| ctx.value(&qp.type_ref, Slot::Named(&qp.wire_name)))
        } else if let TypeRef::List(inner) = &qp.type_ref {
            if qp.one_or_many && !reference {
                ctx.value(inner, Slot::Named(&qp.wire_name))
            } else if matches!(inner.as_ref(), TypeRef::Optional(_)) && !documentation {
                // Nullable items: a docstring passes an empty list. The Markdown
                // passes one element, the non-null one Fern renders for the same
                // items without `nullable`, where Fern's own passes `None`, which
                // the signature's `Sequence[int]` rejects (the
                // `nullable-items-docs` departure).
                Example::List(Vec::new())
            } else {
                Example::List(vec![ctx.value(inner, Slot::Named(&qp.wire_name))])
            }
        } else if let TypeRef::Dict(_, value) = &qp.type_ref {
            let pairs = vec![(
                qp.wire_name.clone(),
                ctx.value(value, Slot::Named(&qp.wire_name)),
            )];
            if reference {
                Example::ReferenceDict(pairs)
            } else {
                Example::Dict(pairs)
            }
        } else {
            // A union led by an unknown takes that unknown's placeholder, which
            // the Markdown writers keep on its line as they keep any untyped
            // value's: rice-shower's `time_start={"key": "value"}`.
            if ctx.resolves_to_union_led_by_unknown(&qp.type_ref) {
                ctx.untyped_arguments.insert(qp.py_name.clone());
            }
            ctx.value(&qp.type_ref, Slot::Named(&qp.wire_name))
        };
        args.push((Some(qp.py_name.clone()), v));
    }
    for header_param in ep
        .header_params
        .iter()
        .filter(|header| header.required && !reference)
    {
        let v = header_param
            .example
            .as_ref()
            .filter(|_| ctx.example_is_scalar(&header_param.type_ref))
            .and_then(|example| ctx.value_from_example(&header_param.type_ref, example))
            .unwrap_or_else(|| {
                let slot = header_slot(&header_param.wire_name);
                header_example(ctx.value(&header_param.type_ref, Slot::Named(&slot)))
            });
        args.push((Some(header_param.py_name.clone()), v));
    }
    if header_first_request && !reference {
        for header_param in ep
            .header_params
            .iter()
            .filter(|header| !header.required && header.example.is_some() && declared_examples)
        {
            let value = header_param
                .example
                .as_ref()
                .filter(|_| ctx.example_is_scalar(&header_param.type_ref))
                .and_then(|example| ctx.value_from_example(&header_param.type_ref, example))
                .unwrap_or_else(|| {
                    let slot = header_slot(&header_param.wire_name);
                    header_example(ctx.value(&header_param.type_ref, Slot::Named(&slot)))
                });
            args.push((Some(header_param.py_name.clone()), value));
        }
        for qp in ep.query_params.iter().filter(|qp| qp.required) {
            args.push((
                Some(qp.py_name.clone()),
                header_first_query_example(ctx, qp),
            ));
        }
    }
    if header_first_request && reference {
        for qp in ep.query_params.iter().filter(|qp| qp.required) {
            args.push((
                Some(qp.py_name.clone()),
                header_first_query_example(ctx, qp),
            ));
        }
        for header_param in ep.header_params.iter().filter(|header| header.required) {
            let value = header_param
                .example
                .as_ref()
                .filter(|_| ctx.example_is_scalar(&header_param.type_ref))
                .and_then(|example| ctx.value_from_example(&header_param.type_ref, example))
                .unwrap_or_else(|| {
                    let slot = header_slot(&header_param.wire_name);
                    header_example(ctx.value(&header_param.type_ref, Slot::Named(&slot)))
                });
            args.push((Some(header_param.py_name.clone()), value));
        }
        for header_param in ep
            .header_params
            .iter()
            .filter(|header| !header.required && header.example.is_some() && declared_examples)
        {
            let value = header_param
                .example
                .as_ref()
                .filter(|_| ctx.example_is_scalar(&header_param.type_ref))
                .and_then(|example| ctx.value_from_example(&header_param.type_ref, example))
                .unwrap_or_else(|| {
                    let slot = header_slot(&header_param.wire_name);
                    header_example(ctx.value(&header_param.type_ref, Slot::Named(&slot)))
                });
            args.push((Some(header_param.py_name.clone()), value));
        }
    }
    for qp in ep.query_params.iter().filter(|qp| !qp.required) {
        if qp.example.is_none() || !qp.example_is_scalar || !declared_examples {
            continue;
        }
        let example = qp.example.as_deref().unwrap_or_default();
        // A YAML timestamp is a date to Fern's parser, so it is no example for a
        // parameter that is not temporal, and an optional one then goes
        // unshown: the hand-written `ref-pointer-walk` fixture's `since` writes
        // `example: 2018-01-01 10:00:00` unquoted over a `format: dateTime` string.
        if let Ok(serde_json::Value::String(value)) =
            serde_json::from_str::<serde_json::Value>(example)
        {
            if ctx
                .yaml_unquoted_timestamps
                .is_some_and(|unquoted| unquoted.contains(&value))
                && !ctx.example_is_temporal(&qp.type_ref)
                && yaml_resolves_as_timestamp(&value)
            {
                continue;
            }
        }
        args.push((
            Some(qp.py_name.clone()),
            ctx.value_from_example(&qp.type_ref, example)
                .unwrap_or_else(|| Example::Atom(example.to_string())),
        ));
    }
    if reference && !header_first_request {
        for header_param in ep.header_params.iter().filter(|header| header.required) {
            let value = header_param
                .example
                .as_ref()
                .filter(|_| ctx.example_is_scalar(&header_param.type_ref))
                .and_then(|example| ctx.value_from_example(&header_param.type_ref, example))
                .unwrap_or_else(|| {
                    let slot = header_slot(&header_param.wire_name);
                    header_example(ctx.value(&header_param.type_ref, Slot::Named(&slot)))
                });
            args.push((Some(header_param.py_name.clone()), value));
        }
    }
    for header_param in ep.header_params.iter().filter(|header| {
        !header_first_request && !header.required && header.example.is_some() && declared_examples
    }) {
        let value = header_param
            .example
            .as_ref()
            .filter(|_| ctx.example_is_scalar(&header_param.type_ref))
            .and_then(|example| ctx.value_from_example(&header_param.type_ref, example))
            .unwrap_or_else(|| {
                let slot = header_slot(&header_param.wire_name);
                header_example(ctx.value(&header_param.type_ref, Slot::Named(&slot)))
            });
        args.push((Some(header_param.py_name.clone()), value));
    }
    // The docstring writer walks an example's headers before its query
    // parameters, whatever the body (`EndpointFunctionSnippetGenerator`); the
    // Markdown writers keep the order above. Sim's `create_table_import_part_urls`
    // documents its `upload-token` header ahead of its `workspaceId` query in
    // `client.py` and after it in `reference.md`.
    if !documentation {
        let (headers, rest): (Vec<_>, Vec<_>) =
            args.drain(parameters_start..).partition(|(name, _)| {
                ep.header_params
                    .iter()
                    .any(|header| Some(&header.py_name) == name.as_ref())
            });
        args.extend(headers);
        args.extend(rest);
    }
    // A constant header is no argument, so no call passes it, though Fern's
    // Markdown does (the `constant-header-docs-arguments` departure).
    match &ep.request_body {
        Some(RequestBody::Single(s)) => {
            let mut v = if let Some(example) = body_example {
                ctx.value_from_example(&s.type_ref, &example.to_string())
                    .unwrap_or_else(|| ctx.value(&s.type_ref, Slot::Plain))
            } else {
                s.example
                    .as_deref()
                    .and_then(|example| ctx.value_from_example(&s.type_ref, example))
                    .unwrap_or_else(|| ctx.value(&s.type_ref, Slot::Plain))
            };
            // The JSON binary example must have its generated bytes type. The
            // matched method still passes the value directly to `json=`.
            if s.type_ref == TypeRef::Primitive(Prim::Bytes)
                && s.content_type
                && s.content_type_override.is_none()
                && matches!(&v, Example::Atom(value) if value == "\"string\"")
            {
                v = Example::Atom("b\"string\"".to_string());
            }
            if untyped_path_parameter
                && body_example.is_none()
                && s.example.is_none()
                && is_unknown_map(&s.type_ref)
            {
                let pairs = vec![(
                    "string".to_string(),
                    ctx.value(&TypeRef::Primitive(Prim::Any), Slot::Map),
                )];
                v = if reference {
                    Example::ReferenceDict(pairs)
                } else {
                    Example::Dict(pairs)
                };
            }
            // A binary download's array body is exampled with TWO synthesized
            // elements where every other endpoint's takes one. SFTPGo's
            // `streamzip` posts `{type: array, items: {type: string}}` and streams
            // a zip back, and its golden documents `request=["string", "string"]`;
            // gambitcomm's `set_protocols` posts the same shape — its item is
            // described too — and answers JSON, and its golden documents
            // `request=["string"]`, as do audiobookshelf's `downloadEpisodes`,
            // komga's `markAnnouncementsRead`.
            if ep.binary_response && body_example.is_none() && s.example.is_none() {
                v = match &v {
                    Example::List(items) => match items.as_slice() {
                        [only] => Example::List(vec![only.clone(), only.clone()]),
                        _ => v,
                    },
                    Example::ReferenceList(items) => match items.as_slice() {
                        [only] => Example::ReferenceList(vec![only.clone(), only.clone()]),
                        _ => v,
                    },
                    _ => v,
                };
            }
            // Without an importer example, Fern's IR fallback keys a map to
            // unknown by its key type's sample: Spendesk's `request_access_token`
            // posts a bare `type: object` and answers an undeclared `$ref`, and
            // its golden documents `request={"string": {"key": "value"}}`.
            // So does a map the body names through its component alias:
            // CloudPDF's `doc.pages.extract` posts `$ref DocPagesExtractRequest`.
            if ep.importer_example_missing
                && body_example.is_none()
                && s.example.is_none()
                && (matches!(&s.type_ref, TypeRef::Dict(_, value) if is_any_type(value))
                    || ctx.resolves_to_unknown_map(&s.type_ref))
            {
                v = if ctx.reference {
                    Example::ReferenceDict(vec![("string".to_string(), Example::Atom(v.flat()))])
                } else {
                    Example::Dict(vec![("string".to_string(), v)])
                };
            }
            // An optional body Fern types `Optional[Any]` has nothing to show, in
            // either document version: letta declares the shape in 3.1 and
            // braintrust's proxy endpoints in 3.0.3, and neither golden passes
            // `request=`. Nor does one naming an alias of it: marimo-plugins'
            // `write_marimo_chatbot_cancel_prompt_output` takes a `type: "null"`
            // component and is documented with no argument.
            if s.required
                || body_example.is_some()
                || s.example.is_some()
                || !(is_any_type(&s.type_ref) || ctx.resolves_to_any(&s.type_ref))
            {
                args.push((Some("request".to_string()), v));
            }
        }
        Some(RequestBody::Inline(fields)) => {
            // Fern's importer example gives a required-but-nullable field (one
            // whose signature is still `Optional[...] = OMIT`) its own example
            // where it declares one, and `null` where it does not (measured at
            // Fern CLI 5.163.1 with fern-python-sdk 5.41.3; the 5.20.0 pair left
            // every such field out). apideck's `Lead.company_name` declares
            // `Spacex`, which apideck-lead's `add`/`update` examples pass in every
            // writer; osparc-simcore-webserver's `phone` declares none, so its
            // docstring leaves it out and `reference.md` writes `phone=None` —
            // the same as an example's explicit `null` below.
            // Without an importer example, Fern's IR fallback reads no example the
            // request media or its fields declare: a `text/plain` answer leaves
            // only the required fields' placeholders, and no nullable one.
            let fallback = ep.importer_example_missing;
            let reference_fields = body_example
                .filter(|_| !fallback)
                .and_then(serde_json::Value::as_object);
            // An example's `null` is no value: Fern's docstring leaves the field
            // out, or synthesizes it where the signature requires it; its
            // Markdown writers do the same but write a required nullable
            // field's `null` as `None`.
            let required_nullable =
                |f: &BodyField| f.spec_required && f.optional && !is_any_type(&f.type_ref);
            let example_null = |f: &BodyField| match reference_fields {
                Some(values) => values
                    .get(&f.wire_name)
                    .is_some_and(serde_json::Value::is_null),
                None => {
                    f.media_example && f.example.as_deref() == Some("null")
                        || !fallback && required_nullable(f) && f.example.is_none()
                }
            };
            let named_present = |f: &BodyField| {
                reference_fields
                    .and_then(|values| values.get(&f.wire_name))
                    .is_some_and(|value| !value.is_null())
            };
            // An empty array names the field without a value, as in the
            // docstring's example (see `apply_body_example`): the field is
            // passed its own example or a synthesized one.
            let named_value = |f: &BodyField| {
                reference_fields
                    .and_then(|values| values.get(&f.wire_name))
                    .filter(|value| {
                        !value.is_null() && !value.as_array().is_some_and(Vec::is_empty)
                    })
            };
            // A required field is shown whatever its marks; an optional one whose
            // schema is deprecated is left out even where the example names it.
            let selected = |f: &&BodyField| {
                if example_null(f) {
                    return f.spec_required
                        && (!f.optional || is_any_type(&f.type_ref) || documentation);
                }
                f.spec_required
                    && !(required_nullable(f) && (fallback || reference_fields.is_some()))
                    || !f.deprecated
                        && (named_present(f)
                            || reference_fields.is_none() && f.media_example && !fallback)
            };
            let composed_request_media_example = ep.body_composition.is_all_of()
                && fields
                    .iter()
                    .any(|field| field.media_example && !field.schema_body_example);
            let mut example_fields: Vec<&BodyField> = if composed_request_media_example {
                fields
                    .iter()
                    .filter(|field| field.spec_required)
                    .filter(selected)
                    .collect()
            } else {
                fields.iter().filter(selected).collect()
            };
            if composed_request_media_example {
                if let Some(values) = reference_fields {
                    // The reference writer takes required arguments in signature
                    // order, then optional arguments in the selected named
                    // example's declaration order.
                    example_fields.extend(values.keys().filter_map(|wire_name| {
                        fields
                            .iter()
                            .find(|field| !field.spec_required && field.wire_name == *wire_name)
                            .filter(selected)
                    }));
                } else {
                    example_fields.extend(
                        fields
                            .iter()
                            .filter(|field| !field.spec_required)
                            .filter(selected),
                    );
                }
            }
            // A composed body's example lists its bases' fields ahead of its own:
            // the docstring every base's first all the way down, the Markdown
            // writers each base's own fields before that base's bases (TAMS'
            // `WebhookPut` documents `id`, then `WebhookWithId`'s base `Webhook`'s
            // `url` and `events`, where its docstring writes `id` third), and the
            // body's own first when it is itself an `allOf` parent. Measured on
            // variants of one `allOf: [inline, $ref Base]` body, `required` and
            // the members' order in `allOf` change none of these.
            if ep.body_composition.is_all_of() {
                if documentation && ep.body_composition == crate::ir::BodyComposition::AllOfParent {
                    example_fields.sort_by_key(|field| {
                        fields
                            .iter()
                            .position(|candidate| candidate.wire_name == field.wire_name)
                    });
                } else if documentation {
                    example_fields.sort_by_key(|field| field.markdown_order);
                } else {
                    example_fields.sort_by_key(|field| field.declaration_order);
                }
            }
            // The field a stream condition fixes is no argument, so no example
            // passes it, even where the schema's own example writes it.
            example_fields.retain(|field| {
                ep.stream_condition
                    .as_ref()
                    .is_none_or(|(wire, _)| *wire != field.wire_name)
            });
            for f in example_fields {
                let recorded = ctx.recorded();
                let supplied_example = f
                    .example
                    .as_deref()
                    .filter(|_| {
                        (reference_fields.is_none() || named_present(f) && named_value(f).is_none())
                            && !example_null(f)
                            && !fallback
                    })
                    // A binary download's inline body drops its media example:
                    // MockServer's file retrieval shows Fern's `name="name"`.
                    .filter(|_| !(ep.binary_response && f.media_example && !f.schema_body_example))
                    .map(str::to_string)
                    .or_else(|| named_value(f).map(serde_json::Value::to_string));
                let supplied_example = supplied_example.map(|example| {
                        if matches!(&f.type_ref, TypeRef::List(inner) | TypeRef::Set(inner) if ctx.resolves_to_string(inner))
                            && example.starts_with('[')
                            && example.contains("null")
                        {
                            if let Ok(mut values) = serde_json::from_str::<Vec<serde_json::Value>>(&example) {
                                values.iter_mut().filter(|value| value.is_null()).for_each(|value| {
                                    *value = serde_json::Value::String(f.wire_name.clone());
                                });
                                return serde_json::to_string(&values).unwrap_or(example);
                            }
                        }
                        example
                    });
                let mut v = supplied_example
                    .as_deref()
                    .and_then(|example| {
                        if f.schema_body_example
                            && ctx.example_is_temporal(&f.type_ref)
                            && example.starts_with("\"2000-01-23T04:56:07")
                        {
                            return None;
                        }
                        if reference_fields.is_some()
                            || ep.body_schema_is_beta && !ep.body_schema_example_wrapped
                            || f.media_example
                            || example.starts_with('{') && ctx.resolves_to_any(&f.type_ref)
                            || ctx.example_is_scalar(&f.type_ref)
                            || ctx.example_is_composite(&f.type_ref)
                            || ctx.example_is_temporal(&f.type_ref)
                        {
                            ctx.value_from_example(&f.type_ref, example)
                        } else {
                            None
                        }
                    })
                    .unwrap_or_else(|| {
                        if documentation
                            && f.optional
                            && !is_any_type(&f.type_ref)
                            && example_null(f)
                        {
                            Example::Atom("None".to_string())
                        } else if f.admits_only_empty_object {
                            ctx.empty_object()
                        } else {
                            ctx.value(&f.type_ref, Slot::Named(&f.wire_name))
                        }
                    });
                let synthesized_items =
                    ((ep.text_response || ep.binary_response || ep.importer_example_missing)
                        && f.spec_required)
                        .then_some(2);
                if let Some(min_items) = synthesized_items {
                    if let Example::List(items)
                    | Example::ExplicitList(items)
                    | Example::ReferenceList(items) = &mut v
                    {
                        if let Some(first) = items.first().cloned() {
                            items.resize(min_items, first);
                        }
                    }
                }
                if (ep.text_response || ep.binary_response)
                    && matches!(&f.type_ref, TypeRef::Dict(_, value) if is_any_type(value))
                {
                    v = if reference {
                        Example::ReferenceDict(vec![(f.wire_name.clone(), Example::Atom(v.flat()))])
                    } else {
                        Example::Dict(vec![(f.wire_name.clone(), v)])
                    };
                }
                let example_name = if (reference
                    || ep.request_body_required
                    || ep.request_body_has_multipart_related)
                    && f.example.is_some()
                    && f.wire_name
                        .starts_with(|character: char| character.is_ascii_digit())
                {
                    if reference {
                        format!(
                            "f_{}",
                            naming::model_field_name(&f.wire_name)
                                .rsplit('_')
                                .next()
                                .unwrap_or(&f.py_name)
                        )
                    } else {
                        naming::model_field_name(&f.wire_name)
                    }
                } else {
                    f.py_name.clone()
                };
                // An optional field whose example narrows to an empty model is
                // no argument, as a nested one is (see `example_call_args`).
                if !f.spec_required && matches!(&v, Example::Call(_, args) if args.is_empty()) {
                    ctx.restore_recorded(recorded);
                    continue;
                }
                args.push((Some(example_name), v));
            }
        }
        // A form body: required non-file fields, plus any field the media type's
        // own example names (a file cannot be shown as a literal, so Fern omits it
        // from the example). The example's reach is what adds the optional
        // `code`/`redirect_uri`/`code_verifier` of
        // `openbankingproject-ch-kundenbeziehung`'s `/token` beside its two
        // required fields. A *related* multipart body — one whose `encoding`
        // gives a part its own content type — additionally shows every file part
        // in the documentation writers, which is the whole example for
        // `free5gc-pdu-session` (no part is declared required there) and one
        // argument beside the required `descriptor` for VolView's `/stage`.
        Some(RequestBody::Form(form)) => {
            let related = form
                .fields
                .iter()
                .any(|field| field.form_content_type.is_some());
            let mut example_fields: Vec<_> = form
                .fields
                .iter()
                .filter(|f| {
                    // The Markdown writers show every multipart file input,
                    // required or not — DaniWeb's optional `csv` is the corpus's
                    // first in `reference.md`, and the README passes an optional
                    // scan beside a required photo — while the client docstring
                    // shows an optional one only when the media example or a
                    // per-part content type reaches it.
                    (f.spec_required
                        || f.media_example
                        || ((related || documentation) && f.is_file)
                        // A part that is a LIST of files is shown wherever the
                        // example is written, required or not: SFTPGo's optional
                        // `filenames` reaches `README.md` and the client docstring
                        // as well as `reference.md`, where DaniWeb's optional
                        // scalar `csv` reaches only the reference writer.
                        || f.is_file && matches!(f.type_ref, TypeRef::List(_) | TypeRef::Set(_)))
                        // Fern's docstring leaves every file out, even a
                        // required one the call cannot bind without; crozier
                        // passes the required ones (the
                        // `multipart-object-required-file-example` departure).
                        && (documentation
                            || !f.is_file
                            || form.multipart && f.spec_required && !f.optional)
                })
                .collect();
            // Fern's Markdown writers list multipart file inputs before the other
            // fields, even though executable signatures retain the schema's
            // required-first declaration order.
            if form.multipart && documentation {
                example_fields.sort_by_key(|field| !field.is_file);
            }
            for f in example_fields {
                let v = if f.is_file && (documentation || form.multipart) {
                    if matches!(f.type_ref, TypeRef::List(_) | TypeRef::Set(_)) {
                        // A list of files is one flat placeholder list, not an
                        // exploded one: SFTPGo's `filenames` documents
                        // `filenames=["example_filenames"]` on a single line in
                        // both `README.md` and `reference.md`.
                        Example::Atom(format!("[\"example_{}\"]", f.wire_name))
                    } else {
                        Example::Atom(format!("\"example_{}\"", f.wire_name))
                    }
                } else if f.admits_only_empty_object {
                    ctx.empty_object()
                } else {
                    // A declared example is shown where a JSON field's would be:
                    // Zulip's `to` is a union whose example `[9, 10]` Fern passes
                    // over for the sample `"to"`.
                    f.example
                        .as_deref()
                        .filter(|_| {
                            ctx.example_is_scalar(&f.type_ref)
                                || ctx.example_is_composite(&f.type_ref)
                                || ctx.example_is_temporal(&f.type_ref)
                                // A model takes its object example: Zulip's
                                // `draft` is exampled from its annotation.
                                || ctx.example_is_object(&f.type_ref)
                        })
                        .and_then(|example| ctx.value_from_example(&f.type_ref, example))
                        .unwrap_or_else(|| ctx.value(&f.type_ref, Slot::Named(&f.wire_name)))
                };
                args.push((Some(f.py_name.clone()), v));
            }
        }
        _ => {}
    }

    let example_name = if is_async {
        format!("Async{client_name}")
    } else {
        client_name.to_string()
    };
    // The call, rendered at logical indent 0 (sync) or 4 (inside `main`, async).
    let call_indent = if is_async { 4 } else { 0 };
    // A `stream-condition` split shows the streaming half's call in `reference.md`
    // under both halves; every other writer uses the method's own name.
    let method_name = if reference {
        ep.reference_method_name
            .as_deref()
            .unwrap_or(&ep.method_name)
    } else {
        &ep.method_name
    };
    // An async streaming method is an async generator function, which cannot be
    // awaited, so its async examples iterate the call directly (measured at
    // fern-python-sdk 5.41.3; 5.20.0 wrote `await`): exa-gate's docstring
    // documents `response = client.get_proxy_events()` inside `async def main()`,
    // and the README's async client `async for chunk in …: print(chunk)`.
    let awaited = is_async && !ep.streaming;
    let await_kw = if awaited { "await " } else { "" };
    let receiver = if module.is_empty() {
        format!("{await_kw}client.{method_name}")
    } else {
        format!("{await_kw}client.{}.{method_name}", module_path(module))
    };
    // `render` positions continuation lines but leaves the first line for the
    // caller to place, so prepend the base indent to it.
    let pad = " ".repeat(call_indent);
    let call = {
        // The markdown writers render a length-wrapped dict without its magic
        // trailing comma, so the whole argument tree switches dict flavor here
        // rather than at each site that builds one.
        let args = if documentation {
            args.into_iter()
                .map(|(kw, value)| (kw, value.into_documentation_dicts()))
                .collect()
        } else {
            args
        };
        let readme_stream = ep.streaming && documentation && is_async;
        if readme_stream {
            // The README's async client elides the arguments: `(...)` wherever
            // the method takes any (huatuo-node's `watch_events(...)`, beside a
            // sync call passing none), `()` where it takes none (sse-streaming's
            // `streammessages()`). Nothing an argument would import is written.
            ctx.uses_datetime = false;
            ctx.referenced_tag.clear();
            ctx.referenced_tag_doc_order.clear();
        }
        let takes_arguments = !ep.path_params.is_empty()
            || !ep.query_params.is_empty()
            || !ep.header_params.is_empty()
            || ep.request_body.is_some();
        let stream_call = format!("{receiver}({})", if takes_arguments { "..." } else { "" });
        let example = Example::Call(receiver, args);
        let rendered = example.render(call_indent);
        if readme_stream {
            format!("{pad}async for chunk in {stream_call}:\n{pad}    print(chunk)")
        } else if ep.streaming && !documentation {
            let for_kw = if is_async { "async for" } else { "for" };
            // Fern's snippet formatter (ruff at width 80) parenthesizes the
            // assignment's right-hand side when `response = <call>(` overflows
            // and the call's opening line fits once indented a level deeper.
            let head = rendered.lines().next().unwrap_or_default();
            let assigned = if pad.len() + "response = ".len() + head.len() > 80
                && pad.len() + 4 + head.len() <= 80
            {
                format!("(\n{pad}    {}\n{pad})", example.render(call_indent + 4))
            } else {
                rendered
            };
            format!(
                "{pad}response = {assigned}\n{pad}{for_kw} chunk in response:\n{pad}    yield chunk"
            )
        } else if ep.pagination.is_some() && !documentation {
            // A pager's worked example shows both ways of consuming it: item by
            // item, and page by page. The async form separates the two with a
            // blank line; the sync form does not.
            let for_kw = if is_async { "async for" } else { "for" };
            let gap = if is_async { "\n" } else { "" };
            [
                format!("{pad}response = {rendered}"),
                format!("{pad}{for_kw} item in response:"),
                format!("{pad}    yield item"),
                format!("{gap}{pad}# alternatively, you can paginate page-by-page"),
                format!("{pad}{for_kw} page in response.iter_pages():"),
                format!("{pad}    yield page"),
            ]
            .join("\n")
        } else {
            format!("{pad}{rendered}")
        }
    };

    // Preamble stdlib imports: `asyncio` for the async form, `datetime` when a
    // value needs it.
    let mut preamble: Vec<String> = Vec::new();
    if is_async {
        preamble.push("import asyncio".to_string());
    }
    if ctx.uses_datetime {
        preamble.push("import datetime".to_string());
    }

    // `from <pkg> import <client + referenced types>`, alphabetical, wrapped at
    // the snippet's import width (80). Fern's examples are laid out by hand
    // (not `ruff format`) — see `Example::render` for why.
    let mut names: Vec<String> = if documentation && is_async {
        Vec::new()
    } else if documentation {
        ctx.referenced_doc_order.clone()
    } else {
        ctx.referenced.iter().cloned().collect()
    };
    if documentation {
        names.insert(0, example_name.clone());
    } else {
        names.push(example_name.clone());
        // The same discriminated-union ordering the tag-scoped imports below use:
        // Fern writes `StageInputDescriptor_Labelmap` before
        // `StageInputDescriptorLabelmapReferenceImage`, where a plain sort puts
        // the underscore last.
        names.sort_by(|left, right| example_import_cmp(left, right));
    }
    let import_flat = format!("from {pkg} import {}", names.join(", "));
    let import_width = if documentation && !is_async {
        usize::MAX
    } else {
        80
    };
    let import_lines: Vec<String> = if import_flat.len() <= import_width {
        vec![import_flat]
    } else {
        let mut v = vec![format!("from {pkg} import (")];
        for n in &names {
            v.push(format!("    {n},"));
        }
        v.push(")".to_string());
        v
    };
    // Hoisted tag-scoped types import from their tag package, one line per tag,
    // as a separate group above the main import (`from fern.items import ...`).
    let mut tag_import_lines: Vec<String> = Vec::new();
    {
        let mut by_module: Vec<(&str, Vec<&str>)> = Vec::new();
        let mut tag_refs: Vec<(&String, &String)> = if documentation {
            ctx.referenced_tag_doc_order
                .iter()
                .map(|(module, name)| (module, name))
                .collect()
        } else {
            ctx.referenced_tag
                .iter()
                .map(|(module, name)| (module, name))
                .collect()
        };
        if !documentation {
            tag_refs.sort_by(|(left_module, left_name), (right_module, right_name)| {
                left_module
                    .cmp(right_module)
                    .then_with(|| example_import_cmp(left_name, right_name))
            });
        }
        for (module, name) in tag_refs {
            if let Some((_, names)) = by_module.iter_mut().find(|(group, _)| *group == module) {
                names.push(name);
            } else {
                by_module.push((module, vec![name]));
            }
        }
        for (module, tag_names) in by_module {
            // A nested client module imports by its dotted path (CloudPDF's
            // `from fern.doc.signatures import …`).
            let module = module_path(module);
            let flat = format!("from {pkg}.{module} import {}", tag_names.join(", "));
            let import_width = if documentation { usize::MAX } else { 80 };
            if flat.len() > import_width {
                tag_import_lines.push(format!("from {pkg}.{module} import ("));
                tag_import_lines.extend(tag_names.iter().map(|name| format!("    {name},")));
                tag_import_lines.push(")".to_string());
            } else {
                tag_import_lines.push(flat);
            }
        }
    }
    let mut client_args = Vec::new();
    // Method docstrings follow constructor order (global headers, auth). Fern's
    // Markdown snippets instead lead with auth and omit optional global headers.
    if documentation {
        for arg in documentation_client_example_args(
            ctx.auth,
            ctx.global_headers,
            ctx.client_path_parameters,
        ) {
            client_args.push(format!("    {arg},"));
        }
    } else {
        for parameter in ctx.client_path_parameters {
            client_args.push(format!("    {},", client_path_parameter_example(parameter)));
        }
        let leading: Vec<GlobalHeader> = ctx
            .global_headers
            .iter()
            .filter(|h| h.default().is_none())
            .cloned()
            .collect();
        for h in distinct_global_header_params(&leading) {
            client_args.push(format!("    {},", global_header_example(h)));
        }
        // A credential sharing a promoted header's parameter is passed once (the
        // `repeated-credential-example-keyword` departure): Fern repeats it.
        for arg in auth_example_args(ctx.auth) {
            if !ctx
                .global_headers
                .iter()
                .any(|h| h.default().is_none() && keyword_name(&arg) == h.py_name)
            {
                client_args.push(format!("    {arg},"));
            }
        }
    }
    if !ctx.has_environment {
        client_args.push("    base_url=\"https://yourhost.com/path/to/api\",".to_string());
    }
    let client_block = if client_args.is_empty() {
        vec![format!("client = {example_name}()")]
    } else {
        let mut block = vec![format!("client = {example_name}(")];
        block.extend(client_args);
        block.push(")".to_string());
        block
    };

    let mut out: Vec<String> = Vec::new();
    if !preamble.is_empty() {
        out.extend(preamble);
        if !(documentation && is_async && !tag_import_lines.is_empty()) {
            out.push(String::new());
        }
    }
    if documentation || ctx.sdk_first_party {
        if !tag_import_lines.is_empty() {
            out.extend(tag_import_lines);
            out.push(String::new());
        }
        out.extend(import_lines);
    } else {
        // A third-party SDK's imports sort as one group, where the root module
        // precedes its tag packages: `from acme import AcmeApi`, then
        // `from acme.pet import …`, with no blank line between.
        out.extend(import_lines);
        out.extend(tag_import_lines);
    }
    out.push(String::new());
    out.extend(client_block);
    if is_async {
        out.push(String::new());
        out.push(String::new());
        out.push("async def main() -> None:".to_string());
        out.extend(call.split('\n').map(String::from));
        out.push(String::new());
        out.push(String::new());
        out.push("asyncio.run(main())".to_string());
    } else {
        out.extend(call.split('\n').map(String::from));
    }
    if documentation {
        // An untyped value's placeholder stays on its line: Fern writes NextGen's
        // `request={"key": "value"},` and NPQ's model field
        // `attributes={"key": "value"},` flat, where a map-typed one (bunq's
        // `AttachmentPublic`) is wrapped by `compact_documentation_values`.
        let mut untyped_arguments = std::mem::take(&mut ctx.untyped_arguments);
        // So is a body naming a union of nothing but unknowns: People Data
        // Labs' search bodies are `Union[typing.Any]` aliases, and its README
        // writes `request={"key": "value"},` on one line.
        if matches!(&ep.request_body, Some(RequestBody::Single(single))
            if matches!(single.type_ref, TypeRef::Primitive(Prim::Any))
                || ctx.resolves_to_unknown_union(&single.type_ref))
        {
            untyped_arguments.insert("request".to_string());
        }
        Some(format_documentation_example(
            out,
            is_async,
            pkg,
            environment,
            reference,
            ctx.datetime_precedes_tag_import,
            &untyped_arguments,
        ))
    } else {
        Some(out)
    }
}

/// The client-constructor arguments Fern's Markdown snippets pass: auth first,
/// then every *required* global header (optional ones are left out).
fn documentation_client_example_args(
    auth: &Auth,
    global_headers: &[GlobalHeader],
    client_path_parameters: &[ClientPathParameter],
) -> Vec<String> {
    // Fern's Markdown passes a lifted base-path parameter to each method, which
    // does not take it, and leaves it out of the constructor; crozier passes it
    // to the constructor, first, by keyword: its default, or the placeholder the
    // method docstrings use (the `lifted-base-path-docs-examples` departure).
    client_path_parameters
        .iter()
        .map(client_path_parameter_example)
        .chain(documentation_auth_example_args(auth))
        .chain(
            global_headers
                .iter()
                // Nor does it pass an array header, required or not.
                .filter(|header| header.required() && !header.py_type().is_list())
                .map(|header| format!("{}=\"<{}>\"", header.py_name, header.wire_name)),
        )
        .fold(Vec::new(), |mut args: Vec<String>, arg| {
            // Each keyword once (the `repeated-credential-example-keyword`
            // departure): a credential and a promoted header sharing a parameter
            // are one argument, where Fern writes the keyword twice.
            if !args
                .iter()
                .any(|seen| keyword_name(seen) == keyword_name(&arg))
            {
                args.push(arg);
            }
            args
        })
}

fn keyword_name(arg: &str) -> &str {
    arg.split_once('=').map_or(arg, |(name, _)| name)
}

fn documentation_auth_example_args(auth: &Auth) -> Vec<String> {
    match auth {
        Auth::ApiKey { credential, .. } => vec![format!("{}=\"<value>\"", credential.param)],
        Auth::Bearer { credential, .. } => vec![format!("{}=\"<token>\"", credential.param)],
        Auth::Basic {
            username, password, ..
        } => vec![
            format!("{}=\"<username>\"", username.param),
            format!("{}=\"<password>\"", password.param),
        ],
        Auth::None => Vec::new(),
    }
}

fn format_documentation_example(
    lines: Vec<String>,
    is_async: bool,
    pkg: &str,
    environment: Option<&crate::ir::Environment>,
    reference: bool,
    datetime_first: bool,
    untyped_arguments: &BTreeSet<String>,
) -> Vec<String> {
    let client_index = lines
        .iter()
        .position(|line| line.starts_with("client = "))
        .unwrap_or(0);
    let imports: Vec<String> = lines[..client_index]
        .iter()
        .filter(|line| !line.is_empty())
        .cloned()
        .collect();
    let mut out = Vec::new();
    if is_async {
        out.extend_from_slice(&lines[..client_index]);
    } else {
        let main_start = imports
            .iter()
            .position(|line| line.starts_with(&format!("from {pkg} import ")))
            .unwrap_or(0);
        let main_end = if imports[main_start].ends_with('(') {
            imports[main_start..]
                .iter()
                .position(|line| line == ")")
                .map_or(main_start, |offset| main_start + offset)
        } else {
            main_start
        };
        out.extend_from_slice(&imports[main_start..=main_end]);
        if let Some(environment) = environment {
            out.push(format!(
                "from {pkg}.environment import {}",
                environment.enum_name
            ));
        }
        // The package import leads; the stdlib line and the tag imports follow in
        // the order the snippet first uses them.
        let stdlib = imports
            .iter()
            .filter(|line| line.starts_with("import "))
            .cloned();
        let from_imports = imports
            .iter()
            .enumerate()
            .filter(|(index, line)| {
                (*index < main_start || *index > main_end) && !line.starts_with("import ")
            })
            .map(|(_, line)| line.clone());
        if datetime_first {
            out.extend(stdlib);
            out.extend(from_imports);
        } else {
            out.extend(from_imports);
            out.extend(stdlib);
        }
        out.push(String::new());
    }
    let mut body = lines[client_index..].to_vec();
    if let Some(environment) = environment {
        let default_arg = format!("    environment={},", environment.default_ref());
        if body.first().is_some_and(|line| line.ends_with("()")) {
            let class = body[0].trim_end_matches("()").to_string();
            body.splice(0..1, [format!("{class}("), default_arg, ")".to_string()]);
        } else if let Some(close) = body.iter().position(|line| line == ")") {
            body.insert(close, default_arg);
        }
    }
    if !is_async {
        if body.first().is_some_and(|line| line.ends_with("()")) {
            body.insert(1, String::new());
        } else if let Some(close) = body.iter().position(|line| line == ")") {
            if body.get(close + 1).is_some_and(|line| !line.is_empty()) {
                body.insert(close + 1, String::new());
            }
        }
    }
    out.extend(body);
    compact_documentation_values(out, reference, untyped_arguments)
}

fn compact_documentation_values(
    lines: Vec<String>,
    reference: bool,
    untyped_arguments: &BTreeSet<String>,
) -> Vec<String> {
    let mut compact = Vec::with_capacity(lines.len());
    let mut index = 0;
    while index < lines.len() {
        if index + 2 < lines.len()
            && lines[index].contains("datetime.")
            && lines[index].trim_end().ends_with(".fromisoformat(")
            && matches!(lines[index + 2].trim(), ")" | "),")
        {
            let value = lines[index + 1].trim().trim_end_matches(',');
            let comma = if lines[index + 2].trim().ends_with(',') {
                ","
            } else {
                ""
            };
            compact.push(format!("{}{value}){comma}", lines[index]));
            index += 3;
            continue;
        }
        let line = &lines[index];
        let trimmed = line.trim();
        if let Some((head, items)) = trimmed
            .strip_suffix("],")
            .and_then(|value| value.split_once("=["))
            .filter(|(_, items)| !items.is_empty())
            // A multipart part that is a LIST of files is the one list the
            // markdown writers leave on its own line: SFTPGo documents
            // `filenames=["example_filenames"]` flat, where every other list
            // argument of the same writers is exploded one item per line.
            .filter(|(head, items)| *items != format!("\"example_{}\"", head.trim()))
        {
            let indent = line.len() - line.trim_start().len();
            compact.push(format!("{}{head}=[", " ".repeat(indent)));
            let items: Vec<&str> = items.split(", ").collect();
            compact.extend(items.iter().enumerate().map(|(index, item)| {
                let comma = if index + 1 == items.len() { "" } else { "," };
                format!("{}{item}{comma}", " ".repeat(indent + 4))
            }));
            compact.push(format!("{}],", " ".repeat(indent)));
            index += 1;
            continue;
        }
        // The Markdown writers wrap a one-pair dict argument onto its own lines:
        // bunq's `{"key": "value"}` placeholder and the Auto Agent Protocol's
        // `data={"type": "dealer.information.request"}` alike.
        if !reference {
            if let Some((head, item)) = trimmed
                .strip_suffix("},")
                .and_then(|value| value.split_once("={"))
                .filter(|(_, item)| {
                    item.starts_with('"')
                        && item.contains("\": ")
                        && !item.contains(['{', '[', '}', ']'])
                        && item.matches("\", \"").count() == 0
                })
                .filter(|(head, _)| !untyped_arguments.contains(*head))
            {
                let indent = line.len() - line.trim_start().len();
                compact.push(format!("{}{head}={{", " ".repeat(indent)));
                compact.push(format!("{}{item}", " ".repeat(indent + 4)));
                compact.push(format!("{}}},", " ".repeat(indent)));
                index += 1;
                continue;
            }
        }
        if line.trim_end().ends_with(".0,") {
            compact.push(format!("{},", line.trim_end_matches(".0,")));
        } else {
            compact.push(line.clone());
        }
        index += 1;
    }
    for index in 0..compact.len().saturating_sub(1) {
        if compact[index].trim_end().ends_with(',') && compact[index + 1].trim().starts_with(']') {
            compact[index].pop();
        }
    }
    compact
}

/// Whether a binary download is one Fern documents with no worked example: a
/// root-client method of a 3.0 document whose success schema itself declares
/// `format: binary`. Its docstring has no `Examples`, and the README anchors on
/// the next eligible operation instead (`binary-download-with-body-has-no-example`).
fn root_binary_download_undocumented(ep: &Endpoint) -> bool {
    ep.binary_response && ep.module.is_empty() && ep.binary_schema_response && !ep.openapi_31
}

fn endpoint_has_worked_example(ep: &Endpoint) -> bool {
    // Array query params always default to `None` in the generated signature, so a
    // binary download with only those params has no exampleable required argument.
    // That is a `GET`'s loss alone: HuaTuo's Pyroscope queries are argument-less
    // `POST`s streaming bytes, and Fern documents each of them.
    let binary_get_has_no_required_args = ep.http_method == "GET"
        && ep.path_params.is_empty()
        && ep
            .query_params
            .iter()
            .all(|param| matches!(param.type_ref, TypeRef::List(_)))
        && ep.header_params.is_empty()
        && ep.request_body.is_none();
    // A binary download substitutes the parameter's own name for each path
    // placeholder whatever `pattern` the schema declares: discord's
    // `get_guild_widget_png` documents `guild_id="guild_id"` although its
    // `SnowflakeType` is `^(0|[1-9][0-9]*)$` (measured at fern-python-sdk 5.41.3;
    // 5.20.0 dropped the whole example).
    let opaque_multipart_example = ep.body_media_has_example
        && matches!(&ep.request_body, Some(RequestBody::Form(form)) if form.fields.iter().any(|field| field.is_file));
    // A list-typed path parameter beside a request body defeats Fern's example
    // generator: it renders every path parameter as a plain string, which the
    // list type rejects once a body makes the wrapper validate, so the endpoint
    // gets no worked example at all (DaniWeb's `POST /conversations/{ID}/schedules`
    // and `/searches`, and `POST /groups/{ID}/schedules`). Either half alone still
    // gets one — `GET /apps/{ID}` shows the bogus `id="ID"`, and every scalar-path
    // body endpoint of the same document keeps its example.
    // …and only when that body is one the example actually passes: braintrust's
    // catch-all proxy takes a `Sequence[str]` path parameter beside an optional
    // unknown body the example omits, and Fern documents it.
    let body_shown = match &ep.request_body {
        Some(RequestBody::Single(single)) => {
            single.required || single.example.is_some() || !is_any_type(&single.type_ref)
        }
        _ => true,
    };
    let list_path_param_with_body = ep.request_body.is_some()
        && body_shown
        && ep
            .path_params
            .iter()
            .any(|param| matches!(param.type_ref, TypeRef::List(_)));
    // A binary download the importer declines has only Fern's IR-generated
    // examples, and the success one fails (*File download unsupported*): what
    // the docstring shows is the first *error* example, so a download declaring
    // no error response shows none. Codat Assess's `get_excel_report` answers a
    // lone `200` of `application/octet-stream` and its golden has no example.
    let binary_download_without_errors =
        ep.binary_response && ep.importer_example_missing && ep.errors.is_empty();
    !(ep.binary_response && binary_get_has_no_required_args
        || binary_download_without_errors
        || opaque_multipart_example
        || list_path_param_with_body)
}

/// Assemble the `raw_client.py` file for one client module: the sync and async
/// raw client classes, each wrapping every operation.
fn raw_client_file(
    env: &Environment<'static>,
    pkg: &str,
    root_client_name: &str,
    module: &str,
    endpoints: &[&Endpoint],
    tag_types: &BTreeMap<String, String>,
) -> Result<GeneratedFile> {
    let loc = if module.is_empty() {
        RefLoc::PackageRoot
    } else {
        RefLoc::Client(module.to_string())
    };
    let mut imports = Imports::at(loc.clone(), tag_types);
    // Imports every raw client needs regardless of operation shape — except for a
    // parent client, whose two classes hold nothing but the wrapper they were
    // constructed with, so only the wrapper types are imported.
    imports.add_core("client_wrapper", "AsyncClientWrapper");
    imports.add_core("client_wrapper", "SyncClientWrapper");
    if !endpoints.is_empty() {
        imports.add_plain("typing");
        imports.add_from("json.decoder", "JSONDecodeError");
        imports.add_core("api_error", "ApiError");
        // A client every one of whose methods returns a pager never names the
        // buffered response wrapper, so Fern does not import it.
        if endpoints.iter().any(|ep| ep.pagination.is_none()) {
            imports.add_core("http_response", "AsyncHttpResponse");
            imports.add_core("http_response", "HttpResponse");
        }
        imports.add_core("request_options", "RequestOptions");
    }

    let class_stem = if module.is_empty() {
        root_client_name.to_string()
    } else {
        naming::to_pascal_case(module_stem(module))
    };
    let sync_class = if module.is_empty() {
        format!("Raw{class_stem}")
    } else {
        format!("Raw{class_stem}Client")
    };
    let async_class_name = if module.is_empty() {
        format!("AsyncRaw{class_stem}")
    } else {
        format!("AsyncRaw{class_stem}Client")
    };
    // A first pass registers every import the two classes need; a name arriving
    // from two modules (a component schema called `ApiError` beside the core one)
    // is aliased in both, and the real pass writes those aliases at every
    // occurrence. Rendering twice is what lets the *body* carry the alias — the
    // collision is only knowable once every reference has been walked.
    {
        let mut probe = Imports::at(loc, tag_types);
        probe.from = imports.from.clone();
        raw_client_class(
            &sync_class,
            "SyncClientWrapper",
            endpoints,
            false,
            &mut probe,
        );
        raw_client_class(
            &async_class_name,
            "AsyncClientWrapper",
            endpoints,
            true,
            &mut probe,
        );
        imports.aliases = probe.collision_aliases();
    }
    let sync = raw_client_class(
        &sync_class,
        "SyncClientWrapper",
        endpoints,
        false,
        &mut imports,
    );
    let async_class = raw_client_class(
        &async_class_name,
        "AsyncClientWrapper",
        endpoints,
        true,
        &mut imports,
    );
    // A module with any request body declares the `OMIT` sentinel above the
    // classes; `omit=OMIT` drops unset optional fields from the serialized body.
    let omit = if endpoints.iter().any(|e| e.request_body.is_some()) {
        "OMIT = typing.cast(typing.Any, ...)\n\n\n"
    } else {
        ""
    };
    let body = format!("{omit}{sync}\n\n\n{async_class}");

    let contents = render(
        env,
        "raw_client.py",
        module,
        context! {
            header => HEADER,
            imports => imports.render(),
            body => body,
        },
    )?;
    Ok(GeneratedFile {
        path: if module.is_empty() {
            PathBuf::from(format!("src/{pkg}/raw_client.py"))
        } else {
            PathBuf::from(format!("src/{pkg}/{module}/raw_client.py"))
        },
        contents,
    })
}

/// Render one raw-client class: the `__init__` binding the client wrapper, then
/// one method per operation separated by a blank line.
fn raw_client_class(
    class_name: &str,
    wrapper_type: &str,
    endpoints: &[&Endpoint],
    is_async: bool,
    imports: &mut Imports,
) -> String {
    let mut out = format!(
        "class {class_name}:\n    def __init__(self, *, client_wrapper: {wrapper_type}):\n        self._client_wrapper = client_wrapper"
    );
    for ep in endpoints {
        out.push_str("\n\n");
        out.push_str(&raw_method(ep, is_async, imports));
    }
    out
}

/// Render a named template with a context, mapping errors to [`Error::Render`].
fn render(
    env: &Environment<'static>,
    template: &str,
    file: &str,
    ctx: minijinja::value::Value,
) -> Result<String> {
    env.get_template(template)
        .and_then(|t| t.render(ctx))
        .map_err(|e| Error::Render {
            file: file.to_string(),
            message: e.to_string(),
        })
}

/// Remove the generated package tree (`src/<package>`) before a fresh write so
/// regeneration is idempotent: a schema or endpoint removed from the spec must
/// not leave an orphaned module behind. Scoped to `src/<package>`, which is
/// entirely crozier's own namespace — anything the user keeps elsewhere under
/// `--output` is untouched. A missing tree is not an error (first run).
pub fn clean_package_tree(root: &std::path::Path, package: &str) -> Result<()> {
    let tree = root.join("src").join(package);
    match std::fs::remove_dir_all(&tree) {
        Ok(()) => Ok(()),
        Err(source) if source.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(source) => Err(Error::WriteOutput { path: tree, source }),
    }
}

/// The flat layout's counterpart of [`clean_package_tree`]: there the package *is*
/// the output root, so a previous generation's modules sit directly under it.
///
/// A root counts as a previous generation only when it carries the
/// `.fern/metadata.json` crozier writes; any other directory (a fresh one, or one
/// the user owns) is written into without deleting anything. A previous
/// generation loses every entry except the dot-entries beside `.fern/` (`.git`,
/// `.fernignore`, editor state), which crozier never writes.
pub fn clean_flat_tree(root: &std::path::Path) -> Result<()> {
    if !root.join(".fern").join("metadata.json").is_file() {
        return Ok(());
    }
    let entries = std::fs::read_dir(root)
        .and_then(|dir| dir.collect::<std::io::Result<Vec<_>>>())
        .map_err(|source| Error::WriteOutput {
            path: root.to_path_buf(),
            source,
        })?;
    for entry in entries {
        let name = entry.file_name();
        if name.to_string_lossy().starts_with('.') && name != ".fern" {
            continue;
        }
        let path = entry.path();
        let removed = if path.is_dir() && !path.is_symlink() {
            std::fs::remove_dir_all(&path)
        } else {
            std::fs::remove_file(&path)
        };
        removed.map_err(|source| Error::WriteOutput { path, source })?;
    }
    Ok(())
}

/// Format every crozier-*generated* `.py` file in place with `ruff format`,
/// delegating line-wrapping to the tool Fern uses. Two classes of file are left
/// untouched:
///
/// - **Vendored Python assets** — the `core/` runtime, default-client helper, and
///   generated aiohttp tests are already Fern-formatted. Re-formatting would
///   change their post-comment-strip whitespace and break the byte match.
/// - **Whitespace-only files** — the empty `py.typed` marker and the comment-only
///   endpoint `__init__.py` (emitted in its four-blank-line stripped form): there
///   is nothing to wrap and `ruff` would collapse them.
///
/// The lazy-loader `__init__.py` aggregators *are* formatted: they overflow (long
/// `_dynamic_imports`/`__all__`/import lines) and `ruff` wraps them like any other
/// file. Their leading blank lines collapse under `ruff`, but that is a
/// comment-strip artifact the comparison drops on both sides (`crate::parity`),
/// so the byte match is preserved.
fn format_python_files(pkg: &str, files: &mut [GeneratedFile]) -> Result<()> {
    let core_root = PathBuf::from(format!("src/{pkg}/core"));
    let root_client = PathBuf::from(format!("src/{pkg}/client.py"));
    let root_raw_client = PathBuf::from(format!("src/{pkg}/raw_client.py"));
    let default_clients = PathBuf::from(format!("src/{pkg}/_default_clients.py"));
    let generated_aiohttp_test = PathBuf::from("tests/test_aiohttp_autodetect.py");
    let generated_conftest = PathBuf::from("tests/conftest.py");
    for file in files.iter_mut() {
        let is_py = file.path.extension().and_then(|e| e.to_str()) == Some("py");
        // The `core/` tree is vendored verbatim (already ruff-formatted) EXCEPT
        // `client_wrapper.py`, which crozier generates from the auth/global-header
        // model and so must be wrapped like every other generated file.
        let is_vendored = file.path.starts_with(&core_root)
            && file.path.file_name().and_then(|n| n.to_str()) != Some("client_wrapper.py");
        let is_vendored_scaffolding = file.path == default_clients
            || file.path == generated_aiohttp_test
            || file.path == generated_conftest;
        let has_code = file.contents.chars().any(|c| !c.is_whitespace());
        if is_py && !is_vendored && !is_vendored_scaffolding && has_code {
            // Fern's code writer replaces every tab it writes with four spaces
            // (`WriterImpl.write`, `TAB_LENGTH = 4`): People Data Labs' field
            // description `HQ country\tunited states` is four spaces apart.
            if file.contents.contains('\t') {
                file.contents = file.contents.replace('\t', "    ");
            }
            let name = file.path.to_string_lossy();
            file.contents =
                crate::pyfmt::format_source(&name, &file.contents, crate::pyfmt::LINE_LENGTH)?;
            if file.path == root_client || file.path == root_raw_client {
                preserve_fenced_docstring_blank_indent(&mut file.contents);
            }
        }
    }
    Ok(())
}

fn preserve_fenced_docstring_blank_indent(source: &mut String) {
    let had_final_newline = source.ends_with('\n');
    let mut lines: Vec<String> = source.lines().map(String::from).collect();
    let mut start = 0;
    while start < lines.len() {
        if lines[start] != "        \"\"\"" {
            start += 1;
            continue;
        }
        let Some(relative_end) = lines[start + 1..]
            .iter()
            .position(|line| line == "        \"\"\"")
        else {
            break;
        };
        let end = start + 1 + relative_end;
        if lines[start + 1..end]
            .iter()
            .any(|line| line.trim_start().starts_with("```"))
        {
            for line in &mut lines[start + 1..end] {
                if line.is_empty() {
                    *line = "        ".to_string();
                }
            }
        }
        start = end + 1;
    }
    *source = lines.join("\n");
    if had_final_newline {
        source.push('\n');
    }
}

/// Write each generated file under `root`, creating parent directories as needed.
pub fn write_files(root: &std::path::Path, files: &[GeneratedFile]) -> Result<()> {
    for file in files {
        let full = root.join(&file.path);
        if let Some(parent) = full.parent() {
            std::fs::create_dir_all(parent).map_err(|source| Error::WriteOutput {
                path: parent.to_path_buf(),
                source,
            })?;
        }
        std::fs::write(&full, &file.contents).map_err(|source| Error::WriteOutput {
            path: full.clone(),
            source,
        })?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{
        abbrev_call, auth_client_parts, auth_example_args, auth_wrapper_parts,
        build_documentation_example, build_example, client_method, cycle_components, environment,
        escape_py_str, example_from_json_as, example_import_cmp, field_decl, forward_repair_map,
        generate, natural_cmp, path_field_render, path_object_decl, path_object_documented,
        raw_method, raw_type_str, readme_endpoint, readme_endpoint_eligible, reference_entry,
        reference_param_annotation, render, render_class_body, render_enum, render_type_decl,
        root_sub_client_alias, root_sub_client_import, url_arg, BodySchemaShape, ClientCtx,
        DeclSettings, Example, ExampleCtx, FieldView, ForwardRepair, Imports, ParamRow, RefLoc,
        ReferenceEntryView, RenderedField, RootClientView, RootModuleView, Slot,
    };
    use crate::ir::Credential;
    use crate::ir::{
        AliasType, Auth, BodyField, DiscriminatedUnion, Endpoint, EnumMember, EnumType,
        ErrorResponse, Field, FormBody, GlobalHeader, HeaderParam, HeaderType, Ir, ObjectType,
        PathParam, Prim, QueryParam, RequestBody, SingleBody, TagTypeDecl, TypeDecl, TypeRef,
        UnionMember,
    };
    use crate::wrap::Doc;

    /// Every file the generator writes for `document`, before `ruff format`.
    fn files_for(document: serde_json::Value) -> Vec<(String, String)> {
        let doc: crate::openapi::OpenApi =
            serde_json::from_value(document).expect("document deserializes");
        let config = crate::config::GenerateConfig::new(
            std::path::PathBuf::from("openapi.yml"),
            std::path::PathBuf::from("out"),
            Some("api".to_string()),
            None,
            None,
            crate::settings::ExtraFields::default(),
            "Api",
        )
        .expect("the config is well formed");
        generate(&crate::ir::build(&doc, &config))
            .expect("the document generates")
            .into_iter()
            .map(|file| {
                (
                    file.path.to_string_lossy().replace('\\', "/"),
                    file.contents,
                )
            })
            .collect()
    }

    #[test]
    fn the_ir_fallback_example_sizes_strings_by_their_declared_lengths() {
        let files = files_for(serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "paths": {"/": {"post": {
                "operationId": "send",
                "requestBody": {"required": true, "content": {"application/json": {
                    "schema": {"type": "object", "required": ["message"], "properties": {
                        "message": {"$ref": "#/components/schemas/Message"}
                    }}
                }}},
                "responses": {"204": {"description": "OK"}}
            }}},
            "components": {"schemas": {
                "ToNumber": {"type": "string", "minLength": 7, "maxLength": 15,
                    "example": "447700900000"},
                "FromNumber": {"type": "string", "example": "447700900001"},
                "Message": {
                    "type": "object", "required": ["to", "from", "code", "text"],
                    "properties": {
                        "to": {"$ref": "#/components/schemas/ToNumber"},
                        "from": {"$ref": "#/components/schemas/FromNumber"},
                        "code": {"type": "string", "maxLength": 4, "example": "ab12"},
                        "text": {"description": "Untyped and unexampled."}
                    }
                }
            }}
        }));
        let client = files
            .iter()
            .find(|(path, _)| path.ends_with("/client.py") && path.matches('/').count() == 2)
            .map(|(_, contents)| contents)
            .expect("the root client is generated");
        // The untyped required `text` leaves Fern's importer no example, so its
        // IR fallback writes the call, reading no declared example.
        assert!(
            client.contains("to=\"apricot\",")
                && client.contains("from_=\"from\",")
                && client.contains("code=\"buzz\",")
                && !client.contains("447700900000"),
            "{client}"
        );
    }

    #[test]
    fn a_flat_example_for_a_list_of_unknown_lists_examples_empty_lists() {
        let files = files_for(serde_json::json!({
            "openapi": "3.1.0",
            "info": {"title": "Api", "version": "1"},
            "paths": {"/tags": {"put": {
                "operationId": "setTags",
                "requestBody": {"required": true, "content": {"application/json": {
                    "schema": {"type": "array", "items": {
                        "type": "array",
                        "prefixItems": [{"type": "string"}, {"type": "string"}],
                        "items": false
                    }},
                    "example": ["title", "My title"]
                }}},
                "responses": {"204": {"description": "OK"}}
            }}}
        }));
        let client = files
            .iter()
            .find(|(path, _)| path.ends_with("/client.py") && path.matches('/').count() == 2)
            .map(|(_, contents)| contents)
            .expect("the root client is generated");
        assert!(
            client.contains("request=[[], []]") && !client.contains("\"My title\""),
            "{client}"
        );
    }

    /// `GET /probe` carrying the header `probeParam` with `schema`, beside a
    /// `GET /other` that carries it only when `everywhere` is set.
    fn header_array_document(
        schema: serde_json::Value,
        required: bool,
        everywhere: bool,
    ) -> serde_json::Value {
        let header = serde_json::json!([
            {"name": "probeParam", "in": "header", "required": required, "schema": schema}
        ]);
        let other = if everywhere {
            header.clone()
        } else {
            serde_json::json!([])
        };
        serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "paths": {
                "/probe": {"get": {"operationId": "probe", "parameters": header,
                    "responses": {"204": {"description": "OK"}}}},
                "/other": {"get": {"operationId": "other", "parameters": other,
                    "responses": {"204": {"description": "OK"}}}}
            }
        })
    }

    /// The `_AUTH_HEADERS` line of the `core/http_client.py` emitted for a
    /// document whose only security scheme is `scheme`.
    fn auth_headers_line(scheme: serde_json::Value) -> String {
        let files = files_for(serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "components": {"securitySchemes": {"credential": scheme}},
            "security": [{"credential": []}],
            "paths": {"/probe": {"get": {"operationId": "probe",
                "responses": {"204": {"description": "OK"}}}}}
        }));
        let (_, http_client) = files
            .iter()
            .find(|(path, _)| path == "src/api/core/http_client.py")
            .expect("core/http_client.py is emitted");
        http_client
            .lines()
            .find(|line| line.starts_with("_AUTH_HEADERS"))
            .expect("http_client.py declares _AUTH_HEADERS")
            .to_owned()
    }

    #[test]
    fn an_api_key_header_is_redacted_by_name_and_a_bearer_token_is_not_listed() {
        assert_eq!(
            auth_headers_line(
                serde_json::json!({"type": "apiKey", "in": "header", "name": "X-Api-Key"})
            ),
            "_AUTH_HEADERS: typing.FrozenSet[str] = frozenset({\"x-api-key\"})"
        );
        // `Authorization` is in the runtime's own sensitive set, so Fern lists no
        // bearer or basic credential's header here.
        for scheme in [
            serde_json::json!({"type": "http", "scheme": "bearer"}),
            serde_json::json!({"type": "http", "scheme": "basic"}),
        ] {
            assert_eq!(
                auth_headers_line(scheme),
                "_AUTH_HEADERS: typing.FrozenSet[str] = frozenset()"
            );
        }
    }

    /// The `src/api/{path}` file emitted for a document whose only security scheme
    /// is `scheme`, required on every operation when `required` is set.
    fn emitted_for_scheme(
        scheme: serde_json::Value,
        required: bool,
        servers: serde_json::Value,
        path: &str,
    ) -> String {
        let security = if required {
            serde_json::json!([{"credential": []}])
        } else {
            serde_json::json!([])
        };
        files_for(serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "servers": servers,
            "components": {"securitySchemes": {"credential": scheme}},
            "security": security,
            "paths": {"/probe": {"get": {"operationId": "probe",
                "responses": {"204": {"description": "OK"}}}}}
        }))
        .into_iter()
        .find(|(emitted, _)| *emitted == format!("src/api/{path}"))
        .map(|(_, contents)| contents)
        .unwrap_or_else(|| panic!("{path} is emitted"))
    }

    #[test]
    fn a_bearer_wrapper_skips_the_sync_token_when_an_async_one_is_set() {
        let bearer = serde_json::json!({"type": "http", "scheme": "bearer"});
        for required in [true, false] {
            let wrapper = emitted_for_scheme(
                bearer.clone(),
                required,
                serde_json::json!([]),
                "core/client_wrapper.py",
            );
            assert!(
                wrapper.contains(
                    "    def get_headers(self, *, include_token: bool = True) -> typing.Dict[str, str]:\n"
                ),
                "{wrapper}"
            );
            let token_write = if required {
                "        if include_token:\n            headers[\"Authorization\"] = f\"Bearer {self._get_token()}\"\n"
            } else {
                "        if include_token:\n            token = self._get_token()\n            if token is not None:\n                headers[\"Authorization\"] = f\"Bearer {token}\"\n"
            };
            assert!(wrapper.contains(token_write), "{wrapper}");
            assert!(
                wrapper.contains(
                    "        headers = self.get_headers(include_token=self._async_token is None)\n"
                ),
                "{wrapper}"
            );
        }
        // Any other credential keeps the flag-free `get_headers`.
        for scheme in [
            serde_json::json!({"type": "apiKey", "in": "header", "name": "X-Api-Key"}),
            serde_json::json!({"type": "http", "scheme": "basic"}),
        ] {
            let wrapper = emitted_for_scheme(
                scheme,
                true,
                serde_json::json!([]),
                "core/client_wrapper.py",
            );
            assert!(
                wrapper.contains("    def get_headers(self) -> typing.Dict[str, str]:\n"),
                "{wrapper}"
            );
            assert!(!wrapper.contains("include_token"), "{wrapper}");
            assert!(
                wrapper.contains("        headers = self.get_headers()\n"),
                "{wrapper}"
            );
        }
    }

    #[test]
    fn a_server_variable_override_formats_the_chosen_environments_template() {
        let client = emitted_for_scheme(
            serde_json::json!({"type": "apiKey", "in": "header", "name": "X-Api-Key"}),
            true,
            serde_json::json!([
                {"url": "https://{region}.example.com", "description": "Production",
                    "variables": {"region": {"default": "eu"}}},
                {"url": "https://sandbox.example.com", "description": "Sandbox"},
            ]),
            "client.py",
        );
        // Only the server declaring variables has a template, and a caller's
        // `base_url` wins over the formatted one.
        assert!(
            client.contains(concat!(
                "        if region is not None:\n",
                "            _region = region if region is not None else \"eu\"\n",
                "            _environment_url_templates = {\n",
                "                ApiApiEnvironment.PRODUCTION: \"https://{region}.example.com\",\n",
                "            }\n",
                "            _url_template = _environment_url_templates.get(environment, \"https://{region}.example.com\")\n",
                "            if base_url is None:\n",
                "                base_url = _url_template.format(region=_region)\n",
            )),
            "{client}"
        );
    }

    #[test]
    fn multipart_json_module_imports_follow_each_methods_shadowing() {
        for order in [[true, false], [false, true]] {
            let mut imports = super::Imports::default();
            for shadowed in order {
                assert_eq!(
                    imports.json_module(shadowed),
                    if shadowed { "_json" } else { "json" }
                );
            }
            assert_eq!(imports.render(), "import json\nimport json as _json");
        }
        let mut imports = super::Imports::default();
        imports.json_module(true);
        assert_eq!(imports.render(), "import json as _json");
    }

    #[test]
    fn a_method_array_header_is_a_sequence_documented_as_a_list() {
        let files = files_for(header_array_document(
            serde_json::json!({"type": "array", "items": {"type": "string"}}),
            true,
            false,
        ));
        let client = file(&files, "src/api/client.py");
        assert!(
            client.contains("probe_param: typing.Sequence[str]"),
            "{client}"
        );
        // Fern fills an array header's example with two copies of its name.
        assert!(
            client.contains("probe_param=[\"probeParam\", \"probeParam\"]"),
            "{client}"
        );
        let reference = file(&files, "reference.md");
        assert!(
            reference.contains("**probe_param:** `typing.List[str]`"),
            "{reference}"
        );
        // And the abbreviated README calls take no `...` for it.
        let readme = file(&files, "README.md");
        assert!(readme.contains("client.probe()"), "{readme}");
        assert!(!readme.contains("client.probe(...)"), "{readme}");
    }

    #[test]
    fn a_promoted_array_header_is_a_list_field_left_out_of_markdown_examples() {
        let files = files_for(header_array_document(
            serde_json::json!({"type": "array", "items": {"type": "string"}}),
            true,
            true,
        ));
        let wrapper = file(&files, "src/api/core/client_wrapper.py");
        assert!(
            wrapper.contains("probe_param: typing.List[str],"),
            "{wrapper}"
        );
        assert!(
            wrapper.contains("headers[\"probeParam\"] = str(self._probe_param)"),
            "{wrapper}"
        );
        let readme = file(&files, "README.md");
        assert!(!readme.contains("probe_param=\"<probeParam>\""), "{readme}");
    }

    #[test]
    fn a_defaulted_promoted_header_trails_logging_and_sends_its_default() {
        let files = files_for(header_array_document(
            serde_json::json!({"type": "array", "items": {"type": "string"}, "default": "all"}),
            false,
            true,
        ));
        let wrapper = file(&files, "src/api/core/client_wrapper.py");
        let logging = "        logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,\n";
        let field = "        probe_param: typing.Optional[str] = None,\n";
        assert_eq!(
            wrapper.matches(&format!("{logging}{field}")).count(),
            3,
            "{wrapper}"
        );
        assert!(wrapper.contains(
            "        self._logging = logging\n        self._probe_param = probe_param\n"
        ));
        assert!(wrapper.contains(
            "headers[\"probeParam\"] = self._probe_param if self._probe_param is not None else \"all\""
        ));
        assert_eq!(
            wrapper
                .matches("            logging=logging,\n            probe_param=probe_param,\n")
                .count(),
            2,
            "{wrapper}"
        );
        let client = file(&files, "src/api/client.py");
        assert!(
            client.contains(&format!("{logging}{field}    ):")),
            "{client}"
        );
        assert!(
            client.contains("    probe_param : typing.Optional[str]\n    Examples"),
            "{client}"
        );
        assert!(
            client.contains("            logging=logging,\n            probe_param=probe_param,\n")
        );
        assert!(
            !client.contains("probe_param=\"YOUR_PROBE_PARAM\""),
            "{client}"
        );
    }

    fn file<'a>(files: &'a [(String, String)], suffix: &str) -> &'a str {
        files
            .iter()
            .find(|(path, _)| path.ends_with(suffix))
            .map(|(_, contents)| contents.as_str())
            .unwrap_or_else(|| panic!("no generated file ends with {suffix}"))
    }

    #[test]
    fn a_nested_client_package_owns_its_types_and_its_typed_header() {
        let operation = |group: serde_json::Value, method: &str| {
            serde_json::json!({
                "x-fern-sdk-group-name": group,
                "x-fern-sdk-method-name": method,
                "parameters": [{
                    "name": "Request-Timeout", "in": "header",
                    "schema": { "type": "integer" }
                }],
                "requestBody": { "content": { "application/json": { "schema": {
                    "type": "object",
                    "properties": { "unit": { "type": "string", "enum": ["mm", "pt"] } }
                } } } },
                "responses": { "200": { "description": "ok" } }
            })
        };
        let files = files_for(serde_json::json!({
            "openapi": "3.0.3",
            "info": { "title": "nested", "version": "1" },
            "paths": {
                "/doc/scale": { "post": operation(serde_json::json!(["doc", "pages"]), "setScale") },
                "/doc/rotate": { "post": operation(serde_json::json!(["doc", "pages"]), "rotate") }
            }
        }));
        // CloudPDF's layout: the nested package's types are re-exported one level
        // up, and not at the root.
        let parent = file(&files, "src/api/doc/__init__.py");
        assert!(parent.contains("from .pages import"), "{parent}");
        assert!(
            parent.contains("\"SetScalePagesRequestUnit\": \".pages\""),
            "{parent}"
        );
        let root = file(&files, "src/api/__init__.py");
        assert!(!root.contains("SetScalePagesRequestUnit"), "{root}");
        // Its enum imports `core` from four levels up.
        let unit = file(&files, "doc/pages/types/set_scale_pages_request_unit.py");
        assert!(unit.contains("from ....core import enum"), "{unit}");
        // Milvus's integer `Request-Timeout`, promoted to the wrapper.
        let wrapper = file(&files, "src/api/core/client_wrapper.py");
        assert!(
            wrapper.contains("request_timeout: typing.Optional[int] = None"),
            "{wrapper}"
        );
        assert!(
            wrapper.contains("headers[\"Request-Timeout\"] = str(self._request_timeout)"),
            "{wrapper}"
        );
    }

    /// Render a class body through the real `class_body.py` template — the fast
    /// feedback loop for layout/filter changes (no binary, no `ruff`). Returns the
    /// body exactly as emitted, before the file-level `ruff format` pass.
    fn class_body(docstring: Option<&str>, fields: Vec<FieldView>) -> String {
        render_class_body(
            &environment(),
            docstring.map(str::to_string),
            fields,
            Some(crate::settings::ExtraFields::Allow),
        )
        .expect("renders")
    }

    /// Render any registered template with a serializable view — the direct
    /// "minijinja in the loop" harness for the presentational templates.
    fn render_tmpl(name: &str, view: impl serde::Serialize) -> String {
        render(
            &environment(),
            name,
            name,
            minijinja::Value::from_serialize(&view),
        )
        .expect("renders")
    }

    fn field(decl: &str, docstring: Option<&str>) -> FieldView {
        FieldView {
            decl: decl.to_string(),
            docstring: docstring.map(str::to_string),
        }
    }

    #[test]
    fn example_literals_parse_as_json_whether_python_or_json_spelled() {
        assert_eq!(
            super::parse_example_value(r#"[{"on": True, "off": False, "none": None}]"#),
            Some(serde_json::json!([{"on": true, "off": false, "none": null}]))
        );
        // Inside a string the Python words are text.
        assert_eq!(
            super::parse_example_value(r#"{"text": "True \"None\"", "on": True}"#),
            Some(serde_json::json!({"text": "True \"None\"", "on": true}))
        );
        assert_eq!(
            super::parse_example_value("[1, 2]"),
            Some(serde_json::json!([1, 2]))
        );
        assert_eq!(super::parse_example_value("not json"), None);
    }

    #[test]
    fn class_body_separates_documented_fields_with_a_blank_line() {
        let body = class_body(
            None,
            vec![
                field("a: str = pydantic.Field()", Some("Doc A.")),
                field("b: str = pydantic.Field()", Some("Doc B.")),
            ],
        );
        assert_eq!(
            body,
            r#"    a: str = pydantic.Field()
    """
    Doc A.
    """

    b: str = pydantic.Field()
    """
    Doc B.
    """

    if IS_PYDANTIC_V2:
        model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow", frozen=True)  # type: ignore # Pydantic v2
    else:

        class Config:
            frozen = True
            smart_union = True
            extra = pydantic.Extra.allow
"#
        );
    }

    #[test]
    fn class_body_omits_blank_after_undocumented_field() {
        let body = class_body(
            None,
            vec![
                field("a: str", None),
                field("b: typing.Optional[int] = None", None),
            ],
        );
        // Undocumented fields sit on consecutive lines; one blank precedes config.
        assert!(body.starts_with(
            "    a: str\n    b: typing.Optional[int] = None\n\n    if IS_PYDANTIC_V2:"
        ));
    }

    #[test]
    fn class_body_renders_class_docstring_then_a_blank_line() {
        let body = class_body(Some("A model."), vec![field("a: str", None)]);
        assert!(body.starts_with("    \"\"\"\n    A model.\n    \"\"\"\n\n    a: str\n"));
    }

    /// The `extra-fields` config drives both pydantic-version config blocks. Fern
    /// omits the v2 `extra=` kwarg for `ignore` (v2's own default) but always spells
    /// out the v1 `pydantic.Extra.<name>` member — verified against real Fern output
    /// in `tests/fixtures/pydantic-extra-fields`.
    #[test]
    fn class_body_extra_fields_drives_both_config_blocks() {
        let render = |extra| {
            render_class_body(&environment(), None, vec![field("a: str", None)], extra)
                .expect("renders")
        };

        let allow = render(Some(crate::settings::ExtraFields::Allow));
        assert!(
            allow.contains("pydantic.ConfigDict(extra=\"allow\", frozen=True)")
                && allow.contains("extra = pydantic.Extra.allow"),
            "{allow}"
        );

        // `ignore` drops the v2 `extra=` kwarg (v2 defaults to ignore) but keeps
        // the explicit v1 member.
        let ignore = render(Some(crate::settings::ExtraFields::Ignore));
        assert!(
            ignore.contains("pydantic.ConfigDict(frozen=True)")
                && !ignore.contains("extra=\"ignore\"")
                && ignore.contains("extra = pydantic.Extra.ignore"),
            "{ignore}"
        );

        let forbid = render(Some(crate::settings::ExtraFields::Forbid));
        assert!(
            forbid.contains("pydantic.ConfigDict(extra=\"forbid\", frozen=True)")
                && forbid.contains("extra = pydantic.Extra.forbid"),
            "{forbid}"
        );
    }

    #[test]
    fn reference_entry_renders_details_line_and_param_row() {
        let view = ReferenceEntryView {
            client_prefix: "client.endpoints_urls.".to_string(),
            href: "src/fern/endpoints_urls/client.py".to_string(),
            pkg: "fern".to_string(),
            method: "get".to_string(),
            dots: "...",
            return_type: " -> Widget".to_string(),
            description: None,
            example: "client.endpoints_urls.get()".to_string(),
            example_gap: "\n",
            params: vec![ParamRow {
                name: "id".to_string(),
                annot: "str".to_string(),
                suffix: " ".to_string(),
            }],
        };
        let out = render_tmpl("reference_entry.md", &view);
        assert!(out.contains(
            "<details><summary><code>client.endpoints_urls.\
             <a href=\"src/fern/endpoints_urls/client.py\">get</a>(...) -> Widget</code></summary>"
        ));
        // The param value line carries the suffix's trailing space, then the fixed
        // four-space line (driven by an expression, not fragile template whitespace).
        assert!(out.contains("**id:** `str` \n    \n</dd>"));
        // No description block when there is no description.
        assert!(!out.contains("Description"));
    }

    #[test]
    fn root_client_loops_the_tag_properties() {
        let view = RootClientView {
            class_name: "FernApi".to_string(),
            pkg: "fern".to_string(),
            httpx_type: "httpx.Client".to_string(),
            wrapper: "SyncClientWrapper".to_string(),
            ctor_param: "        token: str,\n".to_string(),
            wrapper_arg: "            token=token,".to_string(),
            doc_param: "    token : str\n\n".to_string(),
            example_line: "        token=\"YOUR_TOKEN\",\n".to_string(),
            auth_check: String::new(),
            base_url_doc:
                "    base_url : str\n        The base url to use for requests from the client.\n\n"
                    .to_string(),
            env_doc: String::new(),
            server_variable_doc: String::new(),
            base_url_ctor: "        base_url: str,\n".to_string(),
            server_variable_init: String::new(),
            wrapper_location: "base_url=base_url".to_string(),
            example_base_url: "        base_url=\"https://yourhost.com/path/to/api\",\n"
                .to_string(),
            client_param_doc: String::new(),
            client_param_ctor: String::new(),
            client_param_example: String::new(),
            client_example_imports: String::new(),
            client_param_wrapper: String::new(),
            tr_doc: String::new(),
            tr_ctor: String::new(),
            tr_wrapper: String::new(),
            raw_client_cls: String::new(),
            root_methods: Vec::new(),
            modules: vec![RootModuleView {
                attr: "dispatch_bays".to_string(),
                cls: "DispatchBaysClient".to_string(),
                import: "DispatchBaysClient".to_string(),
                wrap: false,
            }],
            is_async: false,
            async_token_doc: String::new(),
            async_token_ctor: String::new(),
            async_token_wrapper: String::new(),
            default_max_retries: 2,
        };
        let out = render_tmpl("root_client.py", &view);
        assert!(out.starts_with("class FernApi:"));
        // The auth slots land in the ctor and the wrapper call.
        assert!(out.contains("        token: str,\n        headers:"));
        assert!(out.contains("token=token,"));
        // One lazy slot and one cached property per module.
        assert!(
            out.contains("        self._dispatch_bays: typing.Optional[DispatchBaysClient] = None")
        );
        assert!(out.contains(
            "    @property\n    def dispatch_bays(self):\n        if self._dispatch_bays is None:"
        ));
    }

    #[test]
    fn root_sub_client_alias_names_only_the_classes_the_root_defines() {
        // `client_class_name: EcosystemClient` defines `EcosystemClient` and
        // `AsyncEcosystemClient`, so both of the `ecosystem` sub-client's classes
        // are aliased and an unrelated sub-client is not.
        assert_eq!(
            root_sub_client_import("ecosystem", "EcosystemClient", "EcosystemClient"),
            "EcosystemClient as ecosystem_client_EcosystemClient"
        );
        assert_eq!(
            root_sub_client_alias("ecosystem", "AsyncEcosystemClient", "EcosystemClient"),
            Some("ecosystem_client_AsyncEcosystemClient".to_string())
        );
        assert_eq!(
            root_sub_client_import("category", "CategoryClient", "EcosystemClient"),
            "CategoryClient"
        );
        // A root named like a sub-client's async class collides with that class
        // alone: the root's own async class is `AsyncAsyncFooClient`.
        assert_eq!(
            root_sub_client_alias("foo", "AsyncFooClient", "AsyncFooClient"),
            Some("foo_client_AsyncFooClient".to_string())
        );
        assert_eq!(
            root_sub_client_alias("foo", "FooClient", "AsyncFooClient"),
            None
        );
    }

    #[test]
    fn client_class_lays_out_skeleton_and_methods() {
        let view = minijinja::context! {
            class_name => "DispatchBaysClient",
            wrapper => "SyncClientWrapper",
            raw_client_cls => "RawDispatchBaysClient",
            methods => vec!["    def add(self):\n        ...", "    def remove(self):\n        ..."],
        };
        let out = render_tmpl("client_class.py", view);
        assert!(out.starts_with(
            "class DispatchBaysClient:\n    def __init__(self, *, client_wrapper: SyncClientWrapper):"
        ));
        assert!(out.contains("    def with_raw_response(self) -> RawDispatchBaysClient:"));
        // Methods are separated by one blank line, in order.
        assert!(out.contains("        return self._raw_client\n\n    def add(self):"));
        assert!(out.contains("        ...\n\n    def remove(self):"));
    }

    #[test]
    fn field_decl_is_flat_name_annotation_default() {
        let rf = RenderedField {
            py_name: "count".to_string(),
            annotation: Doc::group("typing.Optional[", vec![Doc::atom("int")], "]"),
            default: " = None".to_string(),
            docstring: None,
        };
        assert_eq!(field_decl(&rf), "count: typing.Optional[int] = None");
    }

    #[test]
    fn abbrev_call_keeps_fern_520_placeholders_inline() {
        // No complex body → empty parens.
        assert_eq!(abbrev_call(4, "client.a.b", false), "    client.a.b()");
        // Complex + short → inline `...`.
        assert_eq!(abbrev_call(4, "client.a.b", true), "    client.a.b(...)");
        // Fern keeps even unusually long descriptive operation names flat.
        let long = format!("client.tag.{}", "m".repeat(120));
        assert_eq!(abbrev_call(0, &long, true), format!("{long}(...)"));
        // Raw-response assignments follow the same literal placeholder rule.
        let root_raw =
            "response = client.with_raw_response.get_all_colors_of_the_default_color_name_list";
        assert_eq!(
            abbrev_call(0, root_raw, true),
            "response = client.with_raw_response.get_all_colors_of_the_default_color_name_list(...)"
        );
        let nested_raw =
            "response = client.account_access.with_raw_response.create_account_access_consents";
        assert_eq!(
            abbrev_call(0, nested_raw, true),
            "response = client.account_access.with_raw_response.create_account_access_consents(...)"
        );
        let fitting_prefix =
            "response = client.attachment_public.with_raw_response.create_attachment_public";
        assert_eq!(
            abbrev_call(0, fitting_prefix, true),
            "response = client.attachment_public.with_raw_response.create_attachment_public(...)"
        );
    }

    #[test]
    fn natural_comparison_orders_numeric_runs_and_leading_zero_ties() {
        use std::cmp::Ordering::{Equal, Greater, Less};

        assert_eq!(natural_cmp("item9", "item10"), Less);
        assert_eq!(natural_cmp("item10", "item9"), Greater);
        assert_eq!(natural_cmp("item1", "item01"), Less);
        assert_eq!(natural_cmp("item001", "item01"), Greater);
        assert_eq!(natural_cmp("item0", "item00"), Less);
        assert_eq!(natural_cmp("item12a", "item12b"), Less);
        assert_eq!(natural_cmp("alpha2", "beta1"), Less);
        assert_eq!(natural_cmp("item", "item0"), Less);
        assert_eq!(natural_cmp("same007tail", "same007tail"), Equal);
    }

    #[test]
    fn example_imports_put_union_wrappers_before_their_generated_children() {
        use std::cmp::Ordering::{Greater, Less};

        let wrapper = "MetadataSendTelemetryRequestEventsItem_SessionStart";
        let child = "MetadataSendTelemetryRequestEventsItemSessionStartData";
        assert_eq!(example_import_cmp(wrapper, child), Less);
        assert_eq!(example_import_cmp(child, wrapper), Greater);
        assert_eq!(
            example_import_cmp(
                "ScheduledMessagesScheduleAgentMessageRequestMessagesItemRole",
                "ScheduledMessagesScheduleAgentMessageRequestMessagesItemContentZeroItem_Text"
            ),
            Greater
        );
    }

    #[test]
    fn example_imports_order_names_case_insensitively() {
        use std::cmp::Ordering::{Greater, Less};

        assert_eq!(
            example_import_cmp("RouteGroupsTable", "RouteGroupUser"),
            Less
        );
        assert_eq!(
            example_import_cmp("RouteGroupUser", "RouteGroupsTable"),
            Greater
        );
        assert_eq!(example_import_cmp("FernApi", "fernApi"), Less);
    }

    #[test]
    fn bytes_body_endpoint_is_not_readme_eligible() {
        let mut bytes = endpoint("/bytes", Vec::new(), None);
        bytes.request_body = Some(RequestBody::Bytes {
            content_type: "application/octet-stream".to_string(),
        });
        assert!(!readme_endpoint_eligible(&bytes, &[], &[]));
    }

    #[test]
    fn optional_single_body_conversion_keeps_optional_annotation() {
        let mut ep = endpoint("/approval", Vec::new(), None);
        ep.request_body = Some(RequestBody::Single(SingleBody {
            type_ref: TypeRef::Named("ModifyApprovalRequest".to_string()),
            required: false,
            convert: true,
            content_type: true,
            content_type_override: None,
            example: None,
        }));
        let mut lines = Vec::new();
        let mut imports = Imports::default();
        super::append_request_call_args(&mut lines, &ep, &mut imports);
        let rendered = lines.join("\n");
        assert!(
            rendered.contains("annotation=typing.Optional[ModifyApprovalRequest]"),
            "{rendered}"
        );

        ep.request_body = Some(RequestBody::Inline(vec![BodyField {
            wire_name: "search".to_string(),
            py_name: "search".to_string(),
            type_ref: TypeRef::List(Box::new(TypeRef::Named("SearchItem".to_string()))),
            optional: true,
            nullable: false,
            spec_required: false,
            docstring: None,
            convert: true,
            is_file: false,
            form_json: false,
            form_content_type: None,
            collision_prefix: None,
            inline_object: false,
            example: None,
            media_example: false,
            schema_body_example: false,
            reference_order: 0,
            deprecated: false,
            declaration_order: 0,
            markdown_order: 0,
            admits_only_empty_object: false,
        }]));
        let mut lines = Vec::new();
        super::append_request_call_args(&mut lines, &ep, &mut imports);
        let rendered = lines.join("\n");
        assert!(
            rendered.contains("annotation=typing.Sequence[SearchItem]"),
            "{rendered}"
        );
        assert!(!rendered.contains("annotation=typing.Optional[typing.Sequence"));
    }

    /// An optional body Fern types `Optional[Any]` carries nothing a worked
    /// example could show, and the document version does not change that: letta
    /// declares the shape in `openapi: 3.1` and braintrust's proxy endpoints
    /// declare it in `3.0.3` (corpus row 126), and neither golden passes
    /// `request=`.
    #[test]
    fn optional_unknown_body_is_omitted_from_worked_example() {
        let mut ep = endpoint("/projects/{id}", Vec::new(), None);
        ep.openapi_31 = true;
        ep.request_body = Some(RequestBody::Single(SingleBody {
            type_ref: TypeRef::Primitive(Prim::Any),
            required: false,
            convert: false,
            content_type: true,
            content_type_override: None,
            example: None,
        }));
        let auth = Auth::None;
        let mut ctx = example_ctx(&[], &[], &auth);
        let rendered = build_example(&ep, false, "projects", "fern", "FernApi", &mut ctx)
            .expect("endpoint has an example")
            .join("\n");
        assert!(!rendered.contains("request="), "{rendered}");
        ep.openapi_31 = false;
        let mut ctx = example_ctx(&[], &[], &auth);
        let rendered = build_example(&ep, false, "projects", "fern", "FernApi", &mut ctx)
            .expect("legacy endpoint has an example")
            .join("\n");
        assert!(!rendered.contains("request="), "{rendered}");
        // A *required* unknown body is still shown, which is what keeps the
        // exclusion keyed on optionality rather than on the unknown type.
        if let Some(RequestBody::Single(body)) = ep.request_body.as_mut() {
            body.required = true;
        }
        let mut ctx = example_ctx(&[], &[], &auth);
        let rendered = build_example(&ep, false, "projects", "fern", "FernApi", &mut ctx)
            .expect("required unknown body has an example")
            .join("\n");
        assert!(
            rendered.contains("request={\"key\": \"value\"}"),
            "{rendered}"
        );
    }

    fn api_key_auth(header: &str, required: bool) -> Auth {
        Auth::ApiKey {
            header: header.to_string(),
            required,
            credential: Credential::plain("api_key"),
            prefix: None,
        }
    }

    fn bearer_auth(required: bool) -> Auth {
        Auth::Bearer {
            required,
            credential: Credential::plain("token"),
        }
    }

    fn basic_auth(required: bool) -> Auth {
        Auth::Basic {
            required,
            username: Credential::plain("username"),
            password: Credential::plain("password"),
        }
    }

    #[test]
    fn auth_fragments_cover_every_scheme() {
        let free = std::collections::HashSet::new();
        // api-key required: public `api_key`, unconditional header, no token helper.
        let w = auth_wrapper_parts(&api_key_auth("X-API-Key", true));
        assert_eq!(w.param(&free), "        api_key: str,\n");
        assert_eq!(w.assign, "        self.api_key = api_key\n");
        assert_eq!(
            w.header_block,
            "        headers[\"X-API-Key\"] = self.api_key\n"
        );
        assert!(w.token_method.is_empty());
        // The `super().__init__` arg carries a trailing separator so the following
        // `headers=` kwarg needs no leading comma (which the no-auth arm would omit).
        assert_eq!(w.super_arg(&free), "api_key=api_key, ");

        // No auth: every credential fragment is empty, so the wrapper carries no
        // token and `super().__init__(headers=...)` has no dangling comma.
        let none = auth_wrapper_parts(&Auth::None);
        assert!(none.param(&free).is_empty());
        assert!(none.assign.is_empty());
        assert!(none.header_block.is_empty());
        assert!(none.token_method.is_empty());
        assert!(none.super_arg(&free).is_empty());
        assert!(auth_client_parts(&Auth::None, &free).ctor_param.is_empty());
        assert!(auth_example_args(&Auth::None).is_empty());

        // api-key optional: nullable param and a guarded header write.
        let w = auth_wrapper_parts(&api_key_auth("X-Key", false));
        assert_eq!(
            w.param(&free),
            "        api_key: typing.Optional[str] = None,\n"
        );
        assert_eq!(
            w.header_block,
            "        if self.api_key is not None:\n            headers[\"X-Key\"] = self.api_key\n"
        );

        // bearer required vs optional: the token helper's signature differs.
        let req = auth_wrapper_parts(&bearer_auth(true));
        assert!(req.token_method.contains("-> str:"));
        assert!(req.header_block.contains("f\"Bearer {self._get_token()}\""));
        let opt = auth_wrapper_parts(&bearer_auth(false));
        assert!(opt.token_method.contains("-> typing.Optional[str]:"));
        assert!(opt
            .param(&free)
            .contains("typing.Optional[typing.Union[str"));

        // The root-client fragments and example arg track the same schemes.
        let c = auth_client_parts(&api_key_auth("X-API-Key", true), &free);
        assert_eq!(c.ctor_param, "        api_key: str,\n");
        assert_eq!(c.doc_param, "    api_key : str\n");
        assert_eq!(c.example_line, "        api_key=\"YOUR_API_KEY\",\n");
        assert!(c.check.is_empty());

        // The remaining root-client arms (the docstring drops a ` = None` default).
        let c = auth_client_parts(&api_key_auth("X-Key", false), &free);
        assert_eq!(
            c.ctor_param,
            "        api_key: typing.Optional[str] = None,\n"
        );
        assert_eq!(c.doc_param, "    api_key : typing.Optional[str]\n");
        let c = auth_client_parts(&bearer_auth(true), &free);
        assert_eq!(
            c.ctor_param,
            "        token: typing.Union[str, typing.Callable[[], str]],\n"
        );
        let c = auth_client_parts(&bearer_auth(false), &free);
        assert!(c.ctor_param.contains("typing.Optional[typing.Union[str"));
        assert!(!c.doc_param.contains(" = None"));

        // basic: a required `username`/`password` pair everywhere a single
        // credential would otherwise appear.
        let w = auth_wrapper_parts(&basic_auth(true));
        assert!(w.param(&free).contains("username: typing.Union[str"));
        assert!(w.param(&free).contains("password: typing.Union[str"));
        assert!(w.assign.contains("self._username = username"));
        assert!(w.assign.contains("self._password = password"));
        assert!(w.header_block.contains("httpx.BasicAuth("));
        assert!(w.token_method.contains("def _get_username(self) -> str:"));
        assert!(w.token_method.contains("def _get_password(self) -> str:"));
        assert_eq!(w.super_arg(&free), "username=username, password=password, ");
        let c = auth_client_parts(&basic_auth(true), &free);
        assert_eq!(
            c.ctor_param,
            "        username: typing.Union[str, typing.Callable[[], str]],\n        password: typing.Union[str, typing.Callable[[], str]],\n"
        );
        assert_eq!(
            c.doc_param,
            "    username : typing.Union[str, typing.Callable[[], str]]\n    password : typing.Union[str, typing.Callable[[], str]]\n"
        );
        assert_eq!(
            c.wrapper_arg,
            "            username=username,\n            password=password,"
        );
        assert_eq!(
            c.example_line,
            "        username=\"YOUR_USERNAME\",\n        password=\"YOUR_PASSWORD\",\n"
        );
        let w = auth_wrapper_parts(&basic_auth(false));
        assert!(w
            .header_block
            .contains("if username is not None and password is not None:"));

        assert_eq!(
            auth_example_args(&bearer_auth(true)),
            vec!["token=\"YOUR_TOKEN\""]
        );
        assert_eq!(
            auth_example_args(&api_key_auth("X", false)),
            vec!["api_key=\"YOUR_API_KEY\""]
        );
        assert_eq!(
            auth_example_args(&basic_auth(true)),
            vec!["username=\"YOUR_USERNAME\"", "password=\"YOUR_PASSWORD\""]
        );
    }

    #[test]
    fn named_credentials_carry_their_name_prefix_and_environment() {
        let free = std::collections::HashSet::new();
        let meter = Auth::ApiKey {
            header: "X-Meter-Key".to_string(),
            required: true,
            credential: Credential::plain("meter_token"),
            prefix: Some("Meter".to_string()),
        };
        let w = auth_wrapper_parts(&meter);
        assert_eq!(
            w.header_block,
            "        headers[\"X-Meter-Key\"] = f\"Meter {self.meter_token}\"\n"
        );
        assert_eq!(
            auth_example_args(&meter),
            vec!["meter_token=\"YOUR_METER_TOKEN\""]
        );
        let lift = Auth::Bearer {
            required: true,
            credential: Credential {
                param: "lift_pass".to_string(),
                env: Some("LIFT_PASS".to_string()),
            },
        };
        let c = auth_client_parts(&lift, &free);
        assert_eq!(
            c.ctor_param,
            "        lift_pass: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = None,\n"
        );
        assert_eq!(
            c.doc_param,
            "    lift_pass : typing.Optional[typing.Union[str, typing.Callable[[], str]]]\n"
        );
        // The variable is read when the client is built, then a required
        // credential is checked.
        assert_eq!(
            c.check,
            concat!(
                "        lift_pass = lift_pass if lift_pass is not None else os.getenv(\"LIFT_PASS\")\n",
                "        if lift_pass is None:\n",
                "            raise ApiError(body=\"The client must be instantiated be either passing in lift_pass or setting LIFT_PASS\")\n",
            )
        );
        // An optional one is read the same way but never checked.
        let optional_lift = Auth::Bearer {
            required: false,
            credential: Credential {
                param: "lift_pass".to_string(),
                env: Some("LIFT_PASS".to_string()),
            },
        };
        assert_eq!(
            auth_client_parts(&optional_lift, &free).check,
            "        lift_pass = lift_pass if lift_pass is not None else os.getenv(\"LIFT_PASS\")\n"
        );
        assert!(auth_wrapper_parts(&lift)
            .token_method
            .contains("def _get_lift_pass(self) -> str:"));
        // A credential a promoted header already declares is neither declared,
        // passed nor exampled again, but still documented and assigned.
        let taken: std::collections::HashSet<&str> = ["api_key"].into_iter().collect();
        let shared = api_key_auth("X-Api-Key", true);
        let c = auth_client_parts(&shared, &taken);
        assert!(c.ctor_param.is_empty() && c.wrapper_arg.is_empty() && c.example_line.is_empty());
        assert_eq!(c.doc_param, "    api_key : str\n");
        let w = auth_wrapper_parts(&shared);
        assert!(w.param(&taken).is_empty() && w.super_arg(&taken).is_empty());
        assert_eq!(w.assign, "        self.api_key = api_key\n");
    }

    #[test]
    fn an_offset_pager_defaults_and_steps_its_offset() {
        let mut ep = endpoint(
            "/entries",
            Vec::new(),
            Some(TypeRef::Named("EntryPage".to_string())),
        );
        ep.query_params = vec![QueryParam {
            wire_name: "page".to_string(),
            py_name: "page".to_string(),
            type_ref: TypeRef::Primitive(Prim::Int),
            required: false,
            nullable: false,
            convert: false,
            comma_separated: false,
            allow_multiple: false,
            one_or_many: false,
            example: None,
            example_is_scalar: false,
            aliased_datetime: None,
            docstring: None,
            default: None,
        }];
        ep.pagination = Some(crate::ir::EndpointPagination {
            results: "entries".to_string(),
            advance: crate::ir::PageAdvance::Offset,
            cursor_param: "page".to_string(),
            item_type: TypeRef::Named("Entry".to_string()),
        });
        let out = raw_method(&ep, false, &mut Imports::default());
        assert!(
            out.contains("page = page if page is not None else 1"),
            "{out}"
        );
        assert!(out.contains("_has_next = len(_items or []) > 0"), "{out}");
        assert!(out.contains("page=page + 1,"), "{out}");
        assert!(
            !out.contains("_parsed_next") && !out.contains("_get_next = None"),
            "{out}"
        );
    }

    #[test]
    fn endpoint_extensions_add_an_idempotency_argument_and_disable_retries() {
        let mut ep = endpoint("/parcels", Vec::new(), None);
        ep.http_method = "POST";
        ep.extensions = crate::ir::EndpointExtensions {
            idempotency_headers: vec![HeaderParam {
                wire_name: "X-Dedupe-Token".to_string(),
                py_name: "dedupe_token".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                required: false,
                docstring: None,
                example: None,
                enum_value: false,
            }],
            retries_disabled: true,
            environment_field: None,
        };
        let out = raw_method(&ep, false, &mut Imports::default());
        assert!(
            out.contains("dedupe_token: typing.Optional[str] = None"),
            "{out}"
        );
        assert!(
            out.contains(
                "\"X-Dedupe-Token\": str(dedupe_token) if dedupe_token is not None else None,"
            ),
            "{out}"
        );
        assert!(out.contains(
            "_request_options_with_retries_disabled: typing.Optional[RequestOptions] = ("
        ));
        assert!(out.contains("request_options=_request_options_with_retries_disabled,"));
        let plain = raw_method(
            &endpoint("/parcels", Vec::new(), None),
            false,
            &mut Imports::default(),
        );
        assert!(!plain.contains("retries_disabled") && !plain.contains("dedupe_token"));
    }

    fn endpoint(path: &str, params: Vec<PathParam>, response: Option<TypeRef>) -> Endpoint {
        Endpoint {
            openapi_31: false,
            module: "m".to_string(),
            method_name: "op".to_string(),
            stream_condition: None,
            reference_method_name: None,
            pagination: None,
            http_method: "GET",
            path: path.to_string(),
            path_params: params,
            client_path_params: Vec::new(),
            query_params: Vec::new(),
            header_params: Vec::new(),
            extensions: crate::ir::EndpointExtensions::default(),
            constant_headers: Vec::new(),
            header_order: Vec::new(),
            request_body: None,
            request_body_required: false,
            request_body_has_multipart_related: false,
            reference_body_type: None,
            request_body_doc: None,
            request_body_schema_doc: None,
            body_codegen_named: false,
            body_description_missing: false,
            body_declared_required: false,
            body_component_ref: false,
            body_media_alternatives: false,
            body_collapses_to_type_reference: false,
            body_content_type_override: None,
            body_json_media_type: None,
            body_schema_shape: BodySchemaShape::Other,
            body_schema_dropped: false,
            body_schema_shared: false,
            body_schema_metadata_missing: false,
            body_schema_has_example: false,
            body_schema_example_wrapped: false,
            body_schema_is_beta: false,
            body_schema_example_covers_all_fields: false,
            body_media_has_example: false,
            reference_body_example: None,
            body_schema_documented: false,
            body_schema_titled: false,
            body_schema_is_response_heavy: false,
            body_schema_is_open: false,
            body_schema_implicit_object: false,
            body_composition: crate::ir::BodyComposition::Plain,
            body_response_same_ref: false,
            body_schema_is_success_response: false,
            response,
            response_may_be_empty: false,
            response_names_unknown: false,
            response_doc: Some(String::new()),
            errors: Vec::new(),
            docstring: None,
            reference_description_suffix: String::new(),
            streaming: false,
            stream_chunk: None,
            stream_protocol: None,
            text_response: false,
            binary_response: false,
            importer_example_missing: false,
            binary_schema_response: false,
            wildcard_binary_response: false,
            emittable: true,
        }
    }

    fn ir_with(endpoints: Vec<Endpoint>) -> Ir {
        let endpoint_modules = endpoints
            .iter()
            .filter(|endpoint| !endpoint.module.is_empty())
            .map(|endpoint| endpoint.module.clone())
            .collect::<std::collections::BTreeSet<_>>()
            .into_iter()
            .collect();
        Ir {
            yaml_unquoted_timestamps: None,
            openapi_31: false,
            package_name: "fern".to_string(),
            project_name: "default_package_name".to_string(),
            client_name: "FernApi".to_string(),
            types: Vec::new(),
            tag_types: Vec::new(),
            endpoint_modules,
            empty_endpoint_namespace: false,
            empty_namespace_types: Vec::new(),
            endpoint_module_titles: Default::default(),
            endpoints,
            errors: Vec::new(),
            auth: Auth::None,
            global_headers: Vec::new(),
            client_path_parameters: Vec::new(),
            environment: None,
            extra_fields: crate::settings::ExtraFields::Allow,
            enum_type: crate::settings::EnumType::PythonEnums,
            default_max_retries: crate::settings::DEFAULT_MAX_RETRIES,
            layout: crate::settings::Layout::Packaged,
        }
    }

    #[test]
    fn readme_endpoint_does_not_promote_a_non_post_body_over_the_first_endpoint() {
        // Corpus row 123 declares no POST at all, and Fern anchors its README on
        // the document's first GET rather than on the later PUT that carries a
        // flattened JSON body.
        let mut first_get = endpoint("/organisations", Vec::new(), None);
        first_get.method_name = "get_all_organisations".to_string();
        first_get.module = "organisations".to_string();
        let mut put_with_body = endpoint("/softwarestatements", Vec::new(), None);
        put_with_body.http_method = "PUT";
        put_with_body.method_name = "update_a_software_statement_by_id".to_string();
        put_with_body.module = "software_statements".to_string();
        put_with_body.request_body = Some(RequestBody::Single(SingleBody {
            type_ref: TypeRef::Primitive(Prim::Str),
            required: true,
            convert: false,
            content_type: true,
            content_type_override: None,
            example: None,
        }));

        let ir = ir_with(vec![first_get, put_with_body]);
        assert_eq!(
            readme_endpoint(&ir).map(|e| e.method_name.as_str()),
            Some("get_all_organisations")
        );
    }

    #[test]
    fn readme_endpoint_prefers_the_first_non_binary_post() {
        let mut first_get = endpoint(
            "/one/{id}",
            vec![PathParam {
                wire_name: "id".to_string(),
                py_name: "id".to_string(),
                type_ref: TypeRef::Primitive(Prim::Int),
                docstring: None,
                example: None,
                pattern_constrained: false,
                convert: false,
            }],
            None,
        );
        first_get.method_name = "first_get".to_string();
        let mut no_arg_post = endpoint("/two", Vec::new(), None);
        no_arg_post.http_method = "POST";
        no_arg_post.method_name = "no_arg_post".to_string();

        let ir = ir_with(vec![first_get, no_arg_post]);
        assert_eq!(
            readme_endpoint(&ir).map(|e| e.method_name.as_str()),
            Some("no_arg_post")
        );

        let mut root_get = endpoint("/status", Vec::new(), None);
        root_get.method_name = "status".to_string();
        let mut tagged_post = endpoint("/queues", Vec::new(), None);
        tagged_post.http_method = "POST";
        tagged_post.method_name = "create_queue".to_string();
        tagged_post.module = "queues".to_string();
        let ir = ir_with(vec![root_get, tagged_post]);
        assert_eq!(
            readme_endpoint(&ir).map(|e| e.method_name.as_str()),
            Some("create_queue")
        );

        let mut root_post = endpoint("/token", Vec::new(), None);
        root_post.http_method = "POST";
        root_post.method_name = "get_token".to_string();
        let mut tagged_post = endpoint("/queues", Vec::new(), None);
        tagged_post.http_method = "POST";
        tagged_post.method_name = "create_queue".to_string();
        tagged_post.module = "queues".to_string();
        let ir = ir_with(vec![root_post, tagged_post]);
        assert_eq!(
            readme_endpoint(&ir).map(|e| e.method_name.as_str()),
            Some("get_token")
        );

        let mut no_arg_post = endpoint("/two", Vec::new(), None);
        no_arg_post.http_method = "POST";
        no_arg_post.method_name = "no_arg_post".to_string();
        let mut body = endpoint("/three", Vec::new(), None);
        body.method_name = "body".to_string();
        body.request_body = Some(RequestBody::Single(SingleBody {
            type_ref: TypeRef::Primitive(Prim::Str),
            required: true,
            convert: false,
            content_type: true,
            content_type_override: None,
            example: None,
        }));

        let ir = ir_with(vec![no_arg_post, body]);
        assert_eq!(
            readme_endpoint(&ir).map(|e| e.method_name.as_str()),
            Some("no_arg_post")
        );

        let mut first_module_get = endpoint("/a", Vec::new(), None);
        first_module_get.module = "first".to_string();
        first_module_get.method_name = "first_get".to_string();
        let mut second_module_post = endpoint("/b", Vec::new(), None);
        second_module_post.module = "second".to_string();
        second_module_post.http_method = "POST";
        second_module_post.method_name = "second_post".to_string();
        let mut first_module_post = endpoint("/c", Vec::new(), None);
        first_module_post.module = "first".to_string();
        first_module_post.http_method = "POST";
        first_module_post.method_name = "first_post".to_string();
        let mut ir = ir_with(vec![
            first_module_get,
            second_module_post,
            first_module_post,
        ]);
        ir.endpoint_modules = vec!["first".to_string(), "second".to_string()];
        assert_eq!(
            readme_endpoint(&ir).map(|e| e.method_name.as_str()),
            Some("first_post")
        );
    }

    #[test]
    fn escape_py_str_escapes_quotes_backslashes_and_newlines() {
        // Ordinary spec values are untouched (byte-exact output preserved).
        assert_eq!(
            escape_py_str("https://api.example.com"),
            "https://api.example.com"
        );
        assert_eq!(escape_py_str("X-Tenant"), "X-Tenant");
        // A quote/backslash/newline that would break out of the literal is escaped.
        assert_eq!(escape_py_str("a\"b\\c\nd\re"), "a\\\"b\\\\c\\nd\\re");
    }

    #[test]
    fn reference_normalizes_annotations_without_wrapping_long_names() {
        assert_eq!(
            super::reference_list_annotation("typing.Optional[typing.Sequence[str]]"),
            "typing.Optional[typing.List[str]]"
        );
        let long =
            "typing.Optional[typing.Sequence[PostMyNegotiationsIdCounterRequestOfferItemsItem]]";
        assert_eq!(reference_param_annotation(long), long);
        assert_eq!(
            reference_param_annotation("typing.Optional[typing.Sequence[str]]"),
            "typing.Optional[typing.Sequence[str]]"
        );
        assert_eq!(
            reference_param_annotation(
                "typing.Optional[typing.Union[ResourceToSkip, typing.Sequence[ResourceToSkip]]]"
            ),
            "typing.Optional[typing.Union[ResourceToSkip, typing.Sequence[ResourceToSkip]]]"
        );
        assert_eq!(
            reference_param_annotation(
                "typing.Optional[typing.Union[ResourceToImport, typing.Sequence[ResourceToImport]]]"
            ),
            "typing.Optional[typing.Union[ResourceToImport, typing.Sequence[ResourceToImport]]]"
        );
        assert_eq!(
            reference_param_annotation(
                "typing.Optional[typing.Union[TypeConfigurationIdentifier, typing.Sequence[TypeConfigurationIdentifier]]]"
            ),
            "typing.Optional[typing.Union[TypeConfigurationIdentifier, typing.Sequence[TypeConfigurationIdentifier]]]"
        );
        assert_eq!(
            reference_param_annotation(
                "typing.Optional[typing.Union[StackResourceDriftStatus, typing.Sequence[StackResourceDriftStatus]]]"
            ),
            "typing.Optional[typing.Union[StackResourceDriftStatus, typing.Sequence[StackResourceDriftStatus]]]"
        );
        assert_eq!(
            reference_param_annotation(
                "typing.Optional[typing.Sequence[PostMyNegotiationsIdCounterRequestOfferItemsItemWithEnoughAdditionalContextToExceedTheReferenceWriterWidth]]"
            ),
            "typing.Optional[typing.Sequence[PostMyNegotiationsIdCounterRequestOfferItemsItemWithEnoughAdditionalContextToExceedTheReferenceWriterWidth]]"
        );
        assert_eq!(
            reference_param_annotation(
                "typing.Optional[typing.Union[TypeConfigurationIdentifierWithMoreContext, typing.Sequence[TypeConfigurationIdentifierWithMoreContext]]]"
            ),
            "typing.Optional[typing.Union[TypeConfigurationIdentifierWithMoreContext, typing.Sequence[TypeConfigurationIdentifierWithMoreContext]]]"
        );
        assert_eq!(
            reference_param_annotation(
                "typing.Optional[typing.Union[TypeConfigurationIdentifierWithEnoughAdditionalContext, typing.Sequence[TypeConfigurationIdentifierWithEnoughAdditionalContext]]]"
            ),
            "typing.Optional[typing.Union[TypeConfigurationIdentifierWithEnoughAdditionalContext, typing.Sequence[TypeConfigurationIdentifierWithEnoughAdditionalContext]]]"
        );
        assert_eq!(
            reference_param_annotation("typing.Optional[typing.Optional[dt.date]]"),
            "typing.Optional[datetime.date]"
        );
    }

    #[test]
    fn reference_return_type_spells_datetime_in_full() {
        let endpoint = endpoint("/w", Vec::new(), None);
        // The `<summary>` line is prose, so it uses Fern's full spelling rather
        // than the `dt.` alias the generated Python imports the types under.
        assert_eq!(
            super::reference_return_type(&endpoint, "dt.datetime", "bytes"),
            " -> datetime.datetime"
        );
        assert_eq!(
            super::reference_return_type(&endpoint, "typing.Optional[dt.date]", "bytes"),
            " -> typing.Optional[datetime.date]"
        );
        // A type that merely contains `dt` as part of a name is untouched, and a
        // `None` response still renders no suffix at all.
        assert_eq!(
            super::reference_return_type(&endpoint, "UpdtStatus", "bytes"),
            " -> UpdtStatus"
        );
        assert_eq!(super::reference_return_type(&endpoint, "None", "bytes"), "");
    }

    #[test]
    fn raw_type_str_renders_every_shape() {
        let mut i = Imports::at(
            RefLoc::Client("m".to_string()),
            &std::collections::BTreeMap::new(),
        );
        let str_of = |t: &TypeRef, i: &mut Imports| raw_type_str(t, i);
        assert_eq!(str_of(&TypeRef::Primitive(Prim::Str), &mut i), "str");
        assert_eq!(str_of(&TypeRef::Primitive(Prim::Int), &mut i), "int");
        assert_eq!(str_of(&TypeRef::Primitive(Prim::Float), &mut i), "float");
        assert_eq!(str_of(&TypeRef::Primitive(Prim::Bool), &mut i), "bool");
        assert_eq!(str_of(&TypeRef::Primitive(Prim::Any), &mut i), "typing.Any");
        assert_eq!(
            str_of(&TypeRef::Primitive(Prim::Datetime), &mut i),
            "dt.datetime"
        );
        assert_eq!(str_of(&TypeRef::Primitive(Prim::Date), &mut i), "dt.date");
        assert_eq!(
            str_of(&TypeRef::Named("FooBar".to_string()), &mut i),
            "FooBar"
        );
        assert_eq!(
            str_of(
                &TypeRef::Optional(Box::new(TypeRef::Primitive(Prim::Str))),
                &mut i
            ),
            "typing.Optional[str]"
        );
        assert_eq!(
            str_of(
                &TypeRef::List(Box::new(TypeRef::Primitive(Prim::Int))),
                &mut i
            ),
            "typing.List[int]"
        );
        assert_eq!(
            str_of(
                &TypeRef::Set(Box::new(TypeRef::Primitive(Prim::Str))),
                &mut i
            ),
            "typing.Set[str]"
        );
        assert_eq!(
            str_of(
                &TypeRef::Dict(
                    Box::new(TypeRef::Primitive(Prim::Str)),
                    Box::new(TypeRef::Named("Owner".to_string()))
                ),
                &mut i
            ),
            "typing.Dict[str, Owner]"
        );
        assert_eq!(
            str_of(
                &TypeRef::Union(vec![
                    TypeRef::Primitive(Prim::Str),
                    TypeRef::Primitive(Prim::Int)
                ]),
                &mut i
            ),
            "typing.Union[str, int]"
        );
        assert_eq!(
            str_of(
                &TypeRef::Literal(vec!["a".to_string(), "b".to_string()]),
                &mut i
            ),
            "typing.Literal[\"a\", \"b\"]"
        );
        let rendered = i.render();
        assert!(rendered.contains("from ..types.foo_bar import FooBar"));
        assert!(rendered.contains("import datetime as dt"));
    }

    #[test]
    fn url_arg_is_plain_or_interpolated() {
        let mut imports = Imports::default();
        let plain = endpoint(
            "/urls/MixedCase",
            vec![],
            Some(TypeRef::Primitive(Prim::Str)),
        );
        assert_eq!(url_arg(&plain, &mut imports), "\"urls/MixedCase\"");
        let interp = endpoint(
            "/things/{id}",
            vec![PathParam {
                wire_name: "id".to_string(),
                py_name: "id".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                docstring: None,
                example: None,
                pattern_constrained: false,
                convert: false,
            }],
            Some(TypeRef::Primitive(Prim::Str)),
        );
        assert_eq!(
            url_arg(&interp, &mut imports),
            "f\"things/{encode_path_param(id)}\""
        );
        // An object-typed path parameter is wrapped, and the wrapper's import is
        // registered by the same call — the shape
        // `docs/openapi-surface/probes/parameter-style-simple-path-object.yml`
        // measures.
        let converted = endpoint(
            "/things/{id}",
            vec![PathParam {
                wire_name: "id".to_string(),
                py_name: "id".to_string(),
                type_ref: TypeRef::Named("Thing".to_string()),
                docstring: None,
                example: None,
                pattern_constrained: false,
                convert: true,
            }],
            Some(TypeRef::Primitive(Prim::Str)),
        );
        assert_eq!(
            url_arg(&converted, &mut imports),
            "f\"things/{encode_path_param(convert_and_respect_annotation_metadata(object_=id, annotation=Thing, direction='write'))}\""
        );
        assert!(
            imports
                .render()
                .contains("import convert_and_respect_annotation_metadata"),
            "the wrapper's import is registered by the same call; imports render as:\n{}",
            imports.render()
        );
    }

    #[test]
    fn raw_method_inlines_object_body_fields() {
        let mut i = Imports::default();
        let mut ep = endpoint(
            "/req/inline",
            vec![],
            Some(TypeRef::Named("Resp".to_string())),
        );
        ep.method_name = "inline_body".to_string();
        ep.http_method = "POST";
        ep.request_body = Some(RequestBody::Inline(vec![
            // A required scalar → bare arg, plain json entry.
            BodyField {
                wire_name: "string".to_string(),
                py_name: "string".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                optional: false,
                spec_required: true,
                docstring: None,
                convert: false,
                is_file: false,
                form_json: false,
                form_content_type: None,
                collision_prefix: None,
                inline_object: false,
                example: None,
                media_example: false,
                schema_body_example: false,
                nullable: false,
                reference_order: 0,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            },
            // An optional list → `Optional[Sequence[..]] = OMIT` in request context.
            BodyField {
                wire_name: "list".to_string(),
                py_name: "list_".to_string(),
                type_ref: TypeRef::List(Box::new(TypeRef::Primitive(Prim::Str))),
                optional: true,
                spec_required: false,
                docstring: None,
                convert: false,
                is_file: false,
                form_json: false,
                form_content_type: None,
                collision_prefix: None,
                inline_object: false,
                example: None,
                media_example: false,
                schema_body_example: false,
                nullable: false,
                reference_order: 1,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            },
            // An optional convert field keeps `Optional` in both its signature and
            // serialization annotation.
            BodyField {
                wire_name: "NestedObject".to_string(),
                py_name: "nested_object".to_string(),
                type_ref: TypeRef::Named("Nested".to_string()),
                optional: true,
                spec_required: false,
                docstring: None,
                convert: true,
                is_file: false,
                form_json: false,
                form_content_type: None,
                collision_prefix: None,
                inline_object: false,
                example: None,
                media_example: false,
                schema_body_example: false,
                nullable: false,
                reference_order: 2,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            },
        ]));
        let out = raw_method(&ep, false, &mut i);
        // Hoisted keyword-only args, with the reserved-name munge and request-context
        // `Sequence`, plus the OMIT default on the optional field.
        assert!(out.contains("string: str"), "{out}");
        assert!(
            out.contains("list_: typing.Optional[typing.Sequence[str]] = OMIT"),
            "{out}"
        );
        assert!(
            out.contains("nested_object: typing.Optional[Nested] = OMIT"),
            "{out}"
        );
        // The json dict maps wire names to args, convert-wrapping the object field.
        assert!(out.contains("json={"), "{out}");
        assert!(out.contains("\"string\": string,"), "{out}");
        assert!(out.contains("\"list\": list_,"), "{out}");
        assert!(
            out.contains("\"NestedObject\": convert_and_respect_annotation_metadata("),
            "{out}"
        );
        assert!(out.contains("annotation=Nested"), "{out}");
        // An inlined object body always carries the content-type header.
        assert!(
            out.contains("\"content-type\": \"application/json\","),
            "{out}"
        );
    }

    #[test]
    fn raw_method_sends_constant_headers_without_public_parameters() {
        let mut ep = endpoint(
            "/embeddings/size",
            vec![],
            Some(TypeRef::Primitive(Prim::Float)),
        );
        ep.constant_headers = vec![("storage-unit".to_string(), "GB".to_string())];

        let out = raw_method(&ep, false, &mut Imports::default());

        assert!(!out.contains("storage_unit:"), "{out}");
        assert!(out.contains("\"storage-unit\": \"GB\","), "{out}");

        let ir = ir_with(vec![]);
        let reference = reference_entry(
            &environment(),
            &ir,
            &ep,
            &ep.module,
            &ir.package_name,
            &std::collections::BTreeMap::new(),
        )
        .expect("reference entry renders");
        // The reference documents no argument the method does not take.
        assert!(!reference.contains("storage_unit"), "{reference}");

        ep.request_body = Some(RequestBody::Inline(vec![BodyField {
            wire_name: "1st".to_string(),
            py_name: "f_1st".to_string(),
            type_ref: TypeRef::Primitive(Prim::Str),
            optional: false,
            spec_required: true,
            docstring: None,
            convert: false,
            is_file: false,
            form_json: false,
            form_content_type: None,
            collision_prefix: None,
            inline_object: false,
            example: None,
            media_example: false,
            schema_body_example: false,
            nullable: false,
            reference_order: 0,
            deprecated: false,
            declaration_order: 0,
            markdown_order: 0,
            admits_only_empty_object: false,
        }]));
        let reference = reference_entry(
            &environment(),
            &ir,
            &ep,
            &ep.module,
            &ir.package_name,
            &std::collections::BTreeMap::new(),
        )
        .expect("reference entry renders");
        assert!(reference.contains("**1st:** `str` "), "{reference}");
    }

    #[test]
    fn wrapped_schema_example_keeps_content_type_for_optional_fields() {
        let mut ep = endpoint("/widgets", vec![], Some(TypeRef::Primitive(Prim::Str)));
        ep.http_method = "POST";
        ep.request_body = Some(RequestBody::Inline(vec![
            BodyField {
                wire_name: "name".to_string(),
                py_name: "name".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                optional: true,
                spec_required: false,
                docstring: None,
                convert: false,
                is_file: false,
                form_json: false,
                form_content_type: None,
                collision_prefix: None,
                inline_object: false,
                example: None,
                media_example: false,
                schema_body_example: true,
                nullable: false,
                reference_order: 0,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            },
            BodyField {
                wire_name: "note".to_string(),
                py_name: "note".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                optional: true,
                spec_required: false,
                docstring: None,
                convert: false,
                is_file: false,
                form_json: false,
                form_content_type: None,
                collision_prefix: None,
                inline_object: false,
                example: None,
                media_example: false,
                schema_body_example: true,
                nullable: false,
                reference_order: 1,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            },
        ]));
        ep.body_schema_has_example = true;
        ep.body_schema_example_wrapped = true;
        ep.body_schema_example_covers_all_fields = true;
        ep.body_schema_documented = true;

        let documented = raw_method(&ep, false, &mut Imports::default());
        assert!(
            documented.contains("\"content-type\": \"application/json\","),
            "{documented}"
        );

        ep.body_schema_documented = false;
        let undocumented = raw_method(&ep, false, &mut Imports::default());
        assert!(
            undocumented.contains("\"content-type\": \"application/json\","),
            "{undocumented}"
        );

        if let Some(RequestBody::Inline(fields)) = &mut ep.request_body {
            fields[0].convert = true;
        }
        let nested_undocumented = raw_method(&ep, false, &mut Imports::default());
        assert!(
            !nested_undocumented.contains("\"content-type\": \"application/json\","),
            "{nested_undocumented}"
        );
    }

    #[test]
    fn raw_type_str_request_context_uses_sequence() {
        use super::raw_type_str_ctx;
        let mut i = Imports::default();
        // In request context, both list and set collapse to `typing.Sequence`.
        assert_eq!(
            raw_type_str_ctx(
                &TypeRef::List(Box::new(TypeRef::Primitive(Prim::Str))),
                &mut i,
                true
            ),
            "typing.Sequence[str]"
        );
        assert_eq!(
            raw_type_str_ctx(
                &TypeRef::Set(Box::new(TypeRef::Primitive(Prim::Int))),
                &mut i,
                true
            ),
            "typing.Sequence[int]"
        );
        // Optionals, dicts, and unions recurse in the same context.
        assert_eq!(
            raw_type_str_ctx(
                &TypeRef::Optional(Box::new(TypeRef::Set(Box::new(TypeRef::Primitive(
                    Prim::Str
                ))))),
                &mut i,
                true
            ),
            "typing.Optional[typing.Sequence[str]]"
        );
        assert_eq!(
            raw_type_str_ctx(
                &TypeRef::Union(vec![
                    TypeRef::Primitive(Prim::Str),
                    TypeRef::List(Box::new(TypeRef::Primitive(Prim::Int))),
                ]),
                &mut i,
                true
            ),
            "typing.Union[str, typing.Sequence[int]]"
        );
        // Without request context, list and set keep their concrete forms.
        assert_eq!(
            raw_type_str_ctx(
                &TypeRef::Set(Box::new(TypeRef::Primitive(Prim::Str))),
                &mut i,
                false
            ),
            "typing.Set[str]"
        );
    }

    #[test]
    fn raw_method_streams_a_bytes_body_and_raises_errors() {
        let mut i = Imports::default();
        let mut ep = endpoint(
            "/upload/{id}",
            vec![PathParam {
                wire_name: "id".to_string(),
                py_name: "id".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                docstring: None,
                example: None,
                pattern_constrained: false,
                convert: false,
            }],
            Some(TypeRef::Named("Resp".to_string())),
        );
        ep.method_name = "upload".to_string();
        ep.http_method = "POST";
        ep.request_body = Some(RequestBody::Bytes {
            content_type: "application/octet-stream".to_string(),
        });
        ep.errors = vec![
            ErrorResponse {
                status: crate::ir::ErrorStatus::Exact(400),
                class_name: "BadRequestError".to_string(),
                body_type: TypeRef::Named("BadInfo".to_string()),
            },
            ErrorResponse {
                status: crate::ir::ErrorStatus::ServerRange,
                class_name: "ServerError".to_string(),
                body_type: TypeRef::Named("BadInfo".to_string()),
            },
        ];
        let out = raw_method(&ep, false, &mut i);
        // A bytes body streams via `content=` under the octet-stream content type.
        assert!(
            out.contains(
                "request: typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]]"
            ),
            "{out}"
        );

        assert!(out.contains("content=request,"), "{out}");
        assert!(
            out.contains("\"content-type\": \"application/octet-stream\","),
            "{out}"
        );
        // The declared 400 raises the generated exception, parsing its body.
        assert!(out.contains("if _response.status_code == 400:"), "{out}");
        assert!(out.contains("raise BadRequestError("), "{out}");
        assert!(i
            .render()
            .contains("from ..errors.bad_request_error import BadRequestError"));
        // A `5XX` range raises for any status in it, handing the exception the
        // actual status; the concrete status alone gets no `status_code`.
        assert!(
            out.contains(
                "            if 500 <= _response.status_code < 600:\n                raise ServerError("
            ),
            "{out}"
        );
        assert_eq!(
            out.matches("\n                    status_code=_response.status_code,\n")
                .count(),
            1,
            "{out}"
        );
    }

    #[test]
    fn raw_method_renders_array_query_params_as_allow_multiple() {
        use crate::ir::QueryParam;
        let mut i = Imports::default();
        let mut ep = endpoint("/q", vec![], None);
        ep.query_params = vec![QueryParam {
            wire_name: "tag".to_string(),
            py_name: "tag".to_string(),
            type_ref: TypeRef::List(Box::new(TypeRef::Primitive(Prim::Str))),
            required: true,
            nullable: false,
            default: None,
            convert: false,
            comma_separated: true,
            allow_multiple: true,
            one_or_many: false,
            example: None,
            example_is_scalar: false,
            aliased_datetime: None,
            docstring: None,
        }];
        let out = raw_method(&ep, false, &mut i);
        // An array query param allows multiple values and is always optional.
        assert!(
            out.contains("tag: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None"),
            "{out}"
        );
        assert!(
            out.contains(
                "\"tag\": \",\".join(map(str, tag)) if isinstance(tag, (list, tuple, set)) else tag,"
            ),
            "{out}"
        );
        ep.query_params[0].required = false;
        ep.query_params[0].comma_separated = false;
        ep.query_params[0].allow_multiple = false;
        let out = raw_method(&ep, false, &mut Imports::default());
        assert!(
            out.contains("tag: typing.Optional[typing.Sequence[str]] = None"),
            "{out}"
        );
        assert!(
            !out.contains("typing.Union[str, typing.Sequence[str]]"),
            "{out}"
        );
    }

    #[test]
    fn raw_method_serializes_datetime_query_params() {
        use crate::ir::QueryParam;
        let mut i = Imports::default();
        let mut ep = endpoint("/q", vec![], None);
        ep.query_params = vec![QueryParam {
            wire_name: "start".to_string(),
            py_name: "start".to_string(),
            type_ref: TypeRef::Primitive(Prim::Datetime),
            required: false,
            nullable: false,
            default: None,
            convert: false,
            comma_separated: false,
            allow_multiple: false,
            one_or_many: false,
            example: None,
            example_is_scalar: false,
            aliased_datetime: None,
            docstring: None,
        }];

        let out = raw_method(&ep, false, &mut i);
        assert!(
            out.contains("\"start\": serialize_datetime(start) if start is not None else None,"),
            "{out}"
        );
        assert!(i
            .render()
            .contains("from ..core.datetime_utils import serialize_datetime"));
    }

    #[test]
    fn raw_method_handles_a_none_response_and_a_summary() {
        let mut i = Imports::default();
        let mut ep = endpoint("/x", vec![], None);
        ep.docstring = Some("Does a thing.".to_string());
        let out = raw_method(&ep, false, &mut i);
        // Summary line, a `None` return with no parse, and no dangling parser import.
        assert!(out.contains("Does a thing."), "{out}");
        assert!(out.contains("-> HttpResponse[None]:"), "{out}");
        assert!(
            out.contains("return HttpResponse(response=_response, data=None)"),
            "{out}"
        );
        assert!(!out.contains("parse_obj_as"), "{out}");
    }

    fn example_ctx<'a>(
        types: &'a [TypeDecl],
        tag_decls: &'a [TagTypeDecl],
        auth: &'a Auth,
    ) -> ExampleCtx<'a> {
        ExampleCtx {
            types,
            enum_type: crate::settings::EnumType::default(),
            yaml_unquoted_timestamps: None,
            tag_decls,
            referenced: Default::default(),
            referenced_doc_order: Default::default(),
            referenced_tag: Default::default(),
            referenced_tag_doc_order: Default::default(),
            uses_datetime: false,
            datetime_precedes_tag_import: false,
            auth,
            has_environment: false,
            global_headers: &[],
            client_path_parameters: &[],
            building: Default::default(),
            expanding_aliases: Vec::new(),
            documentation: false,
            reference: false,
            field_examples_ignored: false,
            untyped_arguments: std::collections::BTreeSet::new(),
            sdk_first_party: true,
        }
    }

    fn model_field(name: &str, type_ref: TypeRef, required: bool) -> Field {
        Field {
            wire_name: name.to_string(),
            py_name: name.to_string(),
            type_ref,
            optional: !required,
            nullable: false,
            spec_required: required,
            docstring: None,
            example: None,
            declared_name: None,
            deprecated: false,
            admits_only_empty_object: false,
            xml: None,
            inherited_from_flattened_parent: false,
            min_length: None,
            max_length: None,
        }
    }

    fn object_of(name: &str, fields: Vec<(&str, TypeRef)>) -> TypeDecl {
        TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: name.to_string(),
            module: crate::naming::module_name(name),
            bases: Vec::new(),
            fields: fields
                .into_iter()
                .map(|(field, type_ref)| model_field(field, type_ref, false))
                .collect(),
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        })
    }

    fn named(name: &str) -> TypeRef {
        TypeRef::Named(name.to_string())
    }

    fn sorted_names(repair: &ForwardRepair) -> Vec<&str> {
        let mut names: Vec<&str> = repair.names.iter().map(String::as_str).collect();
        names.sort_unstable();
        names
    }

    #[test]
    fn a_repair_names_only_the_cycles_its_direct_references_close() {
        // `Captain` and `Vessel` close one cycle, which points into the
        // self-recursive `Harbor`; `Roster` sits outside both.
        let types = vec![
            object_of("Roster", vec![("captain", named("Captain"))]),
            object_of(
                "Captain",
                vec![("vessels", TypeRef::List(Box::new(named("Vessel"))))],
            ),
            object_of(
                "Vessel",
                vec![("skipper", named("Captain")), ("homeport", named("Harbor"))],
            ),
            object_of("Harbor", vec![("district", named("Harbor"))]),
        ];
        let map = forward_repair_map(&types, &[]);
        assert_eq!(sorted_names(&map["Roster"]), ["Captain", "Vessel"]);
        assert_eq!(sorted_names(&map["Captain"]), ["Vessel"]);
        assert_eq!(sorted_names(&map["Vessel"]), ["Captain", "Harbor"]);
        assert_eq!(sorted_names(&map["Harbor"]), Vec::<&str>::new());

        let components = cycle_components(&std::collections::HashMap::from([
            ("A".to_string(), vec!["B".to_string()]),
            ("B".to_string(), vec!["A".to_string(), "C".to_string()]),
            (
                "C".to_string(),
                vec!["C".to_string(), "Elsewhere".to_string()],
            ),
        ]));
        assert_eq!(components["A"], components["B"]);
        assert_ne!(components["A"], components["C"]);
        assert!(!components.contains_key("Elsewhere"));
    }

    #[test]
    fn deferred_imports_follow_the_fields_cycle_by_cycle() {
        // Fields reaching two one-member cycles and then a two-member one, in an
        // order no sort reproduces.
        let types = vec![
            object_of(
                "Timetable",
                vec![
                    ("wharf", named("Wharf")),
                    ("buoys", TypeRef::List(Box::new(named("Buoy")))),
                    ("route", named("Route")),
                ],
            ),
            object_of("Wharf", vec![("annex", named("Wharf"))]),
            object_of(
                "Buoy",
                vec![("tethered", TypeRef::List(Box::new(named("Buoy"))))],
            ),
            object_of(
                "Route",
                vec![("stops", TypeRef::List(Box::new(named("Stop"))))],
            ),
            object_of("Stop", vec![("route", named("Route"))]),
        ];
        let map = forward_repair_map(&types, &[]);
        assert_eq!(
            map["Timetable"].deferred_order,
            ["Wharf", "Buoy", "Route", "Stop"]
        );

        // A field reaching a cycle without naming it registers the cycle's
        // non-union members first; the union arrives with the field that names
        // its cycle, so `Knot` precedes `Alpha`.
        let types = vec![
            object_of(
                "Holder",
                vec![("lead", named("Lead")), ("knot", named("Knot"))],
            ),
            object_of("Lead", vec![("knot", named("Knot"))]),
            object_of("Knot", vec![("twist", named("Alpha"))]),
            TypeDecl::Alias(AliasType {
                reach_refs: Vec::new(),
                name: "Alpha".to_string(),
                module: "alpha".to_string(),
                target: TypeRef::Union(vec![named("Knot"), TypeRef::Primitive(Prim::Str)]),
                docstring: None,
                min_length: None,
                max_length: None,
            }),
        ];
        let map = forward_repair_map(&types, &[]);
        assert_eq!(map["Holder"].deferred_order, ["Knot", "Alpha", "Lead"]);
        assert_eq!(sorted_names(&map["Holder"]), ["Alpha", "Knot"]);
    }

    #[test]
    fn examples_import_tag_scoped_discriminated_union_members_from_the_tag() {
        let tag_decls = vec![TagTypeDecl {
            module: "agents".to_string(),
            decl: TypeDecl::DiscriminatedUnion(DiscriminatedUnion {
                base_fields: Vec::new(),
                discriminant_name: None,
                name: "ModifyMessageRequestBody".to_string(),
                module: "modify_message_request_body".to_string(),
                discriminant_property: "message_type".to_string(),
                members: vec![UnionMember {
                    wrapped: false,
                    class_name: "ModifyMessageRequestBody_SystemMessage".to_string(),
                    discriminant: "system_message".to_string(),
                    discriminant_index: None,
                    fields: Vec::new(),
                    source: None,
                    docstring: None,
                }],
                variant_targets: Vec::new(),
                docstring: None,
            }),
        }];
        let auth = Auth::None;
        let mut ctx = example_ctx(&[], &tag_decls, &auth);
        ctx.record_ref("ModifyMessageRequestBody_SystemMessage");
        assert_eq!(
            ctx.referenced_tag_doc_order,
            [(
                "agents".to_string(),
                "ModifyMessageRequestBody_SystemMessage".to_string()
            )]
        );
        assert!(ctx.referenced.is_empty());
    }

    #[test]
    fn datetime_examples_name_their_instant_in_utc() {
        use super::datetime_example as written;
        // Values Fern's reading accepts: `Z`, a positive offset (with or without
        // its colon), no zone at all, a date alone, no seconds, a fraction, and
        // a day past the month's end.
        assert_eq!(written("2023-03-04T05:06:07Z"), "2023-03-04T05:06:07+00:00");
        assert_eq!(
            written("2025-12-02T00:00:00+13:00"),
            "2025-12-01T11:00:00+00:00"
        );
        assert_eq!(
            written("2023-03-04T05:06:07+0200"),
            "2023-03-04T03:06:07+00:00"
        );
        assert_eq!(
            written("2023-03-04 05:06:07+02:00"),
            "2023-03-04T03:06:07+00:00"
        );
        assert_eq!(written("2023-03-04 05:06:07"), "2023-03-04T05:06:07+00:00");
        assert_eq!(written("2023-03-04T05:06:07"), "2023-03-04T05:06:07+00:00");
        assert_eq!(written("2023-03-04t05:06:07Z"), "2023-03-04T05:06:07+00:00");
        assert_eq!(written("2023-03-04"), "2023-03-04T00:00:00+00:00");
        assert_eq!(written("2023-03-04T05:06Z"), "2023-03-04T05:06:00+00:00");
        assert_eq!(
            written("2023-03-04T05:06:07.250Z"),
            "2023-03-04T05:06:07+00:00"
        );
        assert_eq!(
            written("2023-03-04T05:06:07.5+01:00"),
            "2023-03-04T04:06:07+00:00"
        );
        assert_eq!(written("2023-02-30T05:06:07Z"), "2023-03-02T05:06:07+00:00");
        assert_eq!(
            written("2024-12-31T23:30:00+00:00"),
            "2024-12-31T23:30:00+00:00"
        );
        // Values Fern's reading rejects take its replacement: a zone name, a
        // missing separator, an hour, month, day or second out of range, a
        // basic-format value, leading space, and every negative offset or
        // lower-case `z`, which the appended `Z` breaks.
        for invalid in [
            "2023-03-04T05:06:07-05:00",
            "2024-02-28T23:30:00.5-01:00",
            "2023-03-04T05:06:07-00:00",
            "2023-03-04t05:06:07z",
            "2023-12-31 22:00:00-03:00",
            "2022-08-11 14:00:00 CDT",
            "20230304T050607Z",
            "2023-03-04X05:06:07Z",
            "2023-03-04T25:06:07Z",
            "2023-13-04T05:06:07Z",
            "2023-03-32T05:06:07Z",
            "2023-03-04T05:06:60Z",
            "  2023-03-04T05:06:07Z",
            "2023-03-04T05:06-05:00",
            "2023-02-30T05:06:07-05:00",
            "2023-03-04T05:06:07.-05:00",
            "2023-03-04T05:06:07+25:00",
            "2023-03-04T05:06+a\u{e9}1",
            "2023-03-04T05:06:07+0\u{e9}",
            "not a date",
            "2023/03/04T05:06:07Z",
            "2023-03-04T05x06:07Z",
            "",
        ] {
            assert_eq!(written(invalid), super::FERN_DEFAULT_DATETIME, "{invalid}");
        }
    }

    #[test]
    fn example_layout_handles_nested_calls_explicit_lists_and_dicts() {
        let call = Example::Call(
            "Widget".to_string(),
            vec![
                (
                    Some("name".to_string()),
                    Example::Atom("\"sample\"".to_string()),
                ),
                (None, Example::Atom("1".to_string())),
            ],
        );
        assert_eq!(call.flat(), "Widget(name=\"sample\", 1)");
        assert_eq!(
            call.render(4),
            "Widget(\n        name=\"sample\",\n        1,\n    )"
        );

        let nested = Example::List(vec![call]);
        assert_eq!(
            nested.render(0),
            "[\n    Widget(\n        name=\"sample\",\n        1,\n    )\n]"
        );
        let long = Example::ExplicitList(vec![Example::Atom(format!("\"{}\"", "x".repeat(90)))]);
        assert!(long.render(0).starts_with("[\n    \"xxx"));
        // One item, so no trailing comma; two of them carry one.
        assert!(long.render(0).ends_with("\"\n]"));
        let long_pair = Example::ExplicitList(vec![
            Example::Atom(format!("\"{}\"", "x".repeat(90))),
            Example::Atom("\"y\"".to_string()),
        ]);
        assert!(long_pair.render(0).ends_with("\"y\",\n]"));
        let long_inferred = Example::List(vec![Example::Atom(
            "VeryLongGeneratedEnumName::MEMBER".repeat(3),
        )]);
        assert!(long_inferred.render_at(8, 20).starts_with("[\n"));

        let dict = Example::Dict(vec![(
            "widget".to_string(),
            Example::Call(
                "Widget".to_string(),
                vec![(None, Example::Atom("1".to_string()))],
            ),
        )]);
        assert_eq!(
            dict.render(0),
            "{\n    \"widget\": Widget(\n        1,\n    )\n}"
        );
        assert_eq!(Example::Dict(Vec::new()).flat(), "{}");
        assert_eq!(
            Example::ExplicitList(vec![Example::Atom("1".to_string())]).render(0),
            "[1]"
        );
        assert_eq!(
            Example::ReferenceList(vec![
                Example::List(Vec::new()),
                Example::List(Vec::new()),
                Example::List(Vec::new()),
            ])
            .render(0),
            "[\n    [],\n    [],\n    []\n]"
        );
        assert!(
            Example::ReferenceDict(vec![("key".to_string(), Example::Atom("1".to_string()))])
                .forces_multiline()
        );
        assert!(Example::ReferenceList(vec![Example::Atom("1".to_string())]).forces_multiline());
    }

    #[test]
    fn example_context_resolves_literals_aliases_temporals_and_json_values() {
        let types = vec![
            TypeDecl::Alias(AliasType {
                reach_refs: Vec::new(),
                name: "ScalarAlias".to_string(),
                module: "scalar_alias".to_string(),
                target: TypeRef::Primitive(Prim::Str),
                docstring: None,
                min_length: None,
                max_length: None,
            }),
            TypeDecl::Alias(AliasType {
                reach_refs: Vec::new(),
                name: "AnyAlias".to_string(),
                module: "any_alias".to_string(),
                target: TypeRef::Optional(Box::new(TypeRef::Primitive(Prim::Any))),
                docstring: None,
                min_length: None,
                max_length: None,
            }),
            TypeDecl::Enum(EnumType {
                name: "Color".to_string(),
                module: "color".to_string(),
                members: vec![EnumMember {
                    name: "RED".to_string(),
                    visit_param: "red".to_string(),
                    value: "red".to_string(),
                    docstring: None,
                }]
                .into(),
                docstring: None,
            }),
        ];
        let auth = Auth::None;
        let mut ctx = example_ctx(&types, &[], &auth);

        assert_eq!(
            ctx.value_from_example(
                &TypeRef::Optional(Box::new(TypeRef::Primitive(Prim::Datetime))),
                "\"2024-01-15T09:30:00.000Z\"",
            )
            .unwrap()
            .flat(),
            "datetime.datetime.fromisoformat(\"2024-01-15 09:30:00+00:00\")"
        );
        assert_eq!(
            ctx.value_from_example(&TypeRef::Primitive(Prim::Date), "\"2024-02-03\"")
                .unwrap()
                .flat(),
            "datetime.date.fromisoformat(\"2024-02-03\")"
        );
        assert!(ctx.uses_datetime);
        assert_eq!(
            ctx.value_from_example(&TypeRef::Named("ScalarAlias".to_string()), "\"given\"")
                .unwrap()
                .flat(),
            "\"given\""
        );
        assert_eq!(
            ctx.value_from_example(
                &TypeRef::Named("AnyAlias".to_string()),
                r#"{"enabled":true}"#,
            )
            .unwrap()
            .flat(),
            r#"{"enabled": True}"#
        );
        assert_eq!(
            ctx.value_from_example(
                &TypeRef::List(Box::new(TypeRef::Primitive(Prim::Date))),
                r#"["2024-02-03"]"#,
            )
            .unwrap()
            .flat(),
            "[datetime.date.fromisoformat(\"2024-02-03\")]"
        );
        assert_eq!(
            ctx.value_from_example(&TypeRef::Named("Color".to_string()), "\"red\"")
                .unwrap()
                .flat(),
            "Color.RED"
        );
        assert!(ctx
            .value_from_example(&TypeRef::Named("Color".to_string()), "\"blue\"")
            .is_none());
        assert!(ctx
            .value_from_example(&TypeRef::Primitive(Prim::Bool), "not-json")
            .is_some());
        assert!(ctx
            .value_from_example(&TypeRef::Primitive(Prim::Datetime), "not-json")
            .is_none());

        let json = example_from_json_as(
            serde_json::json!({
                "items": ["x", 2, false, null]
            }),
            false,
        );
        assert_eq!(json.flat(), r#"{"items": ["x", 2, False, None]}"#);
        let quoted = serde_json::json!({"body": "{\"orders\":[]}", "note": "it's \"x\""});
        assert_eq!(
            example_from_json_as(quoted.clone(), true).flat(),
            r#"{"body": '{"orders":[]}', "note": "it's \"x\""}"#
        );
        assert_eq!(
            example_from_json_as(quoted, false).flat(),
            r#"{"body": "{\"orders\":[]}", "note": "it's \"x\""}"#
        );
    }

    #[test]
    fn example_context_covers_composite_examples_and_type_matching_edges() {
        let payload = TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: "Payload".to_string(),
            module: "payload".to_string(),
            bases: Vec::new(),
            fields: vec![
                model_field("id", TypeRef::Primitive(Prim::Int), true),
                model_field("label", TypeRef::Primitive(Prim::Str), false),
            ],
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        });
        let types = vec![
            payload,
            TypeDecl::Object(ObjectType {
                reach_refs: Vec::new(),
                name: "Event".to_string(),
                module: "event".to_string(),
                bases: Vec::new(),
                fields: vec![
                    model_field("id", TypeRef::Primitive(Prim::Int), true),
                    model_field("when", TypeRef::Primitive(Prim::Datetime), true),
                ],
                example_fields: Default::default(),
                docstring: None,
                xml: None,
            }),
            TypeDecl::Alias(AliasType {
                reach_refs: Vec::new(),
                name: "PayloadAlias".to_string(),
                module: "payload_alias".to_string(),
                target: TypeRef::Named("Payload".to_string()),
                docstring: None,
                min_length: None,
                max_length: None,
            }),
            TypeDecl::Alias(AliasType {
                reach_refs: Vec::new(),
                name: "CompositeAlias".to_string(),
                module: "composite_alias".to_string(),
                target: TypeRef::Dict(
                    Box::new(TypeRef::Primitive(Prim::Str)),
                    Box::new(TypeRef::Primitive(Prim::Bool)),
                ),
                docstring: None,
                min_length: None,
                max_length: None,
            }),
            TypeDecl::Alias(AliasType {
                reach_refs: Vec::new(),
                name: "BoolAlias".to_string(),
                module: "bool_alias".to_string(),
                target: TypeRef::Primitive(Prim::Bool),
                docstring: None,
                min_length: None,
                max_length: None,
            }),
            TypeDecl::Enum(EnumType {
                name: "Color".to_string(),
                module: "color".to_string(),
                members: vec![EnumMember {
                    name: "RED".to_string(),
                    visit_param: "red".to_string(),
                    value: "red".to_string(),
                    docstring: None,
                }]
                .into(),
                docstring: None,
            }),
            TypeDecl::DiscriminatedUnion(DiscriminatedUnion {
                base_fields: Vec::new(),
                discriminant_name: None,
                name: "Shape".to_string(),
                module: "shape".to_string(),
                discriminant_property: "kind".to_string(),
                members: Vec::new(),
                variant_targets: Vec::new(),
                docstring: None,
            }),
        ];
        let auth = Auth::None;
        let mut ctx = example_ctx(&types, &[], &auth);

        assert!(ctx.example_is_scalar(&TypeRef::Named("BoolAlias".to_string())));
        assert!(!ctx.example_is_scalar(&TypeRef::Named("Payload".to_string())));
        assert!(ctx.example_is_composite(&TypeRef::Named("CompositeAlias".to_string())));
        assert!(ctx.example_is_composite(&TypeRef::Set(Box::new(TypeRef::Primitive(Prim::Str)))));
        assert!(!ctx.example_is_composite(&TypeRef::Primitive(Prim::Int)));
        assert!(
            ctx.example_is_object(&TypeRef::Optional(Box::new(TypeRef::Named(
                "PayloadAlias".to_string()
            ))))
        );
        assert!(!ctx.example_is_object(&TypeRef::Named("Color".to_string())));
        assert!(!ctx.example_is_object(&TypeRef::Primitive(Prim::Any)));
        assert!(
            ctx.example_is_temporal(&TypeRef::Optional(Box::new(TypeRef::Primitive(Prim::Date))))
        );
        assert!(!ctx.example_is_temporal(&TypeRef::Primitive(Prim::Str)));

        assert_eq!(
            ctx.value_from_example(&TypeRef::Primitive(Prim::Float), "2")
                .expect("integer-shaped float is accepted")
                .flat(),
            "2.0"
        );
        assert_eq!(
            ctx.value_from_example(&TypeRef::Primitive(Prim::Float), "2.5")
                .expect("decimal float is accepted")
                .flat(),
            "2.5"
        );
        for (literal, expected) in [("true", "True"), ("false", "False"), ("other", "other")] {
            assert_eq!(
                ctx.value_from_example(&TypeRef::Primitive(Prim::Bool), literal)
                    .expect("boolean example branch returns an atom")
                    .flat(),
                expected
            );
        }
        assert_eq!(
            ctx.value_from_example(
                &TypeRef::Set(Box::new(TypeRef::Primitive(Prim::Int))),
                "[1,null]",
            )
            .expect("set example is accepted")
            .flat(),
            "[1, 1]"
        );
        assert!(ctx
            .value_from_example(
                &TypeRef::List(Box::new(TypeRef::Primitive(Prim::Int))),
                "not-json",
            )
            .is_none());
        assert_eq!(
            ctx.value_from_example(
                &TypeRef::Union(vec![
                    TypeRef::Primitive(Prim::Int),
                    TypeRef::Primitive(Prim::Str),
                ]),
                "\"chosen\"",
            )
            .expect("the matching union variant is selected")
            .flat(),
            "\"chosen\""
        );
        assert!(ctx
            .value_from_example(
                &TypeRef::Union(vec![
                    TypeRef::List(Box::new(TypeRef::Primitive(Prim::Str))),
                    TypeRef::Dict(
                        Box::new(TypeRef::Primitive(Prim::Str)),
                        Box::new(TypeRef::Primitive(Prim::Str)),
                    ),
                ]),
                "false",
            )
            .is_none());
        assert_eq!(
            ctx.value_from_example(
                &TypeRef::Named("CompositeAlias".to_string()),
                r#"{"enabled":true}"#,
            )
            .expect("composite alias accepts JSON")
            .flat(),
            r#"{"enabled": True}"#
        );
        assert_eq!(
            ctx.value_from_example(
                &TypeRef::Named("PayloadAlias".to_string()),
                r#"{"id":7,"label":"sample"}"#,
            )
            .expect("object alias constructs the target")
            .flat(),
            "Payload(id=7, label=\"sample\")"
        );
        assert_eq!(
            ctx.value_from_example(&TypeRef::Named("Payload".to_string()), r#"{"id":7}"#)
                .expect("missing optional fields are omitted")
                .flat(),
            "Payload(id=7)"
        );
        assert!(ctx
            .value_from_example(&TypeRef::Named("Shape".to_string()), "{}")
            .is_none());
        assert_eq!(
            ctx.value_from_example(
                &TypeRef::Named("Event".to_string()),
                r#"{"when":"2000-01-23T04:56:07Z"}"#,
            )
            .expect("sentinel temporal example uses generated placeholders")
            .flat(),
            "Event(id=1, when=datetime.datetime.fromisoformat(\"2024-01-15 09:30:00+00:00\"))"
        );
        assert_eq!(
            ctx.value_from_example(
                &TypeRef::Union(vec![
                    TypeRef::Primitive(Prim::Str),
                    TypeRef::List(Box::new(TypeRef::Primitive(Prim::Int))),
                ]),
                r##"{"$ref":"#/components/schemas/Items"}"##,
            )
            .expect("reference-shaped union examples select their collection")
            .flat(),
            "[1]"
        );

        ctx.reference = true;
        assert_eq!(
            ctx.value_from_example(
                &TypeRef::Dict(
                    Box::new(TypeRef::Primitive(Prim::Str)),
                    Box::new(TypeRef::Primitive(Prim::Any)),
                ),
                r#"{"enabled":true,"count":2}"#,
            )
            .expect("reference dictionaries retain keyed layout")
            .render(0),
            "{\n    \"enabled\": True,\n    \"count\": 2\n}"
        );
        assert_eq!(
            ctx.value_from_example(&TypeRef::Primitive(Prim::Str), "'quoted'")
                .expect("reference strings normalize quote style")
                .flat(),
            "\"quoted\""
        );
        ctx.reference = false;
        ctx.documentation = true;
        assert_eq!(
            ctx.value_from_example(&TypeRef::Primitive(Prim::Str), r#""can't""#)
                .expect("documentation strings escape apostrophes")
                .flat(),
            r#""can\'t""#
        );
        assert_eq!(
            ctx.value_from_example(
                &TypeRef::List(Box::new(TypeRef::Primitive(Prim::Date))),
                r#"["2024-02-03","2024-02-03"]"#,
            )
            .expect("documentation temporal lists preserve duplicate values")
            .flat(),
            "[datetime.date.fromisoformat(\"2024-02-03\"), datetime.date.fromisoformat(\"2024-02-03\")]"
        );

        let payload_type = TypeRef::Named("Payload".to_string());
        assert!(ctx.example_matches_type(&payload_type, &serde_json::json!({ "id": 1 })));
        assert!(!ctx.example_matches_type(&payload_type, &serde_json::json!({})));
        assert!(!ctx.example_matches_type(
            &payload_type,
            &serde_json::json!({ "id": 1, "extra": true }),
        ));
        assert!(!ctx.example_matches_type(&payload_type, &serde_json::json!(1)));
        assert!(ctx.example_matches_type(
            &TypeRef::Named("PayloadAlias".to_string()),
            &serde_json::json!({ "id": 1 }),
        ));
        // An enum matches only one of its own values.
        let color = TypeRef::Named("Color".to_string());
        assert!(ctx.example_matches_type(&color, &serde_json::json!("red")));
        assert!(!ctx.example_matches_type(&color, &serde_json::json!("2030-09-01")));
        assert!(!ctx.example_matches_type(&color, &serde_json::json!(false)));
        assert!(ctx.example_matches_type(
            &TypeRef::Optional(Box::new(TypeRef::Primitive(Prim::Int))),
            &serde_json::Value::Null,
        ));
        assert!(ctx.example_matches_type(
            &TypeRef::Union(vec![
                TypeRef::Primitive(Prim::Bool),
                TypeRef::Primitive(Prim::Str),
            ]),
            &serde_json::json!("value"),
        ));

        let cases = [
            (
                TypeRef::List(Box::new(TypeRef::Primitive(Prim::Str))),
                serde_json::json!([]),
            ),
            (
                TypeRef::Set(Box::new(TypeRef::Primitive(Prim::Str))),
                serde_json::json!([]),
            ),
            (
                TypeRef::Dict(
                    Box::new(TypeRef::Primitive(Prim::Str)),
                    Box::new(TypeRef::Primitive(Prim::Str)),
                ),
                serde_json::json!({}),
            ),
            (TypeRef::Primitive(Prim::Str), serde_json::json!("x")),
            (TypeRef::Primitive(Prim::Datetime), serde_json::json!("x")),
            (TypeRef::Primitive(Prim::Date), serde_json::json!("x")),
            (
                TypeRef::Literal(vec!["x".to_string()]),
                serde_json::json!("x"),
            ),
            (TypeRef::Primitive(Prim::Int), serde_json::json!(1)),
            (TypeRef::Primitive(Prim::Long), serde_json::json!(1)),
            (TypeRef::Primitive(Prim::Float), serde_json::json!(1.5)),
            (TypeRef::Primitive(Prim::Bool), serde_json::json!(true)),
            (TypeRef::Primitive(Prim::Any), serde_json::Value::Null),
        ];
        for (type_ref, value) in cases {
            assert!(ctx.example_matches_type(&type_ref, &value));
        }

        let matrix = Example::ExplicitList(vec![
            Example::List(vec![Example::Atom("1".to_string())]),
            Example::ExplicitList(vec![Example::Atom("2".to_string())]),
            Example::List(vec![Example::Atom("3".to_string())]),
        ]);
        assert_eq!(matrix.render(0), "[\n    [1],\n    [2],\n    [3],\n]");
        let single_call = Example::ExplicitList(vec![Example::Call(
            "Payload".to_string(),
            vec![(Some("id".to_string()), Example::Atom("1".to_string()))],
        )]);
        assert_eq!(
            single_call.render(0),
            "[\n    Payload(\n        id=1,\n    )\n]"
        );
        assert_eq!(
            Example::Call("Empty".to_string(), Vec::new()).render(0),
            "Empty()"
        );
        assert_eq!(
            Example::List(vec![Example::Atom("1".to_string())]).render(0),
            "[1]"
        );
    }

    /// The object-typed path parameter's own rendering rules, over each field kind
    /// the Round 6 probe variants measured (`docs/fern-limitations.md`). The e2e
    /// journeys drive the binary over the whole shape; this pins the rule table
    /// itself, kind by kind, so a kind that silently changed answer fails here.
    #[test]
    fn a_path_object_renders_measured_field_kinds_and_refuses_the_rest() {
        let inner = TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: "Inner".to_string(),
            module: "inner".to_string(),
            bases: Vec::new(),
            fields: vec![model_field("label", TypeRef::Primitive(Prim::Str), true)],
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        });
        let kind = TypeDecl::Enum(EnumType {
            name: "Kind".to_string(),
            module: "kind".to_string(),
            members: vec![EnumMember {
                name: "ALPHA".to_string(),
                value: "alpha".to_string(),
                visit_param: "alpha".to_string(),
                docstring: None,
            }]
            .into(),
            docstring: None,
        });
        let alias = TypeDecl::Alias(AliasType {
            reach_refs: Vec::new(),
            name: "Label".to_string(),
            module: "label".to_string(),
            target: TypeRef::Primitive(Prim::Str),
            docstring: None,
            min_length: None,
            max_length: None,
        });
        let base = TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: "Base".to_string(),
            module: "base".to_string(),
            bases: Vec::new(),
            fields: vec![model_field("role", TypeRef::Primitive(Prim::Str), true)],
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        });
        let probe = TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: "ProbeParam".to_string(),
            module: "probe_param".to_string(),
            bases: vec!["Base".to_string()],
            fields: vec![
                model_field("count", TypeRef::Primitive(Prim::Int), true),
                model_field(
                    "tags",
                    TypeRef::List(Box::new(TypeRef::Primitive(Prim::Str))),
                    true,
                ),
                model_field(
                    "meta",
                    TypeRef::Dict(
                        Box::new(TypeRef::Primitive(Prim::Str)),
                        Box::new(TypeRef::Primitive(Prim::Str)),
                    ),
                    true,
                ),
                model_field("kind", TypeRef::Named("Kind".to_string()), true),
                model_field("nickname", TypeRef::Named("Label".to_string()), true),
                // Optional fields are not passed at all, as Fern's own example omits them.
                model_field("note", TypeRef::Primitive(Prim::Str), false),
            ],
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        });
        let types = vec![inner, kind, alias, base, probe];
        let auth = Auth::None;
        let mut ctx = example_ctx(&types, &[], &auth);
        let rendered = ctx
            .path_object_value(&TypeRef::Optional(Box::new(TypeRef::Named(
                "ProbeParam".to_string(),
            ))))
            .expect("every required field is a measured kind");
        assert_eq!(
            "ProbeParam(role=\"string\", count=1, tags=[], meta={}, kind=Kind.ALPHA, nickname=\"string\")",
            rendered.flat(),
            "inherited field first, values read off each field's type, collections empty"
        );

        // A required field that is itself a generated model is the one kind Fern
        // was measured dropping the whole example for.
        let nested = TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: "Nested".to_string(),
            module: "nested".to_string(),
            bases: Vec::new(),
            fields: vec![model_field(
                "nested",
                TypeRef::Named("Inner".to_string()),
                true,
            )],
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        });
        let mut with_nested = types.clone();
        with_nested.push(nested);
        let mut ctx = example_ctx(&with_nested, &[], &auth);
        assert!(ctx
            .path_object_value(&TypeRef::Named("Nested".to_string()))
            .is_none());
        // …and so is a kind no variant measured — a union, here.
        assert!(path_field_render(
            &TypeRef::Union(vec![
                TypeRef::Primitive(Prim::Str),
                TypeRef::Primitive(Prim::Int)
            ]),
            &with_nested,
            &[]
        )
        .is_none());
        // A parameter that is not object-typed at all is not this rule's business.
        assert!(path_object_decl(&TypeRef::Primitive(Prim::Str), &with_nested, &[]).is_none());
        assert!(path_object_decl(&TypeRef::Named("Kind".to_string()), &with_nested, &[]).is_none());
    }

    /// `path_object_documented` is what the README's endpoint selection and the
    /// `reference.md` snippet gap read, so it answers for the endpoint rather than
    /// for one type — and a scalar path parameter is always documented.
    #[test]
    fn an_endpoint_is_documented_unless_a_path_object_field_is_unrenderable() {
        let inner = TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: "Inner".to_string(),
            module: "inner".to_string(),
            bases: Vec::new(),
            fields: vec![model_field("label", TypeRef::Primitive(Prim::Str), true)],
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        });
        let renderable = TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: "Renderable".to_string(),
            module: "renderable".to_string(),
            bases: Vec::new(),
            fields: vec![model_field("role", TypeRef::Primitive(Prim::Str), true)],
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        });
        let unrenderable = TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: "Unrenderable".to_string(),
            module: "unrenderable".to_string(),
            bases: Vec::new(),
            fields: vec![model_field(
                "nested",
                TypeRef::Named("Inner".to_string()),
                true,
            )],
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        });
        let types = vec![inner, renderable, unrenderable];
        let path_param = |type_ref: TypeRef| PathParam {
            wire_name: "id".to_string(),
            py_name: "id".to_string(),
            type_ref,
            docstring: None,
            example: None,
            pattern_constrained: false,
            convert: false,
        };
        let scalar = endpoint(
            "/things/{id}",
            vec![path_param(TypeRef::Primitive(Prim::Str))],
            Some(TypeRef::Primitive(Prim::Str)),
        );
        assert!(path_object_documented(&scalar, &types, &[]));
        let ok = endpoint(
            "/things/{id}",
            vec![path_param(TypeRef::Named("Renderable".to_string()))],
            Some(TypeRef::Primitive(Prim::Str)),
        );
        assert!(path_object_documented(&ok, &types, &[]));
        let dropped = endpoint(
            "/things/{id}",
            vec![path_param(TypeRef::Named("Unrenderable".to_string()))],
            Some(TypeRef::Primitive(Prim::Str)),
        );
        assert!(!path_object_documented(&dropped, &types, &[]));
    }

    /// The docstring writer renders the model only to learn whether Fern documents
    /// the endpoint, and must not import it for an example that never names it.
    #[test]
    fn rendering_without_recording_leaves_the_import_set_untouched() {
        let probe = TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: "ProbeParam".to_string(),
            module: "probe_param".to_string(),
            bases: Vec::new(),
            fields: vec![model_field(
                "when",
                TypeRef::Primitive(Prim::Datetime),
                true,
            )],
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        });
        let types = vec![probe];
        let auth = Auth::None;
        let mut ctx = example_ctx(&types, &[], &auth);
        let recorded = ctx.path_object_value(&TypeRef::Named("ProbeParam".to_string()));
        assert!(recorded.is_some());
        assert_eq!(vec!["ProbeParam".to_string()], ctx.referenced_doc_order);
        assert!(ctx.uses_datetime, "the datetime value registers its import");

        let mut ctx = example_ctx(&types, &[], &auth);
        let discarded = ctx.without_recording(|ctx| {
            ctx.path_object_value(&TypeRef::Named("ProbeParam".to_string()))
        });
        assert!(discarded.is_some(), "the value is still rendered");
        assert!(
            ctx.referenced_doc_order.is_empty(),
            "and its imports rolled back"
        );
        assert!(ctx.referenced.is_empty());
        assert!(!ctx.uses_datetime);
    }

    #[test]
    fn example_context_constructs_inherited_recursive_and_union_models() {
        let mut base_id = model_field("id", TypeRef::Primitive(Prim::Int), true);
        base_id.example = Some("7".to_string());
        let base = TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: "Base".to_string(),
            module: "base".to_string(),
            bases: Vec::new(),
            fields: vec![base_id],
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        });
        let mut metadata = model_field("metadata", TypeRef::Primitive(Prim::Any), true);
        metadata.example = Some(r#"{"active":true}"#.to_string());
        let mut invalid = model_field("invalid", TypeRef::Primitive(Prim::Any), true);
        invalid.example = Some("{invalid".to_string());
        let recursive = model_field("parent", TypeRef::Named("Child".to_string()), true);
        let children = model_field(
            "children",
            TypeRef::List(Box::new(TypeRef::Named("Child".to_string()))),
            true,
        );
        let child = TypeDecl::Object(ObjectType {
            reach_refs: Vec::new(),
            name: "Child".to_string(),
            module: "child".to_string(),
            bases: vec!["Base".to_string()],
            fields: vec![metadata, invalid, recursive, children],
            example_fields: Default::default(),
            docstring: None,
            xml: None,
        });
        let empty_enum = TypeDecl::Enum(EnumType {
            name: "EmptyEnum".to_string(),
            module: "empty_enum".to_string(),
            members: Vec::new().into(),
            docstring: None,
        });
        let mut nullable_required = model_field("server_url", TypeRef::Primitive(Prim::Str), true);
        nullable_required.optional = true;
        nullable_required.nullable = true;
        let union = TypeDecl::DiscriminatedUnion(DiscriminatedUnion {
            base_fields: Vec::new(),
            discriminant_name: None,
            name: "Shape".to_string(),
            module: "shape".to_string(),
            discriminant_property: "kind".to_string(),
            members: vec![UnionMember {
                wrapped: false,
                class_name: "Shape_Circle".to_string(),
                discriminant: "circle".to_string(),
                discriminant_index: None,
                fields: vec![
                    model_field("radius", TypeRef::Primitive(Prim::Float), true),
                    nullable_required,
                ],
                source: None,
                docstring: None,
            }],
            variant_targets: Vec::new(),
            docstring: None,
        });
        let empty_union = TypeDecl::DiscriminatedUnion(DiscriminatedUnion {
            base_fields: Vec::new(),
            discriminant_name: None,
            name: "Nothing".to_string(),
            module: "nothing".to_string(),
            discriminant_property: "kind".to_string(),
            members: Vec::new(),
            variant_targets: Vec::new(),
            docstring: None,
        });
        let types = vec![base, child, empty_enum, union, empty_union];
        let auth = Auth::None;
        let mut ctx = example_ctx(&types, &[], &auth);

        let rendered = ctx
            .value(&TypeRef::Named("Child".to_string()), Slot::Plain)
            .render(0);
        assert!(rendered.contains("id=7"), "{rendered}");
        assert!(
            rendered.contains("metadata={\"active\": True}"),
            "{rendered}"
        );
        assert!(
            rendered.contains("invalid={\"key\": \"value\"}"),
            "{rendered}"
        );
        assert!(rendered.contains("parent=None"), "{rendered}");
        assert!(rendered.contains("children=[]"), "{rendered}");
        assert_eq!(
            ctx.value(&TypeRef::Named("Shape".to_string()), Slot::Plain)
                .flat(),
            "Shape_Circle(radius=1.1)"
        );
        assert_eq!(
            ctx.value(&TypeRef::Named("EmptyEnum".to_string()), Slot::Plain)
                .flat(),
            "None"
        );
        assert_eq!(
            ctx.value(&TypeRef::Named("Nothing".to_string()), Slot::Plain)
                .flat(),
            r#"{"key": "value"}"#
        );
        assert_eq!(
            ctx.value(&TypeRef::Named("Missing".to_string()), Slot::Plain)
                .flat(),
            r#"{"key": "value"}"#
        );
        assert_eq!(
            ctx.union_value(&[], Slot::Plain).flat(),
            r#"{"key": "value"}"#
        );
        assert_eq!(
            ctx.value(&TypeRef::Literal(Vec::new()), Slot::Plain).flat(),
            "\"\""
        );
        assert!(ctx.referenced.contains("Child"));
        assert!(ctx.referenced.contains("Shape_Circle"));
    }

    #[test]
    fn worked_example_orders_wildcard_parameters_and_renders_body_examples() {
        let mut ep = endpoint(
            "/widgets/{id}",
            vec![PathParam {
                wire_name: "id".to_string(),
                py_name: "id".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                docstring: Some("Widget identifier.".to_string()),
                example: None,
                pattern_constrained: false,
                convert: false,
            }],
            Some(TypeRef::Primitive(Prim::Str)),
        );
        ep.http_method = "POST";
        ep.query_params = vec![
            QueryParam {
                wire_name: "tags".to_string(),
                py_name: "tags".to_string(),
                type_ref: TypeRef::List(Box::new(TypeRef::Primitive(Prim::Str))),
                required: true,
                nullable: false,
                default: None,
                convert: false,
                comma_separated: false,
                allow_multiple: false,
                one_or_many: false,
                example: None,
                example_is_scalar: false,
                aliased_datetime: None,
                docstring: Some("Tags to include.".to_string()),
            },
            QueryParam {
                wire_name: "limit".to_string(),
                py_name: "limit".to_string(),
                type_ref: TypeRef::Primitive(Prim::Int),
                required: false,
                nullable: false,
                default: None,
                convert: false,
                comma_separated: false,
                allow_multiple: false,
                one_or_many: false,
                example: Some("3".to_string()),
                example_is_scalar: true,
                aliased_datetime: None,
                docstring: Some("Maximum results.".to_string()),
            },
        ];
        ep.header_params = vec![HeaderParam {
            wire_name: "X-Trace".to_string(),
            py_name: "trace".to_string(),
            type_ref: TypeRef::Primitive(Prim::Str),
            required: false,
            docstring: Some("Trace token.".to_string()),
            example: Some("\"trace-1\"".to_string()),
            enum_value: false,
        }];
        ep.request_body = Some(RequestBody::Single(SingleBody {
            type_ref: TypeRef::Primitive(Prim::Str),
            required: true,
            convert: false,
            content_type: true,
            content_type_override: Some("*/*".to_string()),
            example: Some("\"payload\"".to_string()),
        }));
        ep.response_doc = Some("Created widget.".to_string());

        let auth = Auth::Bearer {
            required: true,
            credential: Credential::plain("token"),
        };
        let global_headers = [GlobalHeader {
            wire_name: "X-Tenant".to_string(),
            py_name: "tenant".to_string(),
            presence: crate::ir::HeaderPresence::Required(HeaderType::Str),
            credential: None,
        }];
        let mut ctx = example_ctx(&[], &[], &auth);
        ctx.global_headers = &global_headers;
        let rendered = build_example(&ep, false, "widgets", "acme", "AcmeApi", &mut ctx)
            .expect("wildcard request has a worked example")
            .join("\n");
        let trace = rendered.find("trace=\"trace-1\"").unwrap();
        let tags = rendered.find("tags=[\"tags\"]").unwrap();
        assert!(trace < tags, "{rendered}");
        assert!(rendered.contains("limit=3"), "{rendered}");
        assert!(rendered.contains("request=\"payload\""), "{rendered}");
        assert!(rendered.contains("tenant=\"YOUR_TENANT\""), "{rendered}");
        assert!(rendered.contains("token=\"YOUR_TOKEN\""), "{rendered}");

        let mut reference_ctx = example_ctx(&[], &[], &auth);
        reference_ctx.global_headers = &global_headers;
        let reference = build_documentation_example(
            &ep,
            false,
            "widgets",
            "acme",
            "AcmeApi",
            &mut reference_ctx,
            None,
            true,
        )
        .expect("wildcard request has a reference example")
        .join("\n");
        assert!(reference.contains("tags=["), "{reference}");
        assert!(reference.contains("\"tags\""), "{reference}");
        assert!(reference.contains("trace=\"trace-1\""), "{reference}");
        assert!(reference.contains("request=\"payload\""), "{reference}");

        ep.request_body = Some(RequestBody::Inline(vec![
            BodyField {
                wire_name: "when".to_string(),
                py_name: "when".to_string(),
                type_ref: TypeRef::Primitive(Prim::Datetime),
                optional: false,
                nullable: false,
                spec_required: true,
                docstring: None,
                convert: false,
                is_file: false,
                form_json: false,
                form_content_type: None,
                collision_prefix: None,
                inline_object: false,
                example: Some("\"2024-01-15T09:30:00Z\"".to_string()),
                media_example: false,
                schema_body_example: false,
                reference_order: 0,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            },
            BodyField {
                wire_name: "metadata".to_string(),
                py_name: "metadata".to_string(),
                type_ref: TypeRef::Optional(Box::new(TypeRef::Primitive(Prim::Any))),
                optional: true,
                nullable: true,
                spec_required: true,
                docstring: None,
                convert: false,
                is_file: false,
                form_json: false,
                form_content_type: None,
                collision_prefix: None,
                inline_object: false,
                example: Some(r#"{"source":"test"}"#.to_string()),
                media_example: true,
                schema_body_example: false,
                reference_order: 1,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            },
        ]));
        let mut ctx = example_ctx(&[], &[], &Auth::None);
        let rendered = build_example(&ep, true, "widgets", "acme", "AcmeApi", &mut ctx)
            .expect("inline request has an async example")
            .join("\n");
        assert!(rendered.contains("import asyncio"), "{rendered}");
        assert!(rendered.contains("import datetime"), "{rendered}");
        assert!(
            rendered.contains("datetime.datetime.fromisoformat"),
            "{rendered}"
        );
        assert!(
            rendered.contains("metadata={\"source\": \"test\"}"),
            "{rendered}"
        );
        assert!(rendered.contains("await client.widgets.op("), "{rendered}");
    }

    #[test]
    fn a_list_path_parameter_beside_a_request_body_has_no_worked_example() {
        // DaniWeb's `POST /conversations/{ID}/schedules` takes an array path
        // parameter and a body, and Fern emits no example for it while both
        // halves alone keep one.
        let with_body = |type_ref: TypeRef| {
            let mut ep = endpoint(
                "/conversations/{id}/schedules",
                vec![PathParam {
                    wire_name: "id".to_string(),
                    py_name: "id".to_string(),
                    type_ref,
                    docstring: None,
                    example: None,
                    pattern_constrained: false,
                    convert: false,
                }],
                Some(TypeRef::Primitive(Prim::Str)),
            );
            ep.http_method = "POST";
            ep.request_body = Some(RequestBody::Inline(vec![BodyField {
                wire_name: "date".to_string(),
                py_name: "date".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                optional: false,
                nullable: false,
                spec_required: true,
                docstring: None,
                convert: false,
                is_file: false,
                form_json: false,
                form_content_type: None,
                collision_prefix: None,
                inline_object: false,
                example: None,
                media_example: false,
                schema_body_example: false,
                reference_order: 0,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            }]));
            ep
        };
        let auth = Auth::None;

        let list_path = with_body(TypeRef::List(Box::new(TypeRef::Primitive(Prim::Int))));
        let mut ctx = example_ctx(&[], &[], &auth);
        assert!(
            build_example(
                &list_path,
                false,
                "conversations",
                "fern",
                "FernApi",
                &mut ctx,
            )
            .is_none(),
            "a list path parameter beside a body suppresses the example"
        );

        let scalar_path = with_body(TypeRef::Primitive(Prim::Int));
        let mut ctx = example_ctx(&[], &[], &auth);
        assert!(
            build_example(
                &scalar_path,
                false,
                "conversations",
                "fern",
                "FernApi",
                &mut ctx,
            )
            .is_some(),
            "a scalar path parameter beside a body keeps it"
        );

        let mut bodyless = with_body(TypeRef::List(Box::new(TypeRef::Primitive(Prim::Int))));
        bodyless.request_body = None;
        let mut ctx = example_ctx(&[], &[], &auth);
        assert!(
            build_example(
                &bodyless,
                false,
                "conversations",
                "fern",
                "FernApi",
                &mut ctx,
            )
            .is_some(),
            "a list path parameter with no body keeps it"
        );
    }
    #[test]
    fn referenced_request_examples_preserve_fern_importer_names() {
        let alias = TypeDecl::Alias(AliasType {
            reach_refs: Vec::new(),
            name: "EpsBearerContainer".to_string(),
            module: "eps_bearer_container".to_string(),
            target: TypeRef::Primitive(Prim::Str),
            docstring: None,
            min_length: None,
            max_length: None,
        });
        let mut ep = endpoint("/contexts", Vec::new(), Some(TypeRef::Primitive(Prim::Str)));
        ep.http_method = "POST";
        ep.request_body_required = true;
        ep.request_body_has_multipart_related = true;
        ep.reference_body_example = Some(serde_json::json!({
            "epsBearerSetup": [null, null],
            "5gMmCauseValue": 0,
        }));
        ep.request_body = Some(RequestBody::Inline(vec![
            BodyField {
                wire_name: "epsBearerSetup".to_string(),
                py_name: "eps_bearer_setup".to_string(),
                type_ref: TypeRef::List(Box::new(TypeRef::Named("EpsBearerContainer".to_string()))),
                optional: true,
                nullable: false,
                spec_required: false,
                docstring: None,
                convert: true,
                is_file: false,
                form_json: false,
                form_content_type: None,
                collision_prefix: None,
                inline_object: false,
                example: Some("[null,null]".to_string()),
                media_example: true,
                schema_body_example: true,
                reference_order: 0,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            },
            BodyField {
                wire_name: "5gMmCauseValue".to_string(),
                py_name: "_5g_mm_cause_value".to_string(),
                type_ref: TypeRef::Primitive(Prim::Int),
                optional: true,
                nullable: false,
                spec_required: false,
                docstring: None,
                convert: false,
                is_file: false,
                form_json: false,
                form_content_type: None,
                collision_prefix: None,
                inline_object: false,
                example: Some("0".to_string()),
                media_example: true,
                schema_body_example: true,
                reference_order: 1,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            },
        ]));

        let auth = Auth::None;
        let types = [alias];
        let mut ctx = example_ctx(&types, &[], &auth);
        let executable = build_example(&ep, false, "contexts", "fern", "FernApi", &mut ctx)
            .expect("referenced request has an executable example")
            .join("\n");
        assert!(executable.contains("eps_bearer_setup=[\"epsBearerSetup\", \"epsBearerSetup\"]"));
        assert!(executable.contains("f_5g_mm_cause_value=0"));

        let mut ctx = example_ctx(&types, &[], &auth);
        let reference = build_documentation_example(
            &ep, false, "contexts", "fern", "FernApi", &mut ctx, None, true,
        )
        .expect("referenced request has a reference example")
        .join("\n");
        assert!(reference.contains("f_value=0"), "{reference}");
        assert!(reference.contains("\"epsBearerSetup\""), "{reference}");

        ep.request_body_required = false;
        ep.request_body_has_multipart_related = false;
        let mut ctx = example_ctx(&types, &[], &auth);
        let optional = build_example(&ep, false, "contexts", "fern", "FernApi", &mut ctx)
            .expect("optional request has an example")
            .join("\n");
        assert!(optional.contains("_5g_mm_cause_value=0"), "{optional}");

        ep.request_body = Some(RequestBody::Form(FormBody {
            multipart: true,
            fields: vec![BodyField {
                wire_name: "binaryDataN1SmMessage".to_string(),
                py_name: "binary_data_n1sm_message".to_string(),
                type_ref: TypeRef::Primitive(Prim::Bytes),
                optional: true,
                nullable: false,
                spec_required: false,
                docstring: None,
                convert: false,
                is_file: true,
                form_json: false,
                form_content_type: Some("application/vnd.3gpp.5gnas".to_string()),
                collision_prefix: None,
                inline_object: false,
                example: None,
                media_example: false,
                schema_body_example: false,
                reference_order: 0,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            }],
        }));
        ep.body_schema_shape = BodySchemaShape::Ref;
        assert!(readme_endpoint_eligible(&ep, &[], &[]));
        let mut ctx = example_ctx(&types, &[], &auth);
        let form = build_documentation_example(
            &ep, false, "contexts", "fern", "FernApi", &mut ctx, None, false,
        )
        .expect("multipart related request has a README example")
        .join("\n");
        assert!(
            form.contains("binary_data_n1sm_message=\"example_binaryDataN1SmMessage\""),
            "{form}"
        );
    }

    #[test]
    fn the_empty_namespace_is_an_importable_underscore_package() {
        let mut root = endpoint("/root", Vec::new(), Some(TypeRef::Primitive(Prim::Str)));
        root.openapi_31 = true;
        root.module.clear();
        root.docstring = Some("Root operation.".to_string());
        root.response_doc = Some("Root response.".to_string());
        root.query_params.push(QueryParam {
            wire_name: "filter".to_string(),
            py_name: "filter".to_string(),
            type_ref: TypeRef::Primitive(Prim::Str),
            required: false,
            nullable: false,
            default: None,
            convert: false,
            comma_separated: false,
            allow_multiple: false,
            one_or_many: false,
            example: Some("\"active\"".to_string()),
            example_is_scalar: true,
            aliased_datetime: None,
            docstring: Some("Optional filter.".to_string()),
        });
        let mut explicit_empty =
            endpoint("/empty", Vec::new(), Some(TypeRef::Primitive(Prim::Str)));
        explicit_empty.module = "_".to_string();
        explicit_empty.method_name = "empty".to_string();

        let mut ir = ir_with(vec![root, explicit_empty]);
        ir.endpoint_modules = vec!["_".to_string()];
        ir.tag_types = vec![TagTypeDecl {
            module: "_".to_string(),
            decl: TypeDecl::Alias(AliasType {
                reach_refs: Vec::new(),
                name: "EmptyResult".to_string(),
                module: "empty_result".to_string(),
                target: TypeRef::Primitive(Prim::Str),
                docstring: None,
                min_length: None,
                max_length: None,
            }),
        }];
        let files = generate(&ir).expect("root and empty namespaces generate");
        let file = |rel: &str| {
            files
                .iter()
                .find(|file| file.path == std::path::Path::new(rel))
                .unwrap_or_else(|| panic!("{rel} is not generated"))
        };
        // The `_` namespace is a package of its own, its types inside it, and
        // the root client reaches it through a `_` property.
        for rel in [
            "src/fern/_/__init__.py",
            "src/fern/_/client.py",
            "src/fern/_/raw_client.py",
            "src/fern/_/types/empty_result.py",
            "src/fern/raw_client.py",
        ] {
            file(rel);
        }
        assert!(!files
            .iter()
            .any(|file| file.path == std::path::Path::new("src/fern/types/empty_result.py")));
        let root = &file("src/fern/client.py").contents;
        assert!(root.contains("class FernApi:") && root.contains("    def _(self):"));
        assert!(file("src/fern/_/client.py")
            .contents
            .contains("from ..core.client_wrapper import"));
        assert!(file("reference.md").contents.contains("client._.empty("));
    }

    #[test]
    fn worked_example_renders_every_optional_query_example_it_is_given() {
        let mut ep = endpoint("/widgets", Vec::new(), Some(TypeRef::Primitive(Prim::Str)));
        ep.query_params = vec![
            QueryParam {
                wire_name: "where".to_string(),
                py_name: "where_".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                required: false,
                nullable: false,
                default: None,
                convert: false,
                comma_separated: false,
                allow_multiple: false,
                one_or_many: false,
                example: Some("\"active\"".to_string()),
                example_is_scalar: true,
                aliased_datetime: None,
                docstring: None,
            },
            QueryParam {
                wire_name: "order".to_string(),
                py_name: "order".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                required: false,
                nullable: false,
                default: None,
                convert: false,
                comma_separated: false,
                allow_multiple: false,
                one_or_many: false,
                example: Some("\"name\"".to_string()),
                example_is_scalar: true,
                aliased_datetime: None,
                docstring: None,
            },
            QueryParam {
                wire_name: "page".to_string(),
                py_name: "page".to_string(),
                type_ref: TypeRef::Primitive(Prim::Int),
                required: false,
                nullable: false,
                default: None,
                convert: false,
                comma_separated: false,
                allow_multiple: false,
                one_or_many: false,
                example: Some("1".to_string()),
                example_is_scalar: true,
                aliased_datetime: None,
                docstring: None,
            },
        ];

        let mut ctx = example_ctx(&[], &[], &Auth::None);
        let rendered = build_example(&ep, false, "widgets", "acme", "AcmeApi", &mut ctx)
            .expect("bodyless query endpoint has a worked example")
            .join("\n");
        // Whether an optional query example survives at all is decided while
        // building the IR (see `ir::parameter_example_value`) — Fern discards a
        // parameter-level example on a non-string parameter there, so by the time
        // the emitter sees one it renders it whatever its type is, and a mixed-type
        // run comes out whole (`med-anvisa-price`, `sac-backend`).
        assert!(rendered.contains("where_=\"active\""), "{rendered}");
        assert!(rendered.contains("order=\"name\""), "{rendered}");
        assert!(rendered.contains("page=1"), "{rendered}");
    }

    #[test]
    fn stream_methods_document_and_delegate_every_parameter_in_both_modes() {
        let mut ep = endpoint(
            "/events/{id}",
            vec![PathParam {
                wire_name: "id".to_string(),
                py_name: "id".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                docstring: Some("Event identifier.\nSecond line.".to_string()),
                example: None,
                pattern_constrained: false,
                convert: false,
            }],
            Some(TypeRef::Primitive(Prim::Str)),
        );
        ep.streaming = true;
        ep.docstring = Some("Streams events.\nAs they arrive.".to_string());
        ep.response_doc = Some("Decoded chunks.".to_string());
        ep.query_params.push(QueryParam {
            wire_name: "cursor".to_string(),
            py_name: "cursor".to_string(),
            type_ref: TypeRef::Primitive(Prim::Str),
            required: true,
            nullable: false,
            default: None,
            convert: false,
            comma_separated: false,
            allow_multiple: false,
            one_or_many: false,
            example: None,
            example_is_scalar: true,
            aliased_datetime: None,
            docstring: Some("Starting cursor.".to_string()),
        });
        ep.header_params.push(HeaderParam {
            wire_name: "X-Mode".to_string(),
            py_name: "mode".to_string(),
            type_ref: TypeRef::Primitive(Prim::Str),
            required: true,
            docstring: Some("Stream mode.".to_string()),
            example: None,
            enum_value: false,
        });
        ep.request_body = Some(RequestBody::Form(FormBody {
            fields: vec![BodyField {
                wire_name: "filter".to_string(),
                py_name: "filter_".to_string(),
                type_ref: TypeRef::Primitive(Prim::Str),
                optional: false,
                nullable: false,
                spec_required: true,
                docstring: Some("Filter expression.".to_string()),
                convert: false,
                is_file: false,
                form_json: false,
                form_content_type: None,
                collision_prefix: None,
                inline_object: false,
                example: Some("\"open\"".to_string()),
                media_example: false,
                schema_body_example: false,
                reference_order: 0,
                deprecated: false,
                declaration_order: 0,
                markdown_order: 0,
                admits_only_empty_object: false,
            }],
            multipart: false,
        }));

        let auth = Auth::None;
        let tags = std::collections::BTreeMap::new();
        let cx = ClientCtx {
            yaml_unquoted_timestamps: None,
            pkg: "acme",
            client_name: "AcmeApi",
            module: "events",
            types: &[],
            enum_type: crate::settings::EnumType::default(),
            tag_decls: &[],
            auth: &auth,
            has_environment: false,
            tag_types: &tags,
            global_headers: &[],
            client_path_parameters: &[],
            sdk_first_party: true,
            children: &[],
        };
        let mut imports = Imports::at(RefLoc::Client("events".to_string()), &tags);
        let raw_sync = raw_method(&ep, false, &mut imports);
        let raw_async = raw_method(&ep, true, &mut imports);
        let client_sync = client_method(&cx, &ep, false, &mut imports);
        let client_async = client_method(&cx, &ep, true, &mut imports);
        for output in [&raw_sync, &raw_async, &client_sync, &client_async] {
            assert!(output.contains("id : str"), "{output}");
            assert!(output.contains("cursor : str"), "{output}");
            assert!(output.contains("mode : str"), "{output}");
            assert!(output.contains("filter_ : str"), "{output}");
        }
        assert!(raw_sync.contains("yield _stream()"), "{raw_sync}");
        assert!(raw_async.contains("yield await _stream()"), "{raw_async}");
        assert!(client_sync.contains("yield from r.data"), "{client_sync}");
        assert!(
            client_async.contains("async for _chunk in r.data"),
            "{client_async}"
        );

        ep.streaming = false;
        ep.binary_response = true;
        let raw_binary = raw_method(&ep, true, &mut imports);
        let client_binary = client_method(&cx, &ep, true, &mut imports);
        assert!(raw_binary.contains("aiter_bytes"), "{raw_binary}");
        assert!(raw_binary.contains("Streams events."), "{raw_binary}");
        assert!(
            client_binary.contains("typing.AsyncIterator[bytes]"),
            "{client_binary}"
        );
        assert!(
            client_binary.contains("Filter expression."),
            "{client_binary}"
        );
    }

    #[test]
    fn recursive_alias_enum_docs_and_render_failures_keep_context() {
        let alias = TypeDecl::Alias(AliasType {
            reach_refs: Vec::new(),
            name: "NodeList".to_string(),
            module: "node_list".to_string(),
            target: TypeRef::List(Box::new(TypeRef::Named("Node".to_string()))),
            docstring: None,
            min_length: None,
            max_length: None,
        });
        let rendered = render_type_decl(
            &environment(),
            &alias,
            RefLoc::RootTypes,
            &Default::default(),
            DeclSettings::default(),
            &std::collections::HashSet::from(["Node".to_string()]),
            None,
        )
        .expect("recursive alias renders");
        assert!(rendered.contains("from __future__ import annotations"));
        assert!(rendered.contains("if typing.TYPE_CHECKING:"));
        assert!(rendered.contains("from .node import Node"));
        assert!(rendered.contains("NodeList = typing.List[\"Node\"]"));

        let singleton = TypeDecl::DiscriminatedUnion(DiscriminatedUnion {
            base_fields: Vec::new(),
            discriminant_name: None,
            name: "Content".to_string(),
            module: "content".to_string(),
            discriminant_property: "type".to_string(),
            members: vec![UnionMember {
                wrapped: false,
                class_name: "Content_Text".to_string(),
                discriminant: "text".to_string(),
                discriminant_index: None,
                fields: vec![model_field("text", TypeRef::Primitive(Prim::Str), true)],
                source: None,
                docstring: None,
            }],
            variant_targets: Vec::new(),
            docstring: None,
        });
        let rendered = render_type_decl(
            &environment(),
            &singleton,
            RefLoc::RootTypes,
            &Default::default(),
            DeclSettings::default(),
            &Default::default(),
            None,
        )
        .expect("singleton union renders");
        assert!(rendered.contains("Content = Content_Text"), "{rendered}");
        assert!(!rendered.contains("typing_extensions"), "{rendered}");

        let enum_output = render_enum(
            &environment(),
            &EnumType {
                name: "State".to_string(),
                module: "state".to_string(),
                members: vec![EnumMember {
                    name: "READY".to_string(),
                    visit_param: "ready".to_string(),
                    value: "ready\"now".to_string(),
                    docstring: Some("Ready member.".to_string()),
                }]
                .into(),
                docstring: Some("State enum.".to_string()),
            },
            &RefLoc::RootTypes,
        )
        .expect("documented enum renders");
        assert!(enum_output.contains("from ..core import enum"));
        assert!(enum_output.contains("class State(enum.StrEnum):"));
        assert!(enum_output.contains("State enum."));
        assert!(enum_output.contains("READY = \"ready\\\"now\""));
        assert!(enum_output.contains("Ready member."));

        let error = render(
            &environment(),
            "missing-template.py",
            "broken.py",
            minijinja::Value::UNDEFINED,
        )
        .expect_err("missing template is reported");
        assert!(error.to_string().contains("broken.py"), "{error}");

        let mut source = "        \"\"\"\n        ```python\n\n        pass\n        ```\n        \"\"\"\n\n        \"\"\"\n        plain\n\n".to_string();
        super::preserve_fenced_docstring_blank_indent(&mut source);
        assert!(source.contains("        ```python\n        \n        pass"));
        assert!(source.ends_with("        plain\n\n"));
    }
    #[test]
    fn colliding_query_and_body_arguments_use_their_own_values() {
        let files = files_for(serde_json::json!({
            "openapi": "3.0.3", "info": {"title": "Lantern Circuits", "version": "1"},
            "paths": {"/circuits": {"post": {
                "operationId": "setVoltage",
                "parameters": [{"name": "voltage", "in": "query", "schema": {"type": "integer"}}],
                "requestBody": {"content": {"application/json": {
                    "schema": {"$ref": "#/components/schemas/PowerProfile"}
                }}},
                "responses": {"200": {"description": "Recorded", "content": {
                    "application/json": {"schema": {"$ref": "#/components/schemas/PowerProfile"}}
                }}}
            }}},
            "components": {"schemas": {"PowerProfile": {"type": "object", "properties": {
                "voltage": {"type": "integer"}, "channel": {"type": "string"}
            }}}}
        }));
        let raw = files
            .iter()
            .find(|(path, _)| path.ends_with("/raw_client.py"))
            .unwrap()
            .1
            .as_str();
        assert!(raw.contains("power_profile_voltage:"), "{raw}");
        assert!(
            raw.contains("json={\n                \"voltage\": power_profile_voltage,"),
            "{raw}"
        );
        assert!(
            raw.contains("params={\n                \"voltage\": voltage,"),
            "{raw}"
        );
    }

    /// A 3.0 document posting `body` to `/leads` and answering JSON.
    fn lead_document(body: serde_json::Value) -> serde_json::Value {
        serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "paths": {"/leads": {"post": {
                "operationId": "leadsAdd",
                "tags": ["Leads"],
                "requestBody": {"required": true,
                    "content": {"application/json": {"schema": body}}},
                "responses": {"200": {"description": "OK", "content": {"application/json":
                    {"schema": {"type": "object", "properties": {"id": {"type": "string"}}}}}}}
            }}}
        })
    }

    #[test]
    fn a_required_nullable_body_field_is_shown_by_its_example_or_as_none_in_markdown() {
        let files = files_for(lead_document(serde_json::json!({
            "type": "object",
            "required": ["name", "company_name", "phone"],
            "properties": {
                "name": {"type": "string", "example": "Elon Musk"},
                "company_name": {"type": "string", "nullable": true, "example": "Spacex"},
                "phone": {"type": "string", "nullable": true}
            }
        })));
        let client = file(&files, "leads/client.py");
        // The signature still defaults both nullable fields.
        assert!(
            client.contains("company_name: typing.Optional[str] = OMIT"),
            "{client}"
        );
        assert!(
            client.contains(
                "client.leads.add(\n            name=\"Elon Musk\",\n            company_name=\"Spacex\",\n        )"
            ),
            "{client}"
        );
        let reference = file(&files, "reference.md");
        assert!(
            reference.contains(
                "client.leads.add(\n    name=\"Elon Musk\",\n    company_name=\"Spacex\",\n    phone=None,\n)"
            ),
            "{reference}"
        );
    }

    #[test]
    fn a_union_variants_required_nullable_field_is_none_in_markdown_only() {
        let files = files_for({
            let mut document = lead_document(serde_json::json!({
                "oneOf": [{"$ref": "#/components/schemas/Project"},
                          {"$ref": "#/components/schemas/Solver"}],
                "discriminator": {"propertyName": "kind", "mapping": {
                    "PROJECT": "#/components/schemas/Project",
                    "SOLVER": "#/components/schemas/Solver"}}
            }));
            document["components"] = serde_json::json!({"schemas": {
                "Project": {"type": "object", "required": ["kind", "projectId", "defaults"],
                    "properties": {
                        "kind": {"type": "string", "enum": ["PROJECT"]},
                        "projectId": {"type": "string"},
                        "defaults": {"type": "string", "nullable": true}
                    }},
                "Solver": {"type": "object", "required": ["kind", "solverKey"],
                    "properties": {
                        "kind": {"type": "string", "enum": ["SOLVER"]},
                        "solverKey": {"type": "string"}
                    }}
            }});
            document
        });
        let client = file(&files, "leads/client.py");
        let docstring_call = client
            .split("client.leads.add(")
            .nth(1)
            .and_then(|rest| rest.split("\"\"\"").next())
            .expect("the docstring has a worked example");
        assert!(
            docstring_call.contains("project_id=\"projectId\""),
            "{docstring_call}"
        );
        assert!(!docstring_call.contains("defaults"), "{docstring_call}");
        let reference = file(&files, "reference.md");
        assert!(
            reference.contains("        defaults=None,\n"),
            "{reference}"
        );
    }

    /// `GET /files/{id}/download` answering `media` with a binary string, its
    /// path parameter and optional `fields` query both declaring examples.
    fn download_document(media: &str) -> serde_json::Value {
        serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "paths": {"/files/{id}/download": {"get": {
                "operationId": "filesDownload",
                "tags": ["Files"],
                "parameters": [
                    {"name": "id", "in": "path", "required": true,
                     "schema": {"type": "string", "example": "12345"}},
                    {"name": "fields", "in": "query", "required": false,
                     "schema": {"type": "string", "example": "id,updated_at"}}
                ],
                "responses": {
                    "200": {"description": "OK", "content": {media:
                        {"schema": {"type": "string", "format": "binary"}}}},
                    "404": {"description": "Missing", "content": {"application/json":
                        {"schema": {"type": "object",
                            "properties": {"message": {"type": "string"}}}}}}
                }
            }}}
        })
    }

    #[test]
    fn a_wildcard_download_documents_its_required_arguments_by_name() {
        let files = files_for(download_document("*/*"));
        let client = file(&files, "files/client.py");
        assert!(
            client.contains("client.files.download(\n            id=\"id\",\n        )"),
            "{client}"
        );
        // Any other binary media keeps the optional parameter's example.
        let files = files_for(download_document("application/octet-stream"));
        let client = file(&files, "files/client.py");
        assert!(client.contains("fields=\"id,updated_at\""), "{client}");
    }

    #[test]
    fn a_markdown_answer_documents_its_path_arguments() {
        let files = files_for(serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "paths": {"/connectors/{id}/docs/{doc_id}": {"get": {
                "operationId": "connectorDocsOne",
                "tags": ["Docs"],
                "parameters": [
                    {"name": "id", "in": "path", "required": true, "schema": {"type": "string"}},
                    {"name": "doc_id", "in": "path", "required": true, "schema": {"type": "string"}}
                ],
                "responses": {"200": {"description": "OK", "content": {"text/markdown":
                    {"schema": {"type": "string", "format": "markdown"}}}}}
            }}}
        }));
        let client = file(&files, "docs/client.py");
        assert!(
            client.contains(
                "client.docs.connector_docs_one(\n            id=\"id\",\n            doc_id=\"doc_id\",\n        )"
            ),
            "{client}"
        );
        let reference = file(&files, "reference.md");
        assert!(
            reference.contains("    doc_id=\"doc_id\",\n"),
            "{reference}"
        );
    }

    #[test]
    fn an_async_stream_is_iterated_without_await_in_the_docstring() {
        let files = files_for(serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "paths": {"/events": {"get": {
                "operationId": "getEvents",
                "tags": ["Events"],
                "responses": {"200": {"description": "OK", "content": {"text/event-stream":
                    {"schema": {"type": "string"}}}}}
            }}}
        }));
        let client = file(&files, "events/client.py");
        assert!(
            client.contains(
                "        async def main() -> None:\n            response = client.events.get_events()\n            async for chunk in response:"
            ),
            "{client}"
        );
        assert!(
            client.contains(
                "        response = client.events.get_events()\n        for chunk in response:"
            ),
            "{client}"
        );
        assert!(
            !client.contains("await client.events.get_events()"),
            "{client}"
        );
    }

    #[test]
    fn a_wildcard_binary_body_is_documented_as_bytes() {
        let files = files_for(serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "paths": {"/files": {"post": {
                "operationId": "filesUpload",
                "tags": ["Files"],
                "requestBody": {"required": true, "content": {"*/*":
                    {"schema": {"type": "string", "format": "binary"}}}},
                "responses": {"200": {"description": "OK", "content": {"application/json":
                    {"schema": {"type": "object", "properties": {"id": {"type": "string"}}}}}}}
            }}}
        }));
        let reference = file(&files, "reference.md");
        assert!(reference.contains("**request:** `bytes` "), "{reference}");
    }

    #[test]
    fn the_readme_iterates_an_async_stream_with_its_arguments_elided() {
        let stream = |parameters: serde_json::Value| {
            files_for(serde_json::json!({
                "openapi": "3.0.3",
                "info": {"title": "Api", "version": "1"},
                "paths": {"/events": {"get": {
                    "operationId": "watchEvents",
                    "tags": ["Events"],
                    "parameters": parameters,
                    "responses": {"200": {"description": "OK", "content": {"text/event-stream":
                        {"schema": {"type": "string"}}}}}
                }}}
            }))
        };
        let files = stream(serde_json::json!([]));
        let readme = file(&files, "README.md");
        assert!(
            readme.contains(
                "async def main() -> None:\n    async for chunk in client.events.watch_events():\n        print(chunk)\n"
            ),
            "{readme}"
        );
        // An optional parameter the sync call leaves out is still elided as `...`.
        let files = stream(serde_json::json!([
            {"name": "since", "in": "query", "schema": {"type": "string"}}
        ]));
        let readme = file(&files, "README.md");
        assert!(
            readme.contains("client.events.watch_events()\n```"),
            "{readme}"
        );
        assert!(
            readme.contains("    async for chunk in client.events.watch_events(...):\n"),
            "{readme}"
        );
    }

    #[test]
    fn a_patterned_path_parameter_keeps_a_downloads_worked_example() {
        let files = files_for(serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "paths": {"/guilds/{guild_id}/widget.png": {"get": {
                "operationId": "getGuildWidgetPng",
                "tags": ["Guilds"],
                "parameters": [{"name": "guild_id", "in": "path", "required": true,
                    "schema": {"type": "string", "pattern": "^(0|[1-9][0-9]*)$"}}],
                "responses": {
                    "200": {"description": "OK", "content": {"image/png":
                        {"schema": {"type": "string", "format": "binary"}}}},
                    "404": {"description": "Missing", "content": {"application/json":
                        {"schema": {"type": "object",
                            "properties": {"message": {"type": "string"}}}}}}
                }
            }}}
        }));
        let client = file(&files, "guilds/client.py");
        assert!(client.contains("guild_id=\"guild_id\","), "{client}");
    }

    #[test]
    fn an_optional_field_narrowed_to_an_empty_model_is_no_argument_nor_import() {
        let files = files_for(serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "paths": {"/expectations": {"put": {
                "operationId": "putExpectation",
                "tags": ["Expectations"],
                "requestBody": {"required": true, "content": {"application/json": {
                    "schema": {"$ref": "#/components/schemas/Expectation"},
                    "example": {"matcher": {"method": "GET"}, "status": 200}
                }}},
                "responses": {"204": {"description": "Stored"}}
            }}},
            "components": {"schemas": {
                "Expectation": {"type": "object", "properties": {
                    "matcher": {"$ref": "#/components/schemas/Matcher"},
                    "status": {"type": "integer"}
                }},
                "Matcher": {"oneOf": [
                    {"$ref": "#/components/schemas/PathMatcher"},
                    {"$ref": "#/components/schemas/SpecMatcher"}
                ]},
                "PathMatcher": {"type": "object", "additionalProperties": false,
                    "properties": {"path": {"type": "string"}}},
                "SpecMatcher": {"type": "object", "additionalProperties": false,
                    "properties": {"operation": {"type": "string"}}}
            }}
        }));
        let client = file(&files, "expectations/client.py");
        let example = client
            .split("Examples\n        --------\n")
            .nth(1)
            .and_then(|rest| rest.split("\"\"\"").next())
            .expect("the docstring has a worked example");
        assert!(example.contains("status=200"), "{example}");
        assert!(!example.contains("matcher"), "{example}");
        assert!(!example.contains("PathMatcher"), "{example}");
        assert!(!example.contains("SpecMatcher"), "{example}");
    }

    #[test]
    fn a_nested_required_unknown_nullable_field_is_none_in_markdown_only() {
        let mut document = lead_document(serde_json::json!({
            "type": "object",
            "required": ["messages"],
            "properties": {"messages": {"type": "array", "items": {
                "$ref": "#/components/schemas/Message"}}}
        }));
        document["components"] = serde_json::json!({"schemas": {"Message": {
            "type": "object",
            "required": ["id", "metadata"],
            "properties": {
                "id": {"type": "string"},
                "metadata": {"anyOf": [{}, {"type": "null"}]}
            }
        }}});
        let files = files_for(document);
        let client = file(&files, "leads/client.py");
        assert!(client.contains("Message(\n"), "{client}");
        assert!(!client.contains("metadata="), "{client}");
        let reference = file(&files, "reference.md");
        assert!(reference.contains("metadata=None,"), "{reference}");
    }
}

#[cfg(test)]
mod parameter_lowering_tests {
    //! Each parameter-lowering rule driven through the whole generator, one
    //! document per rule, asserting on the files a user receives.
    use serde_json::{json, Value};

    /// Every file the generator writes for `document`, keyed by path.
    fn files(document: Value) -> std::collections::BTreeMap<String, String> {
        let doc: crate::openapi::OpenApi =
            serde_json::from_value(document).expect("document deserializes");
        let config = crate::config::GenerateConfig::new(
            std::path::PathBuf::from("openapi.yml"),
            std::path::PathBuf::from("out"),
            Some("api".to_string()),
            None,
            None,
            crate::settings::ExtraFields::default(),
            "Api",
        )
        .expect("the config is well formed");
        super::generate(&crate::ir::build(&doc, &config))
            .expect("the document generates")
            .into_iter()
            .map(|file| {
                (
                    file.path.to_string_lossy().replace('\\', "/"),
                    file.contents,
                )
            })
            .collect()
    }

    #[test]
    fn extension_parameters_keep_their_wire_names_and_defaults() {
        let document: Value = serde_json::from_str(include_str!(
            "../docs/openapi-surface/handwritten/observatory-query-extensions/openapi.yml"
        ))
        .unwrap();
        let generated = files(document);
        let client = &generated["src/api/client.py"];
        let raw = &generated["src/api/raw_client.py"];
        assert!(
            client.contains("band: typing.Optional[str] = None"),
            "{client}"
        );
        assert!(
            client.contains("size: typing.Optional[int] = \"12\""),
            "{client}"
        );
        assert!(raw.contains("\"channel\": band"), "{raw}");
    }

    #[test]
    fn document_fields_and_nullable_arrays_reach_emitter_consumers() {
        for (source, expected) in [
            (
                include_str!(
                    "../docs/openapi-surface/handwritten/observatory-client-date/openapi.yml"
                ),
                "sampling_day: typing.Optional[dt.date] = None",
            ),
            (
                include_str!(
                    "../docs/openapi-surface/handwritten/observatory-client-headers/openapi.yml"
                ),
                "station: str",
            ),
            (
                include_str!(
                    "../docs/openapi-surface/handwritten/observatory-client-variable/openapi.yml"
                ),
                "station_code: str",
            ),
        ] {
            let generated = files(serde_json::from_str(source).unwrap());
            assert!(
                generated["src/api/client.py"].contains(expected),
                "{expected}"
            );
        }
        let generated = files(
            serde_json::from_str(include_str!(
                "../docs/openapi-surface/handwritten/observatory-nullable-query/openapi.yml"
            ))
            .unwrap(),
        );
        assert!(generated["src/api/raw_client.py"].contains("\"kinds\": kinds"));
        assert!(!generated["src/api/raw_client.py"].contains("join(map"));
    }

    fn departure_guard(case: &str, rel: &str, id: &str) {
        let case_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("docs/openapi-surface/handwritten")
            .join(case);
        let document: Value =
            serde_json::from_str(&std::fs::read_to_string(case_root.join("openapi.yml")).unwrap())
                .unwrap();
        let doc: crate::openapi::OpenApi = serde_json::from_value(document).unwrap();
        let config = crate::config::GenerateConfig::new(
            "openapi.yml".into(),
            "out".into(),
            Some("fern".into()),
            None,
            None,
            crate::settings::ExtraFields::default(),
            "FernApi",
        )
        .unwrap();
        let generated = super::generate(&crate::ir::build(&doc, &config)).unwrap();
        let out = tempfile::tempdir().unwrap();
        for file in generated {
            let path = out.path().join(file.path);
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(path, file.contents).unwrap();
        }
        let expected = case_root.join("fern-expected");
        let context = crate::departures::Context::from_trees(&expected, out.path());
        let reference = std::fs::read_to_string(expected.join(rel)).unwrap();
        let actual = std::fs::read_to_string(out.path().join(rel)).unwrap();
        let compared = crate::parity::compare_file(&context, rel, &actual, &reference).unwrap();
        assert!(compared.matches());
        assert!(compared
            .departures
            .iter()
            .any(|departure| departure.id == id));
        let changed = actual.replacen(
            if rel.ends_with(".py") {
                "import typing"
            } else {
                "# Reference"
            },
            "unexplained adjacent change",
            1,
        );
        assert!(
            !crate::parity::compare_file(&context, rel, &changed, &reference)
                .unwrap()
                .matches()
        );
    }

    #[test]
    fn sdk_variable_documentation_departure_rejects_an_adjacent_mismatch() {
        departure_guard(
            "observatory-client-variable",
            "reference.md",
            "sdk-variable-docs-examples",
        );
    }

    #[test]
    fn date_header_example_departure_rejects_an_adjacent_mismatch() {
        departure_guard(
            "observatory-client-date",
            "src/fern/client.py",
            "date-header-constructor-example",
        );
    }

    #[test]
    fn reference_string_map_body_uses_declared_example_or_placeholder() {
        for example in [
            Some(serde_json::json!({"cloud.provider": "aws", "tasks.max": "2"})),
            None,
        ] {
            let mut media = serde_json::json!({
                "schema": {"type": "object", "additionalProperties": {"type": "string"}}
            });
            if let Some(example) = example.as_ref() {
                media["example"] = example.clone();
            }
            let generated = files(serde_json::json!({
                "openapi": "3.0.0", "info": {"title": "Connector configuration", "version": "1"},
                "paths": {"/config": {"put": {
                    "operationId": "validateConfig",
                    "requestBody": {"content": {"application/json": media}},
                    "responses": {"200": {"description": "Validated"}}
                }}}
            }));
            for reference in [&generated["reference.md"], &generated["src/api/client.py"]] {
                if example.is_some() {
                    assert!(
                        reference.contains("\"cloud.provider\": \"aws\""),
                        "{reference}"
                    );
                    assert!(reference.contains("\"tasks.max\": \"2\""), "{reference}");
                    assert!(!reference.contains("\"key\": \"value\""), "{reference}");
                } else {
                    assert!(reference.contains("\"key\": \"value\""), "{reference}");
                }
            }
        }
    }

    /// A document of `GET` operations tagged `sky`, one per `(path, parameters)`.
    fn document(version: &str, operations: &[(&str, Value)]) -> Value {
        let mut paths = serde_json::Map::new();
        for (name, parameters) in operations {
            paths.insert(
                format!("/sky/{name}"),
                json!({"get": {
                    "operationId": name,
                    "tags": ["sky"],
                    "parameters": parameters,
                    "responses": {"200": {"description": "ok", "content": {"application/json": {"schema": {"type": "string"}}}}}
                }}),
            );
        }
        json!({"openapi": version, "info": {"title": "Api", "version": "1"}, "paths": paths})
    }

    fn query(required: bool, schema: Value) -> Value {
        json!([{"name": "band", "in": "query", "required": required, "schema": schema}])
    }

    fn has(files: &std::collections::BTreeMap<String, String>, path: &str) -> bool {
        files.contains_key(path)
    }

    #[test]
    fn a_query_union_sits_in_the_tag_exactly_when_required_iff_not_nullable() {
        let mix = json!([{"type": "string"}, {"type": "integer"}]);
        let mix_null = json!([{"type": "string"}, {"type": "integer"}, {"type": "null"}]);
        let consts = json!([{"const": "near"}, {"const": "far"}]);
        let files = files(document(
            "3.1.0",
            &[
                ("reqMix", query(true, json!({"anyOf": mix}))),
                (
                    "optMix",
                    query(false, json!({"anyOf": mix, "title": "Band"})),
                ),
                ("optNullMix", query(false, json!({"anyOf": mix_null}))),
                ("reqNullMix", query(true, json!({"oneOf": mix_null}))),
                ("optConsts", query(false, json!({"anyOf": consts}))),
                (
                    "optNullable",
                    query(false, json!({"anyOf": mix, "nullable": true})),
                ),
                (
                    "headerMix",
                    json!([{"name": "X-Band", "in": "header", "required": false, "schema": {"anyOf": mix}}]),
                ),
                (
                    "soleEnum",
                    query(
                        true,
                        json!({"anyOf": [{"type": "string", "enum": ["a", "b"]}, {"type": "null"}]}),
                    ),
                ),
            ],
        ));
        for tag_local in [
            "src/api/sky/types/req_mix_request_band.py",
            "src/api/sky/types/opt_null_mix_request_band.py",
            "src/api/sky/types/opt_nullable_request_band.py",
            "src/api/sky/types/header_mix_request_x_band.py",
            "src/api/sky/types/sole_enum_request_band.py",
        ] {
            assert!(has(&files, tag_local), "{tag_local} missing");
        }
        for root in [
            "src/api/types/opt_mix_request_band.py",
            "src/api/types/req_null_mix_request_band.py",
            "src/api/types/opt_consts_request_band.py",
        ] {
            assert!(has(&files, root), "{root} missing");
        }
        // A 3.0 `nullable: true` beside the composition makes the argument optional.
        let raw = &files["src/api/sky/raw_client.py"];
        assert!(
            raw.contains("band: typing.Optional[OptNullableRequestBand] = None"),
            "{raw}"
        );
    }

    #[test]
    fn an_array_query_parameter_names_its_union_element() {
        let files = files(document(
            "3.1.0",
            &[
                (
                    "focus",
                    query(
                        false,
                        json!({"type": "array", "items": {"anyOf": [
                            {"type": "string", "enum": ["wide", "tele"]}, {"type": "integer"}
                        ]}}),
                    ),
                ),
                (
                    "nullItems",
                    query(
                        true,
                        json!({"type": "array", "items": {"anyOf": [
                            {"type": "string"}, {"type": "integer"}, {"type": "null"}
                        ]}}),
                    ),
                ),
            ],
        ));
        assert_eq!(
            files["src/api/sky/types/focus_request_band_item.py"]
                .lines()
                .last()
                .unwrap_or_default(),
            "FocusRequestBandItem = typing.Union[FocusRequestBandItemZero, int]"
        );
        assert!(has(
            &files,
            "src/api/sky/types/focus_request_band_item_zero.py"
        ));
        let raw = &files["src/api/sky/raw_client.py"];
        assert!(
            raw.contains(
                "typing.Union[FocusRequestBandItem, typing.Sequence[FocusRequestBandItem]]"
            ),
            "{raw}"
        );
        // A `null` member leaves the alias, and the signature names it bare.
        assert!(
            raw.contains("typing.Sequence[NullItemsRequestBandItem]"),
            "{raw}"
        );
        // The scalar element reaches the URL as text.
        assert!(raw.contains("\"band\": band,"), "{raw}");
        // A required one's docstring passes an empty list, as certified Fern's
        // does for any items admitting `null`.
        let client = &files["src/api/sky/client.py"];
        assert!(client.contains("band=[],"), "{client}");
        assert!(!client.contains("band=[\"band\"]"), "{client}");
    }

    #[test]
    fn a_scalar_or_array_query_parameter_is_one_or_many_only_where_required_iff_nullable() {
        let one_or_many = |null: bool| {
            let mut members = vec![
                json!({"type": "integer"}),
                json!({"type": "array", "items": {"type": "integer"}}),
            ];
            if null {
                members.push(json!({"type": "null"}));
            }
            json!({"oneOf": members})
        };
        let files = files(document(
            "3.1.0",
            &[
                ("required", query(true, one_or_many(false))),
                ("requiredNull", query(true, one_or_many(true))),
                ("optional", query(false, one_or_many(false))),
                (
                    "enumOrMany",
                    query(
                        false,
                        json!({"anyOf": [
                            {"type": "string", "enum": ["x", "y"]},
                            {"type": "array", "items": {"type": "string", "enum": ["x", "y"]}}
                        ]}),
                    ),
                ),
            ],
        ));
        let raw = &files["src/api/sky/raw_client.py"];
        // Required and not nullable: a named, required, converted union.
        assert!(has(&files, "src/api/sky/types/required_request_band.py"));
        assert!(raw.contains("band: RequiredRequestBand,"), "{raw}");
        assert!(
            raw.contains("object_=band, annotation=RequiredRequestBand, direction=\"write\""),
            "{raw}"
        );
        // The other diagonal keeps the optional one-or-many shorthand.
        assert!(
            raw.contains("band: typing.Optional[typing.Union[int, typing.Sequence[int]]] = None"),
            "{raw}"
        );
        assert!(!has(&files, "src/api/types/optional_request_band.py"));
        // An enum spelling is a union whose array member names its element.
        assert!(has(
            &files,
            "src/api/types/enum_or_many_request_band_one_item.py"
        ));
        // A required, nullable one is exampled by its scalar in the docstring and
        // as a list in the reference.
        let client = &files["src/api/sky/client.py"];
        assert!(
            client.contains("client.sky.required_null(\n            band=1,\n        )"),
            "{client}"
        );
        assert!(
            client.contains("client.sky.required(\n            band=1,\n        )"),
            "{client}"
        );
        let reference = &files["reference.md"];
        assert!(
            reference.contains("client.sky.required_null(\n    band=[\n        1\n    ],\n)"),
            "{reference}"
        );
    }

    #[test]
    fn a_query_union_over_a_component_string_enum_is_written_raw() {
        let mut document = document(
            "3.1.0",
            &[(
                "weave",
                query(
                    true,
                    json!({"anyOf": [{"type": "string"}, {"$ref": "#/components/schemas/Weave"}]}),
                ),
            )],
        );
        document["components"] =
            json!({"schemas": {"Weave": {"type": "string", "enum": ["plain", "twill"]}}});
        let files = files(document);
        let raw = &files["src/api/sky/raw_client.py"];
        assert!(raw.contains("\"band\": band,"), "{raw}");
        assert!(
            !raw.contains("convert_and_respect_annotation_metadata"),
            "{raw}"
        );
        // The `str` member ahead of the enum holds its value as plain text.
        let client = &files["src/api/sky/client.py"];
        assert!(client.contains("band=\"plain\","), "{client}");
    }

    fn header_document(schemes: Value, names: &[&str]) -> Value {
        let operations: Vec<(String, Value)> = names
            .iter()
            .enumerate()
            .map(|(index, name)| {
                (
                    format!("fly{index}"),
                    json!([{"name": name, "in": "header", "required": false, "schema": {"type": "string"}}]),
                )
            })
            .collect();
        let borrowed: Vec<(&str, Value)> = operations
            .iter()
            .map(|(name, value)| (name.as_str(), value.clone()))
            .collect();
        let mut document = document("3.0.3", &borrowed);
        document["components"] = json!({"securitySchemes": schemes});
        document["security"] = json!([{"tok": []}]);
        document
    }

    #[test]
    fn only_the_exact_spelling_of_a_credential_header_is_dropped() {
        let bearer = files(header_document(
            json!({"tok": {"type": "http", "scheme": "bearer"}}),
            &["authorization", "AUTHORIZATION", "Authorization"],
        ));
        let raw = &bearer["src/api/sky/raw_client.py"];
        assert!(
            raw.contains("\"authorization\": str(authorization)"),
            "{raw}"
        );
        assert!(
            raw.contains("\"AUTHORIZATION\": str(authorization)"),
            "{raw}"
        );
        assert!(!raw.contains("\"Authorization\""), "{raw}");
        let key = files(header_document(
            json!({"tok": {"type": "apiKey", "in": "header", "name": "X-Kite-Key"}}),
            &["X-Kite-Key", "x-kite-key"],
        ));
        let raw = &key["src/api/sky/raw_client.py"];
        assert!(raw.contains("\"x-kite-key\""), "{raw}");
        assert!(!raw.contains("\"X-Kite-Key\": str("), "{raw}");
        // A header argument is no argument to the README's abbreviated calls.
        assert!(
            key["README.md"].contains("client.sky.fly0()"),
            "{}",
            key["README.md"]
        );
    }

    #[test]
    fn a_subset_promoted_string_header_with_a_default_is_a_literal() {
        let mut operations = Vec::new();
        for index in 0..4 {
            let mut parameters = vec![
                json!({"name": "X-Count", "in": "header", "required": false, "schema": {"type": "integer", "default": 3}}),
            ];
            if index < 3 {
                parameters.push(json!({"name": "Dry-Run", "in": "header", "required": false, "schema": {"type": "string", "default": "off"}}));
            } else {
                parameters.clear();
            }
            operations.push((format!("op{index}"), Value::Array(parameters)));
        }
        let borrowed: Vec<(&str, Value)> = operations
            .iter()
            .map(|(name, value)| (name.as_str(), value.clone()))
            .collect();
        let files = files(document("3.0.3", &borrowed));
        let client = &files["src/api/client.py"];
        assert!(
            client.contains("dry_run: typing.Optional[typing.Literal[\"off\"]] = None,"),
            "{client}"
        );
        // A non-string default keeps the plain scalar.
        assert!(
            client.contains("count: typing.Optional[int] = None,"),
            "{client}"
        );
        let wrapper = &files["src/api/core/client_wrapper.py"];
        assert!(
            wrapper.contains("headers[\"Dry-Run\"] = self._dry_run\n"),
            "{wrapper}"
        );
    }

    fn base_path_document(extension: (&str, Value), include: bool) -> Value {
        let prefix = if include { "/{edition}" } else { "" };
        let mut document = json!({
            "openapi": "3.0.3",
            "info": {"title": "Api", "version": "1"},
            "servers": [{"url": "https://maps.example.org"}],
            "paths": {
                format!("{prefix}/regions"): {"post": {
                    "operationId": "createRegion",
                    "tags": ["regions"],
                    "parameters": if include {
                        json!([{"name": "edition", "in": "path", "required": true, "schema": {"type": "string"}}])
                    } else {
                        json!([])
                    },
                    "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Region"}}}},
                    "responses": {"200": {"description": "ok", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Region"}}}}}
                }}
            },
            "components": {"schemas": {"Region": {"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}}}
        });
        document[extension.0] = extension.1;
        document
    }

    #[test]
    fn a_base_path_lifts_its_placeholders_to_the_client() {
        let defaulted = files(base_path_document(
            (
                "x-fern-base-path",
                json!({"path": "/{edition}", "paths-include-base-path": true, "parameters": {"edition": {"type": "string", "default": "v2"}}}),
            ),
            true,
        ));
        let client = &defaulted["src/api/client.py"];
        assert!(
            client.contains("edition: typing.Optional[str] = \"v2\","),
            "{client}"
        );
        assert!(client.contains("        edition=\"v2\",\n"), "{client}");
        let wrapper = &defaulted["src/api/core/client_wrapper.py"];
        assert!(
            wrapper.contains("edition: typing.Optional[str] = None,"),
            "{wrapper}"
        );
        assert!(!wrapper.contains("headers[\"edition\"]"), "{wrapper}");
        let raw = &defaulted["src/api/regions/raw_client.py"];
        assert!(
            raw.contains("f\"{encode_path_param(self._client_wrapper._edition)}/regions\""),
            "{raw}"
        );
        assert!(
            raw.contains("def create_region(\n        self, *, name: str"),
            "{raw}"
        );
        assert!(!raw.contains("content-type"), "{raw}");
        // The Markdown passes it by keyword with its default to the three
        // constructors that name arguments (sync, async, environments), and to
        // no method.
        let readme = &defaulted["README.md"];
        assert!(
            readme.contains("client = ApiApi(\n    edition=\"v2\",\n)"),
            "{readme}"
        );
        assert_eq!(readme.matches("edition=").count(), 3, "{readme}");
        assert!(
            !readme.contains("    edition=\"v2\",\n    name="),
            "{readme}"
        );

        // Without a default the argument is required, and the Markdown constructs
        // the client with it.
        let required = files(base_path_document(
            ("x-crozier-base-path", json!({"path": "/{edition}"})),
            false,
        ));
        assert!(required["src/api/client.py"].contains("        edition: str,\n"));
        assert!(
            required["README.md"].contains("client = ApiApi(\n    edition=\"YOUR_EDITION\",\n)"),
            "{}",
            required["README.md"]
        );
        assert!(required["src/api/regions/raw_client.py"]
            .contains("self._client_wrapper._edition)}/regions"));

        // `x-crozier-base-path` wins over `x-fern-base-path`.
        let mut both = base_path_document(("x-fern-base-path", json!("/v1")), false);
        both["x-crozier-base-path"] = json!("/v2/");
        let both = files(both);
        assert!(
            both["src/api/regions/raw_client.py"].contains("\"v2/regions\""),
            "{}",
            both["src/api/regions/raw_client.py"]
        );
    }

    #[test]
    fn a_map_value_under_a_one_letter_owner_is_named_across_the_join() {
        let files = files(json!({
            "openapi": "3.0.0",
            "info": {"title": "Api", "version": "1"},
            "paths": {},
            "components": {"schemas": {"Dto": {"type": "object", "properties": {
                "f": {"type": "object", "properties": {
                    "w": {"type": "object", "additionalProperties": {
                        "type": "object", "properties": {"id": {"type": "number"}}
                    }}
                }}
            }}}}
        }));
        assert!(
            files
                .keys()
                .any(|path| path.ends_with("types/dto_fw_value.py")),
            "{:?}",
            files.keys()
        );
        assert!(files
            .values()
            .any(|text| text.contains("class DtoFwValue(")));
    }

    /// One `GET /beds` operation (beside a second operation, so no header is
    /// promoted) taking `parameters`.
    fn beds_document(parameters: Value) -> Value {
        json!({
            "openapi": "3.0.3",
            "info": {"title": "Beds", "version": "1"},
            "paths": {
                "/beds": {"get": {
                    "operationId": "listBeds",
                    "tags": ["beds"],
                    "parameters": parameters,
                    "responses": {"204": {"description": "ok"}}
                }},
                "/beds/count": {"get": {
                    "operationId": "countBeds",
                    "tags": ["beds"],
                    "responses": {"204": {"description": "ok"}}
                }}
            }
        })
    }

    #[test]
    fn nullable_array_items_lose_their_optional_in_the_signature_and_docs() {
        let files = files(beds_document(json!([{
            "name": "trays", "in": "query", "required": true,
            "schema": {"type": "array", "items": {"type": "string", "nullable": true}}
        }])));
        let annotation = "typing.Optional[typing.Union[str, typing.Sequence[str]]]";
        let client = &files["src/api/beds/client.py"];
        assert!(
            client.contains(&format!("trays: {annotation} = None,")),
            "{client}"
        );
        // A docstring passes an empty list; the Markdown one non-null element.
        assert!(client.contains("            trays=[],\n"), "{client}");
        let reference = &files["reference.md"];
        assert!(
            reference.contains(&format!("**trays:** `{annotation}`")),
            "{reference}"
        );
        assert!(
            reference.contains("    trays=[\n        \"trays\"\n    ],"),
            "{reference}"
        );
        assert!(!reference.contains("None"), "{reference}");
    }

    #[test]
    fn constant_headers_are_sent_in_order_and_left_out_of_the_docs() {
        let header = |name: &str, required: bool, schema: Value| json!({"name": name, "in": "header", "required": required, "schema": schema});
        let files = files(beds_document(json!([
            header("X-Watts", true, json!({"type": "integer", "default": 40})),
            header("X-Hue", true, json!({"type": "string", "default": "amber"})),
            header(
                "X-Mist",
                false,
                json!({"type": "string", "enum": ["fine", "coarse"], "default": "fine"})
            ),
        ])));
        let raw = &files["src/api/beds/raw_client.py"];
        assert!(
            raw.contains(
                "                \"X-Watts\": str(watts) if watts is not None else None,\n                \"X-Hue\": \"amber\",\n                \"X-Mist\": \"fine\",\n"
            ),
            "{raw}"
        );
        for doc in ["README.md", "reference.md"] {
            let text = &files[doc];
            assert!(text.contains("watts=1"), "{doc}: {text}");
            assert!(
                !text.contains("hue") && !text.contains("mist"),
                "{doc}: {text}"
            );
        }
    }
}

#[cfg(test)]
mod wire_tests {
    //! How a method builds its request and reads its response, measured at
    //! fern-python-sdk 5.41.3, each rule driven through the whole generator.
    use serde_json::{json, Value};

    /// Every file the generator writes for `document` in `layout`, keyed by path.
    fn files_in(
        document: Value,
        layout: crate::settings::Layout,
    ) -> std::collections::BTreeMap<String, String> {
        let doc: crate::openapi::OpenApi =
            serde_json::from_value(document).expect("document deserializes");
        let mut config = crate::config::GenerateConfig::new(
            std::path::PathBuf::from("openapi.yml"),
            std::path::PathBuf::from("out"),
            Some("api".to_string()),
            None,
            None,
            crate::settings::ExtraFields::default(),
            "Api",
        )
        .expect("the config is well formed");
        config.layout = layout;
        super::generate(&crate::ir::build(&doc, &config))
            .expect("the document generates")
            .into_iter()
            .map(|file| {
                (
                    file.path.to_string_lossy().replace('\\', "/"),
                    file.contents,
                )
            })
            .collect()
    }

    fn files(document: Value) -> std::collections::BTreeMap<String, String> {
        files_in(document, crate::settings::Layout::default())
    }

    fn raw_client(files: &std::collections::BTreeMap<String, String>, module: &str) -> String {
        files
            .iter()
            .find(|(path, _)| path.ends_with(&format!("{module}/raw_client.py")))
            .map(|(_, contents)| contents.clone())
            .unwrap_or_else(|| panic!("no {module}/raw_client.py in {:?}", files.keys()))
    }

    #[test]
    fn an_sse_stream_skips_events_with_empty_data() {
        let tick = json!({"type": "object", "properties": {"n": {"type": "integer"}}});
        let generated = files(json!({
            "openapi": "3.0.3",
            "info": {"title": "t", "version": "1"},
            "paths": {"/ticks": {"get": {
                "operationId": "stream", "tags": ["ticks"],
                "responses": {"200": {"description": "ok",
                    "content": {"text/event-stream": {"schema": {"$ref": "#/components/schemas/Tick"}}}}}
            }}},
            "components": {"schemas": {"Tick": tick}}
        }));
        let raw = raw_client(&generated, "ticks");
        let skip = "if _sse.data == None:\n                                    return\n                                if len(_sse.data) == 0:\n                                    continue\n";
        // Once in the sync method, once in the async one.
        assert_eq!(raw.matches(skip).count(), 2, "{raw}");
    }

    #[test]
    fn an_event_dispatched_stream_parses_each_event_as_its_envelope() {
        let event = |name: &str| {
            json!({"type": "object", "required": ["event", "data"], "properties": {
                "event": {"type": "string", "enum": [name]},
                "data": {"type": "object", "properties": {"n": {"type": "integer"}}}
            }})
        };
        let generated = files(json!({
            "openapi": "3.0.3",
            "info": {"title": "t", "version": "1"},
            "paths": {"/ticks": {"get": {
                "operationId": "stream", "tags": ["ticks"],
                "responses": {"200": {"description": "ok",
                    "content": {"text/event-stream": {"schema": {"$ref": "#/components/schemas/Change"}}}}}
            }}},
            "components": {"schemas": {
                "Change": {
                    "oneOf": [
                        {"$ref": "#/components/schemas/Opened"},
                        {"$ref": "#/components/schemas/Closed"}
                    ],
                    "discriminator": {"propertyName": "event"}
                },
                "Opened": event("opened"),
                "Closed": event("closed")
            }}
        }));
        let raw = raw_client(&generated, "ticks");
        for model in ["Opened", "Closed"] {
            let parse = format!(
                "type_={model},\n                                                object_={{\"data\": json.loads(_sse.data)}},"
            );
            assert_eq!(raw.matches(&parse).count(), 2, "{model}: {raw}");
        }
        assert_eq!(raw.matches("if len(_sse.data) == 0:").count(), 2, "{raw}");
    }

    #[test]
    fn a_multipart_list_of_primitives_repeats_its_field_and_other_lists_stay_json() {
        let generated = files(json!({
            "openapi": "3.0.3",
            "info": {"title": "t", "version": "1"},
            "paths": {"/files": {"post": {
                "operationId": "upload", "tags": ["files"],
                "requestBody": {"content": {"multipart/form-data": {"schema": {
                    "type": "object",
                    "required": ["file", "labels"],
                    "properties": {
                        "file": {"type": "string", "format": "binary"},
                        "labels": {"type": "array", "items": {"type": "string"}},
                        "read": {"type": "array", "items": {"type": "string"}},
                        "kinds": {"type": "array", "items": {"$ref": "#/components/schemas/Kind"}},
                        "ids": {"type": "array", "items": {"type": "string", "format": "uuid"}},
                        "points": {"type": "array", "items": {"type": "object",
                            "properties": {"x": {"type": "integer"}}}}
                    }
                }}}},
                "responses": {"204": {"description": "ok"}}
            }}},
            "components": {"schemas": {"Kind": {"type": "string", "enum": ["a", "b"]}}}
        }));
        let raw = raw_client(&generated, "files");
        assert!(
            raw.contains("\"labels\": jsonable_encoder(labels),\n"),
            "{raw}"
        );
        for name in ["read", "kinds"] {
            assert!(
                raw.contains(&format!(
                    "\"{name}\": jsonable_encoder({name}) if {name} is not OMIT else OMIT,\n"
                )),
                "{name}: {raw}"
            );
        }
        for name in ["ids", "points"] {
            assert!(
                raw.contains(&format!(
                    "\"{name}\": json.dumps(jsonable_encoder({name})) if {name} is not OMIT else OMIT,\n"
                )),
                "{name}: {raw}"
            );
        }
    }

    /// A `PATCH /folders/{id}` whose `$ref` body declares an `id` of its own.
    fn folder_document(body_also_returned: bool, read_only_id: bool) -> Value {
        let mut id = json!({"type": "string"});
        if read_only_id {
            id["readOnly"] = json!(true);
        }
        let response = if body_also_returned {
            json!({"200": {"description": "ok", "content": {"application/json": {
                "schema": {"$ref": "#/components/schemas/Folder"}}}}})
        } else {
            json!({"204": {"description": "ok"}})
        };
        json!({
            "openapi": "3.0.3",
            "info": {"title": "t", "version": "1"},
            "paths": {"/folders/{id}": {"patch": {
                "operationId": "update", "tags": ["folders"],
                "parameters": [{"name": "id", "in": "path", "required": true,
                    "schema": {"type": "string"}}],
                "requestBody": {"required": true, "content": {"application/json": {
                    "schema": {"$ref": "#/components/schemas/Folder"}}}},
                "responses": response
            }}},
            "components": {"schemas": {"Folder": {"type": "object", "properties": {
                "id": id, "name": {"type": "string"}}}}}
        })
    }

    #[test]
    fn a_path_parameter_a_request_only_body_property_claims_is_renamed() {
        // A body schema only a request references claims its property names,
        // `readOnly` ones included, so the path parameter becomes `id_path_param`.
        for read_only in [true, false] {
            let raw = raw_client(&files(folder_document(false, read_only)), "folders");
            assert!(raw.contains("id_path_param: str,"), "{raw}");
            assert!(
                raw.contains("f\"folders/{encode_path_param(id_path_param)}\""),
                "{raw}"
            );
        }
        // A body schema a response also returns claims only `body`: the
        // parameter keeps its name, and the generator's own collision rule
        // suffixes it when the body sends an `id` argument too.
        let raw = raw_client(&files(folder_document(true, false)), "folders");
        assert!(raw.contains("id_: str,"), "{raw}");
        assert!(!raw.contains("id_path_param"), "{raw}");
        // So does one that is the body of a second operation.
        let mut shared = folder_document(false, false);
        shared["paths"]["/folders"] = json!({"post": {
            "operationId": "create", "tags": ["folders"],
            "requestBody": {"required": true, "content": {"application/json": {
                "schema": {"$ref": "#/components/schemas/Folder"}}}},
            "responses": {"204": {"description": "ok"}}
        }});
        let raw = raw_client(&files(shared), "folders");
        assert!(raw.contains("id_: str,"), "{raw}");
        assert!(!raw.contains("id_path_param"), "{raw}");
    }

    #[test]
    fn a_path_parameter_a_query_parameter_shares_a_name_with_is_renamed() {
        let generated = files(json!({
            "openapi": "3.0.3",
            "info": {"title": "t", "version": "1"},
            "paths": {"/items/{kind}": {"get": {
                "operationId": "list", "tags": ["items"],
                "parameters": [
                    {"name": "kind", "in": "path", "required": true, "schema": {"type": "string"}},
                    {"name": "kind", "in": "query", "schema": {"type": "string"}}
                ],
                "responses": {"204": {"description": "ok"}}
            }}}
        }));
        let raw = raw_client(&generated, "items");
        assert!(raw.contains("kind_path_param: str,"), "{raw}");
    }

    #[test]
    fn the_flat_layout_pages_like_the_packaged_one() {
        let page = json!({"type": "object", "properties": {
            "items": {"type": "array", "items": {"type": "string"}},
            "next": {"type": "string"}}});
        let generated = files_in(
            json!({
                "openapi": "3.0.3",
                "info": {"title": "t", "version": "1"},
                "paths": {"/items": {"get": {
                    "operationId": "list", "tags": ["items"],
                    "parameters": [{"name": "cursor", "in": "query", "schema": {"type": "string"}}],
                    "x-fern-pagination": {"cursor": "$request.cursor",
                        "next_cursor": "$response.next", "results": "$response.items"},
                    "responses": {"200": {"description": "ok", "content": {"application/json": {
                        "schema": {"$ref": "#/components/schemas/Page"}}}}}
                }}},
                "components": {"schemas": {"Page": page}}
            }),
            crate::settings::Layout::Flat,
        );
        let raw = raw_client(&generated, "items");
        assert!(raw.contains("SyncPager["), "{raw}");
    }
}