openapi-nexus 0.2.3

OpenAPI 3.x multi-language code generator
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
use std::collections::{BTreeMap, HashSet};

use crate::codegen::traits::file_writer::FileInfo;
use crate::generators::multipart::{MultipartValueEncoding, multipart_parts_for_request_body};
use crate::generators::request_inputs::{RequestInputPlan, request_input_for_operation};
use crate::generators::response_headers::{
    ResponseHeaderPlan, ResponseHeaderValueKind, collect_response_headers,
    unique_response_header_accessor_names,
};
use crate::generators::response_names::{
    response_entry_name as response_variant_name, response_match_rank,
};
use crate::ir::types::{
    IrOperation, IrParameter, IrRequestBody, IrResponse, IrSpec, IrTypeExpr, ParameterLocation,
};
use heck::{ToLowerCamelCase, ToPascalCase};
use sigil_stitch::lang::java::Java;
use sigil_stitch::prelude::*;

use super::util::{
    build_java_getter, java_boxed_type_str, java_field_name, java_ident, java_type_str,
    render_value_as_string, sanitize_operation_id, unique_name,
};

const RENDER_WIDTH: usize = 100;

pub fn generate_api_files(
    ir: &IrSpec,
    package_name: &str,
    header: &str,
    request_inputs: &RequestInputPlan,
) -> Result<Vec<FileInfo>, String> {
    let by_tag = group_by_tag(&ir.operations);
    let mut files = Vec::with_capacity(by_tag.len());
    let mut support_files_seen = HashSet::new();
    let has_models = has_java_models(ir, request_inputs);
    for (tag, ops) in &by_tag {
        let class_name = format!("{}Api", tag.to_pascal_case());
        let filename = format!("{class_name}.java");
        let body = emit_api_file(tag, ops, ir, package_name, request_inputs, has_models);
        let content = format!("{header}{body}");
        files.push(FileInfo::api(filename, content));
        for op in ops {
            let plan = plan_operation(op, ir, request_inputs);
            for file in operation_support_files(&plan, package_name, header, has_models) {
                if support_files_seen.insert(file.filename.clone()) {
                    files.push(file);
                }
            }
        }
    }
    Ok(files)
}

fn group_by_tag(operations: &[IrOperation]) -> BTreeMap<String, Vec<&IrOperation>> {
    let mut out: BTreeMap<String, Vec<&IrOperation>> = BTreeMap::new();
    for op in operations {
        let tags: Vec<String> = if op.tags.is_empty() {
            vec!["default".to_string()]
        } else {
            op.tags.clone()
        };
        for tag in tags {
            out.entry(tag).or_default().push(op);
        }
    }
    out
}

fn has_java_models(ir: &IrSpec, request_inputs: &RequestInputPlan) -> bool {
    !ir.schemas.is_empty() || !request_inputs.models().is_empty()
}

// ---------------------------------------------------------------------------
// File assembly
// ---------------------------------------------------------------------------

fn emit_api_file(
    tag: &str,
    ops: &[&IrOperation],
    ir: &IrSpec,
    package_name: &str,
    request_inputs: &RequestInputPlan,
    has_models: bool,
) -> String {
    let class_name = format!("{}Api", tag.to_pascal_case());
    let plans: Vec<OpPlan> = ops
        .iter()
        .map(|op| plan_operation(op, ir, request_inputs))
        .collect();

    let filename = format!("{class_name}.java");
    let mut fb = FileSpec::builder_with(&filename, Java::new())
        .header(package_header(package_name))
        .add_import(ImportSpec::named(
            &format!("{package_name}.runtime"),
            "ApiClient",
        ))
        .add_import(ImportSpec::named(
            &format!("{package_name}.runtime"),
            "ApiException",
        ))
        .add_import(ImportSpec::named("com.google.gson", "Gson"))
        .add_import(ImportSpec::named("com.google.gson.reflect", "TypeToken"))
        .add_import(ImportSpec::named("java.io", "IOException"))
        .add_import(ImportSpec::named("java.nio.charset", "StandardCharsets"))
        .add_import(ImportSpec::named("java.util", "HashMap"))
        .add_import(ImportSpec::named("java.util", "List"))
        .add_import(ImportSpec::named("java.util", "Map"))
        .add_import(ImportSpec::named("java.util.stream", "Collectors"))
        .add_import(ImportSpec::named("okhttp3", "Request"))
        .add_import(ImportSpec::named("okhttp3", "Response"));
    if has_models {
        fb = fb.add_import(ImportSpec::named(&format!("{package_name}.models"), "*"));
    }
    let has_supported_multipart_body = plans.iter().any(|plan| {
        plan.body.as_ref().is_some_and(|body| {
            media_type_base(&body.media_type) == "multipart/form-data"
                && body.multipart_parts.is_some()
        })
    });
    let has_raw_request_body = plans.iter().any(|plan| plan.body.is_some());
    if has_supported_multipart_body {
        fb = fb.add_import(ImportSpec::named("okhttp3", "MultipartBody"));
    }
    if has_raw_request_body {
        fb = fb.add_import(ImportSpec::named("okhttp3", "RequestBody"));
    }
    if has_supported_multipart_body || has_raw_request_body {
        fb = fb.add_import(ImportSpec::named("okhttp3", "MediaType"));
    }

    // API class
    let mut cls = TypeSpec::builder(&class_name, TypeKind::Class).visibility(Visibility::Public);
    cls = cls.doc(&format!(
        "{class_name} groups operations under the {tag} tag."
    ));

    // Fields
    cls = cls.add_field(
        FieldSpec::builder("client", TypeName::primitive("ApiClient"))
            .visibility(Visibility::Private)
            .is_readonly()
            .build()
            .expect("client field"),
    );
    cls = cls.add_field(
        FieldSpec::builder("gson", TypeName::primitive("Gson"))
            .visibility(Visibility::Private)
            .is_readonly()
            .initializer(CodeBlock::of("new Gson()", ()).expect("gson init"))
            .build()
            .expect("gson field"),
    );

    // Constructor
    let mut ctor = FunSpec::builder(&class_name);
    ctor = ctor.visibility(Visibility::Public);
    ctor = ctor.add_param(
        ParameterSpec::new("ApiClient client", TypeName::primitive("")).expect("client param"),
    );
    let ctor_body = sigil_quote!(Java {
        this.client = client;
    })
    .expect("ctor body");
    ctor = ctor.body(ctor_body);
    cls = cls.add_method(ctor.build().expect("constructor"));

    // API methods
    for plan in &plans {
        cls = cls.add_method(build_operation_fun(plan));
    }

    fb = fb.add_type(cls.build().expect("API class builds"));

    let file = fb.build().expect("FileSpec builds for API file");
    file.render(RENDER_WIDTH)
        .expect("FileSpec renders for API file")
}

fn package_header(package_name: &str) -> CodeBlock {
    sigil_quote!(Java {
        package $L(format!("{package_name}.apis"));
    })
    .expect("package header builds")
}

fn operation_support_files(
    plan: &OpPlan<'_>,
    package_name: &str,
    header: &str,
    has_models: bool,
) -> Vec<FileInfo> {
    let mut files = Vec::new();
    let mut response_imports = vec![
        ImportSpec::named("java.util", "List"),
        ImportSpec::named("java.util", "Map"),
        ImportSpec::named("okhttp3", "Response"),
    ];
    if has_models {
        response_imports.push(ImportSpec::named(&format!("{package_name}.models"), "*"));
    }
    files.push(java_type_file(
        &plan.response_type,
        package_name,
        header,
        response_imports,
        build_response_class(plan),
    ));

    let detail_interface = format!("{}Detail", plan.error_type);
    let mut detail_impl_names = Vec::new();
    let mut detail_seen = HashSet::new();
    for response in &plan.error_responses {
        if !detail_seen.insert(response.field_name.clone()) {
            continue;
        }
        detail_impl_names.push(format!(
            "{}{}",
            plan.method_name.to_pascal_case(),
            response.field_name
        ));
    }
    let unexpected = format!("{}Unexpected", plan.method_name.to_pascal_case());
    detail_impl_names.push(unexpected.clone());
    files.push(java_code_file(
        &detail_interface,
        package_name,
        header,
        Vec::new(),
        java_error_detail_interface(&detail_interface, &detail_impl_names),
    ));

    files.push(java_code_file(
        &plan.error_type,
        package_name,
        header,
        vec![
            ImportSpec::named(&format!("{package_name}.runtime"), "ApiException"),
            ImportSpec::named("okhttp3", "Headers"),
        ],
        java_error_exception_class(&plan.error_type, &detail_interface, &plan.error_headers),
    ));

    let mut seen = HashSet::new();
    for response in &plan.error_responses {
        if !seen.insert(response.field_name.clone()) {
            continue;
        }
        let class_name = format!(
            "{}{}",
            plan.method_name.to_pascal_case(),
            response.field_name
        );
        files.push(java_code_file(
            &class_name,
            package_name,
            header,
            error_detail_imports(package_name, has_models),
            java_error_detail_class(&class_name, &detail_interface, response),
        ));
    }

    files.push(java_code_file(
        &unexpected,
        package_name,
        header,
        Vec::new(),
        java_unexpected_detail_class(&unexpected, &detail_interface),
    ));
    files
}

fn java_type_file(
    class_name: &str,
    package_name: &str,
    header: &str,
    imports: Vec<ImportSpec>,
    type_spec: TypeSpec,
) -> FileInfo {
    let filename = format!("{class_name}.java");
    let mut fb =
        FileSpec::builder_with(&filename, Java::new()).header(package_header(package_name));
    for import in imports {
        fb = fb.add_import(import);
    }
    fb = fb.add_type(type_spec);
    let file = fb.build().expect("Java support type file builds");
    FileInfo::api(
        filename,
        format!(
            "{header}{}",
            file.render(RENDER_WIDTH)
                .expect("Java support type file renders")
        ),
    )
}

fn java_code_file(
    class_name: &str,
    package_name: &str,
    header: &str,
    imports: Vec<ImportSpec>,
    code: CodeBlock,
) -> FileInfo {
    let filename = format!("{class_name}.java");
    let mut fb =
        FileSpec::builder_with(&filename, Java::new()).header(package_header(package_name));
    for import in imports {
        fb = fb.add_import(import);
    }
    fb = fb.add_code(code);
    let file = fb.build().expect("Java support code file builds");
    FileInfo::api(
        filename,
        format!(
            "{header}{}",
            file.render(RENDER_WIDTH)
                .expect("Java support code file renders")
        ),
    )
}

fn error_detail_imports(package_name: &str, has_models: bool) -> Vec<ImportSpec> {
    let mut imports = vec![
        ImportSpec::named("com.google.gson", "Gson"),
        ImportSpec::named("com.google.gson.reflect", "TypeToken"),
        ImportSpec::named("java.nio.charset", "StandardCharsets"),
        ImportSpec::named("java.util", "List"),
        ImportSpec::named("java.util", "Map"),
    ];
    if has_models {
        imports.push(ImportSpec::named(&format!("{package_name}.models"), "*"));
    }
    imports
}

// ---------------------------------------------------------------------------
// Response class
// ---------------------------------------------------------------------------

fn build_response_class(plan: &OpPlan<'_>) -> TypeSpec {
    let mut tb =
        TypeSpec::builder(&plan.response_type, TypeKind::Struct).visibility(Visibility::Public);
    tb = tb.doc(&format!(
        "{} carries the response from {}.",
        plan.response_type, plan.method_name
    ));

    // Fields
    tb = tb.add_field(
        FieldSpec::builder("statusCode", TypeName::primitive("int"))
            .visibility(Visibility::Private)
            .is_readonly()
            .build()
            .expect("field"),
    );
    tb = tb.add_field(
        FieldSpec::builder("raw", TypeName::primitive("Response"))
            .visibility(Visibility::Private)
            .is_readonly()
            .build()
            .expect("field"),
    );

    let mut seen: HashSet<String> = HashSet::new();
    for tr in &plan.typed_responses {
        if !seen.insert(tr.field_name.clone()) {
            continue;
        }
        tb = tb.add_field(
            FieldSpec::builder(&tr.field_name, TypeName::primitive(&tr.java_type))
                .visibility(Visibility::Private)
                .is_readonly()
                .build()
                .expect("field"),
        );
    }

    // Constructor
    let mut ctor = FunSpec::builder(&plan.response_type);
    ctor = ctor.visibility(Visibility::Public);
    ctor = ctor
        .add_param(ParameterSpec::new("int statusCode", TypeName::primitive("")).expect("param"));
    ctor =
        ctor.add_param(ParameterSpec::new("Response raw", TypeName::primitive("")).expect("param"));
    let mut ctor_seen: HashSet<String> = HashSet::new();
    for tr in &plan.typed_responses {
        if !ctor_seen.insert(tr.field_name.clone()) {
            continue;
        }
        ctor = ctor.add_param(
            ParameterSpec::new(
                &format!("{} {}", tr.java_type, tr.field_name),
                TypeName::primitive(""),
            )
            .expect("param"),
        );
    }
    let mut assignment_fields = Vec::new();
    let mut body_seen: HashSet<String> = HashSet::new();
    for tr in &plan.typed_responses {
        if !body_seen.insert(tr.field_name.clone()) {
            continue;
        }
        assignment_fields.push(tr.field_name.clone());
    }
    let ctor_body = sigil_quote!(Java {
        this.statusCode = statusCode;
        this.raw = raw;
        $for(field_name in &assignment_fields) {
            this.$L(field_name.as_str()) = $L(field_name.as_str());
        }
    })
    .expect("ctor body");
    ctor = ctor.body(ctor_body);
    tb = tb.add_method(ctor.build().expect("response ctor"));

    // Getters
    tb = tb.add_method(build_java_getter("getStatusCode", "int", "statusCode"));
    tb = tb.add_method(build_java_getter("getRaw", "Response", "raw"));

    let mut getter_seen: HashSet<String> = HashSet::new();
    for tr in &plan.typed_responses {
        if !getter_seen.insert(tr.field_name.clone()) {
            continue;
        }
        let getter_name = format!("get{}", tr.field_name.to_pascal_case());
        tb = tb.add_method(build_java_getter(
            &getter_name,
            &tr.java_type,
            &tr.field_name,
        ));
    }
    let header_method_names =
        unique_response_header_accessor_names(&plan.success_headers, |wire_name| {
            format!("get{}Header", wire_name.to_pascal_case())
        });
    for (header, method_name) in plan.success_headers.iter().zip(header_method_names) {
        tb = tb.add_method(build_java_header_getter(
            header,
            &method_name,
            "this.raw.header",
        ));
    }

    tb.build().expect("response class builds")
}

fn build_java_header_getter(
    header: &ResponseHeaderPlan,
    method_name: &str,
    lookup: &str,
) -> FunSpec {
    let return_type = java_header_type(header.value_kind);
    FunSpec::builder(method_name)
        .visibility(Visibility::Public)
        .returns(TypeName::primitive(return_type))
        .body(java_header_accessor_body(
            header.value_kind,
            lookup,
            &header.wire_name,
        ))
        .build()
        .expect("Java header getter builds")
}

fn java_error_detail_interface(detail_interface: &str, permits: &[String]) -> CodeBlock {
    let permits_clause = permits.join(", ");
    CodeBlock::of(
        &format!(
            "public sealed interface {detail_interface} permits {permits_clause} {{\n    int statusCode();\n    okhttp3.Headers headers();\n    byte[] rawBody();\n}}\n"
        ),
        (),
    )
    .expect("Java error detail interface builds")
}

fn java_error_exception_class(
    error_type: &str,
    detail_interface: &str,
    headers: &[ResponseHeaderPlan],
) -> CodeBlock {
    let mut cb = CodeBlock::builder();
    cb.add(
        &format!(
            "public final class {error_type} extends ApiException {{\n    private final {detail_interface} detail;\n    private final Headers headers;\n    private final byte[] rawBody;\n\n    public {error_type}(int statusCode, String status, String body, Headers headers, byte[] rawBody, {detail_interface} detail) {{\n        super(statusCode, status, body);\n        this.headers = headers;\n        this.rawBody = rawBody.clone();\n        this.detail = detail;\n    }}\n\n    public {detail_interface} detail() {{\n        return this.detail;\n    }}\n\n    public Headers headers() {{\n        return this.headers;\n    }}\n\n    public byte[] rawBody() {{\n        return this.rawBody.clone();\n    }}\n"
        ),
        (),
    );
    cb.add("%>", ());
    let method_names = unique_response_header_accessor_names(headers, |wire_name| {
        format!("get{}Header", wire_name.to_pascal_case())
    });
    for (header, method_name) in headers.iter().zip(method_names) {
        cb.add_line();
        cb.add_code(
            build_java_header_getter(header, &method_name, "this.headers.get")
                .emit(&Java::new(), DeclarationContext::Member)
                .expect("Java error header getter emits"),
        );
    }
    cb.add("%<}\n", ());
    cb.build().expect("Java error exception class builds")
}

fn java_header_type(kind: ResponseHeaderValueKind) -> &'static str {
    match kind {
        ResponseHeaderValueKind::String => "String",
        ResponseHeaderValueKind::Integer => "Long",
        ResponseHeaderValueKind::Number => "Double",
        ResponseHeaderValueKind::Boolean => "Boolean",
    }
}

fn java_header_accessor_body(
    kind: ResponseHeaderValueKind,
    lookup: &str,
    wire_name: &str,
) -> CodeBlock {
    let integer_pattern = r"[+-]?[0-9]+";
    let number_pattern = r"[+-]?(?:[0-9]+(?:[.][0-9]*)?|[.][0-9]+)(?:[eE][+-]?[0-9]+)?";
    match kind {
        ResponseHeaderValueKind::String => sigil_quote!(Java {
            return $L(lookup)($S(wire_name));
        }),
        ResponseHeaderValueKind::Integer => sigil_quote!(Java {
            String value = $L(lookup)($S(wire_name));
            if (value == null || !value.matches($S(integer_pattern))) {
                return null;
            }
            try {
                return Long.valueOf(value);
            } catch (NumberFormatException ignored) {
                return null;
            }
        }),
        ResponseHeaderValueKind::Number => sigil_quote!(Java {
            String value = $L(lookup)($S(wire_name));
            if (value == null || !value.matches($S(number_pattern))) {
                return null;
            }
            try {
                Double parsed = Double.valueOf(value);
                return Double.isFinite(parsed) ? parsed : null;
            } catch (NumberFormatException ignored) {
                return null;
            }
        }),
        ResponseHeaderValueKind::Boolean => sigil_quote!(Java {
            String value = $L(lookup)($S(wire_name));
            if ($S("true").equals(value)) {
                return true;
            }
            if ($S("false").equals(value)) {
                return false;
            }
            return null;
        }),
    }
    .expect("Java header getter body builds")
}

fn java_unexpected_detail_class(unexpected: &str, detail_interface: &str) -> CodeBlock {
    CodeBlock::of(
        &format!(
            "public final class {unexpected} implements {detail_interface} {{\n    private final int statusCode;\n    private final okhttp3.Headers headers;\n    private final byte[] rawBody;\n\n    public {unexpected}(int statusCode, okhttp3.Headers headers, byte[] rawBody) {{\n        this.statusCode = statusCode;\n        this.headers = headers;\n        this.rawBody = rawBody.clone();\n    }}\n\n    public int statusCode() {{ return this.statusCode; }}\n    public okhttp3.Headers headers() {{ return this.headers; }}\n    public byte[] rawBody() {{ return this.rawBody.clone(); }}\n    public byte[] body() {{ return this.rawBody.clone(); }}\n}}\n"
        ),
        (),
    )
    .expect("Java unexpected detail class builds")
}

fn java_error_detail_class(
    class_name: &str,
    detail_interface: &str,
    response: &TypedResponse,
) -> CodeBlock {
    let body_method = java_error_body_method(response);
    let text_helper = java_error_text_helper(response);
    let block = format!(
        "public final class {class_name} implements {detail_interface} {{\n    private final int statusCode;\n    private final okhttp3.Headers headers;\n    private final byte[] rawBody;\n\n    public {class_name}(int statusCode, okhttp3.Headers headers, byte[] rawBody) {{\n        this.statusCode = statusCode;\n        this.headers = headers;\n        this.rawBody = rawBody.clone();\n    }}\n\n    public int statusCode() {{ return this.statusCode; }}\n    public okhttp3.Headers headers() {{ return this.headers; }}\n    public byte[] rawBody() {{ return this.rawBody.clone(); }}\n{text_helper}{body_method}\n}}\n"
    );
    CodeBlock::of(&block, ()).expect("Java error detail class builds")
}

fn java_error_text_helper(response: &TypedResponse) -> &'static str {
    match response.decoding {
        ResponseDecoding::Json | ResponseDecoding::Text => {
            "\n    private String textBody() {\n        String contentTypeHeader = this.headers.get(\"Content-Type\");\n        okhttp3.MediaType contentType = contentTypeHeader != null ? okhttp3.MediaType.parse(contentTypeHeader) : null;\n        java.nio.charset.Charset charset = contentType != null ? contentType.charset(StandardCharsets.UTF_8) : StandardCharsets.UTF_8;\n        return new String(this.rawBody, charset);\n    }\n"
        }
        ResponseDecoding::Bytes => "",
    }
}

fn java_error_body_method(response: &TypedResponse) -> String {
    match response.decoding {
        ResponseDecoding::Json => format!(
            "    public {} body() {{\n        String text = textBody();\n        return new Gson().fromJson(text.isEmpty() ? \"null\" : text, new TypeToken<{}>() {{}}.getType());\n    }}",
            response.java_type, response.java_type
        ),
        ResponseDecoding::Text => {
            "    public String body() {\n        return textBody();\n    }".to_string()
        }
        ResponseDecoding::Bytes => {
            "    public byte[] body() {\n        return this.rawBody.clone();\n    }".to_string()
        }
    }
}

// ---------------------------------------------------------------------------
// Operation method
// ---------------------------------------------------------------------------

fn build_operation_fun(plan: &OpPlan<'_>) -> FunSpec {
    let mut fb = FunSpec::builder(&plan.method_name);
    fb = fb.visibility(Visibility::Public);

    if let Some(summary) = &plan.op.summary {
        fb = fb.doc(summary);
    } else {
        fb = fb.doc(&format!(
            "{} {} {}.",
            plan.method_name,
            plan.op.method.to_uppercase(),
            plan.op.path,
        ));
    }

    // Parameters
    for p in plan
        .path_params
        .iter()
        .chain(&plan.query_params)
        .chain(&plan.header_params)
    {
        fb = fb.add_param(
            ParameterSpec::new(
                &format!("{} {}", p.java_type, p.var_name),
                TypeName::primitive(""),
            )
            .expect("param"),
        );
    }
    if let Some(body) = &plan.body {
        fb = fb.add_param(
            ParameterSpec::new(
                &format!("{} {}", body.java_type, body.var_name),
                TypeName::primitive(""),
            )
            .expect("body param"),
        );
    }

    fb = fb.returns(TypeName::primitive(&plan.response_type));
    fb = fb.suffix("throws IOException");
    fb = fb.body(emit_method_body(plan));

    fb.build().expect("operation FunSpec builds")
}

// ---------------------------------------------------------------------------
// Method body
// ---------------------------------------------------------------------------

fn emit_method_body(plan: &OpPlan<'_>) -> CodeBlock {
    let mut cb = CodeBlock::builder();

    // Path
    let mut path_expr = format!("\"{}\"", plan.op.path);
    for p in &plan.path_params {
        let placeholder = format!("{{{}}}", p.param.name);
        let stringified = render_value_as_string(&p.var_name, &p.param.type_expr);
        path_expr = format!("{path_expr}.replace(\"{placeholder}\", {stringified})");
    }
    cb.add_statement(&format!("String path = {path_expr}"), ());

    // Query
    let has_query = !plan.query_params.is_empty();
    if has_query {
        cb.add_statement("Map<String, String> query = new HashMap<>()", ());
        for p in &plan.query_params {
            let stringified = render_value_as_string(&p.var_name, &p.param.type_expr);
            cb.add_code(java_query_param_put(
                p.param.required,
                &p.var_name,
                &p.param.name,
                &stringified,
            ));
        }
    }

    // Build request
    let method = plan.op.method.to_uppercase();
    if let Some(body) = &plan.body {
        if let Some(message) = unsupported_request_body_message(body) {
            cb.add_code(
                sigil_quote!(Java {
                    throw new IllegalArgumentException($S(message));
                })
                .expect("unsupported request body builds"),
            );
            return cb.build().expect("method body builds");
        }
        cb.add_statement("Request request", ());
        if body.encoding == BodyEncoding::Multipart {
            if let Some(parts) = &body.multipart_parts {
                emit_multipart_body(&mut cb, body, parts);
                cb.add_code(java_new_request_with_body(
                    &method,
                    has_query,
                    "multipartBody",
                ));
            } else {
                cb.add_statement(
                    "throw new IllegalArgumentException(\"unsupported multipart request body: schema must be object-shaped\")",
                    (),
                );
            }
        } else {
            emit_request_body(&mut cb, body);
            cb.add_code(java_new_request_with_body(
                &method,
                has_query,
                "requestBody",
            ));
        }
    } else {
        cb.add_code(java_new_request(&method, has_query));
    }

    // Headers
    for p in &plan.header_params {
        let stringified = render_value_as_string(&p.var_name, &p.param.type_expr);
        cb.add_code(java_header_param_set(
            p.param.required,
            &p.var_name,
            &p.param.name,
            &stringified,
        ));
    }

    // Execute
    cb.add_statement("Response response = client.execute(request)", ());
    cb.add_line();
    cb.add_code(java_error_throw(plan));

    // Response parsing
    if !plan.typed_responses.is_empty() {
        cb.add_statement(
            "byte[] responseBytes = response.body() != null ? response.body().bytes() : new byte[0]",
            (),
        );
        cb.add_statement(
            "String responseText = new String(responseBytes, StandardCharsets.UTF_8)",
            (),
        );
        let mut seen: HashSet<String> = HashSet::new();

        // Numeric status codes
        for tr in &plan.typed_responses {
            if !seen.insert(tr.field_name.clone()) {
                continue;
            }
            cb.add_statement(&format!("{} {} = null", tr.java_type, tr.field_name), ());
        }
        cb.add_code(java_response_decode_assignments(&plan.typed_responses));

        // Return with typed fields
        let args: Vec<String> = std::iter::once("response.code()".to_string())
            .chain(std::iter::once("response".to_string()))
            .chain(plan.typed_responses.iter().map(|tr| tr.field_name.clone()))
            .collect();
        // deduplicate
        let mut dedup_args: Vec<String> = Vec::new();
        let mut args_seen: HashSet<String> = HashSet::new();
        for a in args {
            if args_seen.insert(a.clone()) {
                dedup_args.push(a);
            }
        }
        cb.add_code(
            sigil_quote!(Java {
                return new $N(plan.response_type.as_str())($for(arg in &dedup_args; separator = ", ") { $L(arg.as_str()) });
            })
            .expect("typed response constructor return"),
        );
    } else {
        cb.add_statement(
            &format!(
                "return new {}(response.code(), response)",
                plan.response_type
            ),
            (),
        );
    }

    cb.build().expect("method body builds")
}

fn unsupported_request_body_message(body: &BodyBinding) -> Option<String> {
    if body.encoding == BodyEncoding::Multipart && body.multipart_parts.is_none() {
        return Some(
            "unsupported multipart request body: schema must be object-shaped".to_string(),
        );
    }
    match body.encoding {
        BodyEncoding::FormUrlEncoded | BodyEncoding::Xml | BodyEncoding::Other => Some(format!(
            "unsupported request body media type: {}",
            body.media_type
        )),
        _ => None,
    }
}

fn java_new_request(method: &str, has_query: bool) -> CodeBlock {
    let with_query =
        format!("Request request = client.newRequest(\"{method}\", path, query, null);");
    let without_query =
        format!("Request request = client.newRequest(\"{method}\", path, null, null);");
    sigil_quote!(Java {
        $if(has_query) {
            $L(with_query.as_str())
        } $else {
            $L(without_query.as_str())
        }
    })
    .expect("Java request construction builds")
}

fn java_new_request_with_body(method: &str, has_query: bool, body_expr: &str) -> CodeBlock {
    let with_query =
        format!("request = client.newRequestWithBody(\"{method}\", path, query, {body_expr});");
    let without_query =
        format!("request = client.newRequestWithBody(\"{method}\", path, null, {body_expr});");
    sigil_quote!(Java {
        $if(has_query) {
            $L(with_query.as_str())
        } $else {
            $L(without_query.as_str())
        }
    })
    .expect("Java request body construction builds")
}

fn java_query_param_put(
    required: bool,
    var_name: &str,
    param_name: &str,
    value_expr: &str,
) -> CodeBlock {
    sigil_quote!(Java {
        $if(required) {
            query.put($S(param_name), $L(value_expr));
        } $else {
            if ($L(var_name) != null) {
                query.put($S(param_name), $L(value_expr));
            }
        }
    })
    .expect("Java query param put builds")
}

fn java_header_param_set(
    required: bool,
    var_name: &str,
    param_name: &str,
    value_expr: &str,
) -> CodeBlock {
    sigil_quote!(Java {
        $if(required) {
            request = request.newBuilder().header($S(param_name), $L(value_expr)).build();
        } $else {
            if ($L(var_name) != null) {
                request = request.newBuilder().header($S(param_name), $L(value_expr)).build();
            }
        }
    })
    .expect("Java header param set builds")
}

fn emit_multipart_body(
    cb: &mut sigil_stitch::code_block::CodeBlockBuilder,
    body: &BodyBinding,
    parts: &[MultipartPart],
) {
    if !body.required {
        cb.add_code(
            sigil_quote!(Java {
                RequestBody multipartBody = RequestBody.create(new byte[0], null);
            })
            .expect("default multipart body builds"),
        );
        cb.begin_control_flow(&format!("if ({} != null)", body.var_name), ());
    }
    cb.add_code(
        sigil_quote!(Java {
            MultipartBody.Builder multipartBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        })
        .expect("multipart builder builds"),
    );
    for part in parts {
        let access = format!(
            "{}.get{}()",
            body.var_name,
            part.field_name.to_pascal_case()
        );
        if part.required {
            emit_required_multipart_part(cb, part, &access);
        } else {
            cb.begin_control_flow(&format!("if ({access} != null)"), ());
            emit_required_multipart_part(cb, part, &access);
            cb.end_control_flow();
        }
    }
    cb.add_code(java_multipart_body_finish(body.required));
    if !body.required {
        cb.end_control_flow();
    }
}

fn java_multipart_body_finish(body_required: bool) -> CodeBlock {
    sigil_quote!(Java {
        $if(body_required) {
            RequestBody multipartBody = multipartBuilder.build();
        } $else {
            multipartBody = multipartBuilder.build();
        }
    })
    .expect("multipart body finish builds")
}

fn emit_request_body(cb: &mut sigil_stitch::code_block::CodeBlockBuilder, body: &BodyBinding) {
    if !body.required {
        cb.add_code(
            sigil_quote!(Java {
                RequestBody requestBody = RequestBody.create(new byte[0], null);
            })
            .expect("default request body builds"),
        );
        cb.begin_control_flow(&format!("if ({} != null)", body.var_name), ());
    }
    match body.encoding {
        BodyEncoding::Json => {
            let body_var = body.var_name.as_str();
            let media_type = body.media_type.as_str();
            cb.add_code(java_json_request_body(body.required, body_var, media_type));
        }
        BodyEncoding::TextPlain | BodyEncoding::OctetStream => {
            let body_var = body.var_name.as_str();
            let media_type = body.media_type.as_str();
            cb.add_code(java_raw_request_body(body.required, body_var, media_type));
        }
        BodyEncoding::FormUrlEncoded | BodyEncoding::Xml | BodyEncoding::Other => {
            let message = format!("unsupported request body media type: {}", body.media_type);
            cb.add_code(
                sigil_quote!(Java {
                    throw new IllegalArgumentException($S(message));
                })
                .expect("unsupported request body builds"),
            );
        }
        BodyEncoding::Multipart => unreachable!("multipart handled separately"),
    }
    if !body.required {
        cb.end_control_flow();
    }
}

fn java_json_request_body(body_required: bool, body_var: &str, media_type: &str) -> CodeBlock {
    sigil_quote!(Java {
        String jsonBody = gson.toJson($L(body_var));
        $if(body_required) {
            RequestBody requestBody = RequestBody.create(jsonBody, MediaType.get($S(media_type)));
        } $else {
            requestBody = RequestBody.create(jsonBody, MediaType.get($S(media_type)));
        }
    })
    .expect("json request body builds")
}

fn java_raw_request_body(body_required: bool, body_var: &str, media_type: &str) -> CodeBlock {
    sigil_quote!(Java {
        $if(body_required) {
            RequestBody requestBody = RequestBody.create($L(body_var), MediaType.get($S(media_type)));
        } $else {
            requestBody = RequestBody.create($L(body_var), MediaType.get($S(media_type)));
        }
    })
    .expect("raw request body builds")
}

fn response_decode_expr(tr: &TypedResponse) -> String {
    match tr.decoding {
        ResponseDecoding::Json => {
            let type_token = format!("new TypeToken<{}>() {{}}.getType()", tr.java_type);
            format!("gson.fromJson(responseText.isEmpty() ? \"null\" : responseText, {type_token})")
        }
        ResponseDecoding::Text => "responseText".to_string(),
        ResponseDecoding::Bytes => "responseBytes".to_string(),
    }
}

fn java_response_decode_assignments(typed_responses: &[TypedResponse]) -> CodeBlock {
    let mut cb = CodeBlock::builder();
    let mut seen: HashSet<String> = HashSet::new();
    let mut emitted_any = false;
    for tr in typed_responses {
        if !seen.insert(tr.field_name.clone()) {
            continue;
        }
        let keyword = if emitted_any { "else if" } else { "if" };
        let guard = response_status_guard_java(&tr.status);
        let assignment = format!("{} = {}", tr.field_name, response_decode_expr(tr));
        cb.begin_control_flow(&format!("{keyword} ({guard})"), ());
        cb.add(&format!("{assignment};\n"), ());
        cb.end_control_flow();
        emitted_any = true;
    }
    cb.build().expect("Java response decode assignments build")
}

fn java_error_throw(plan: &OpPlan<'_>) -> CodeBlock {
    let mut cb = CodeBlock::builder();
    cb.begin_control_flow("if (!response.isSuccessful())", ());
    cb.add_statement("okhttp3.ResponseBody responseBody = response.body()", ());
    cb.add_statement(
        "java.nio.charset.Charset responseCharset = responseBody != null && responseBody.contentType() != null ? responseBody.contentType().charset(StandardCharsets.UTF_8) : StandardCharsets.UTF_8",
        (),
    );
    cb.add_statement(
        "byte[] responseBytes = responseBody != null ? responseBody.bytes() : new byte[0]",
        (),
    );
    cb.add_statement(
        "String responseText = new String(responseBytes, responseCharset)",
        (),
    );
    cb.add(&format!("{}Detail detail;\n", plan.error_type), ());
    cb.begin_control_flow("switch (response.code())", ());
    let mut seen = HashSet::new();
    for response in plan
        .error_responses
        .iter()
        .filter(|response| response.status.parse::<u16>().is_ok())
    {
        if !seen.insert(response.field_name.clone()) {
            continue;
        }
        cb.add(&format!("case {}:\n", response.status), ());
        cb.add("%>", ());
        cb.add_code(java_error_detail_assign(plan, response));
        cb.add("break;\n", ());
        cb.add("%<", ());
    }
    cb.add("default:\n", ());
    cb.add("%>", ());
    for response in plan
        .error_responses
        .iter()
        .filter(|response| response.status.ends_with("XX"))
    {
        cb.begin_control_flow(
            &format!("if ({})", wildcard_status_guard_java(&response.status)),
            (),
        );
        cb.add_code(java_error_detail_assign(plan, response));
        cb.add("break;\n", ());
        cb.end_control_flow();
    }
    if let Some(default) = plan
        .error_responses
        .iter()
        .find(|response| response.status.eq_ignore_ascii_case("default"))
    {
        cb.add_code(java_error_detail_assign(plan, default));
    } else {
        let unexpected = format!("{}Unexpected", plan.method_name.to_pascal_case());
        cb.add(
            &format!(
                "detail = new {unexpected}(response.code(), response.headers(), responseBytes);\n"
            ),
            (),
        );
    }
    cb.add("%<", ());
    cb.end_control_flow();
    cb.add(
        &format!(
            "throw new {}(response.code(), response.message(), responseText, response.headers(), responseBytes, detail);\n",
            plan.error_type
        ),
        (),
    );
    cb.end_control_flow();
    cb.build().expect("Java error throw builds")
}

fn java_error_detail_assign(plan: &OpPlan<'_>, response: &TypedResponse) -> CodeBlock {
    let class_name = format!(
        "{}{}",
        plan.method_name.to_pascal_case(),
        response.field_name
    );
    let stmt =
        format!("detail = new {class_name}(response.code(), response.headers(), responseBytes);");
    CodeBlock::of(&stmt, ()).expect("Java error detail assign builds")
}

fn emit_required_multipart_part(
    cb: &mut sigil_stitch::code_block::CodeBlockBuilder,
    part: &MultipartPart,
    access: &str,
) {
    cb.add_code(java_multipart_part(part, access));
}

fn java_multipart_part(part: &MultipartPart, access: &str) -> CodeBlock {
    let wire_name = part.wire_name.as_str();
    let content_type = part.content_type.as_str();
    sigil_quote!(Java {
        $if(part.is_binary) {
            multipartBuilder.addFormDataPart($S(wire_name), $L(access).filenameOrDefault($S(wire_name)), RequestBody.create($L(access).getData(), MediaType.get($S(content_type))));
        } $else_if(part.value_encoding == MultipartValueEncoding::Json) {
            multipartBuilder.addFormDataPart($S(wire_name), null, RequestBody.create(gson.toJson($L(access)), MediaType.get($S(content_type))));
        } $else_if(part.value_encoding == MultipartValueEncoding::Unsupported) {
            throw new IllegalArgumentException($S("unsupported multipart part content type"));
        } $else {
            multipartBuilder.addFormDataPart($S(wire_name), null, RequestBody.create(String.valueOf($L(access)), MediaType.get($S(content_type))));
        }
    })
    .expect("multipart part block builds")
}

// ---------------------------------------------------------------------------
// Planning
// ---------------------------------------------------------------------------

struct OpPlan<'a> {
    op: &'a IrOperation,
    method_name: String,
    response_type: String,
    error_type: String,
    path_params: Vec<ParamBinding<'a>>,
    query_params: Vec<ParamBinding<'a>>,
    header_params: Vec<ParamBinding<'a>>,
    body: Option<BodyBinding>,
    typed_responses: Vec<TypedResponse>,
    error_responses: Vec<TypedResponse>,
    success_headers: Vec<ResponseHeaderPlan>,
    error_headers: Vec<ResponseHeaderPlan>,
}

struct ParamBinding<'a> {
    param: &'a IrParameter,
    var_name: String,
    java_type: String,
}

struct BodyBinding {
    var_name: String,
    java_type: String,
    media_type: String,
    required: bool,
    encoding: BodyEncoding,
    multipart_parts: Option<Vec<MultipartPart>>,
}

struct MultipartPart {
    wire_name: String,
    field_name: String,
    is_binary: bool,
    required: bool,
    content_type: String,
    value_encoding: MultipartValueEncoding,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum BodyEncoding {
    Json,
    Multipart,
    FormUrlEncoded,
    Xml,
    TextPlain,
    OctetStream,
    Other,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum ResponseDecoding {
    Json,
    Text,
    Bytes,
}

struct TypedResponse {
    status: String,
    field_name: String,
    java_type: String,
    decoding: ResponseDecoding,
}

fn plan_operation<'a>(
    op: &'a IrOperation,
    ir: &IrSpec,
    request_inputs: &RequestInputPlan,
) -> OpPlan<'a> {
    let op_id = sanitize_operation_id(&op.operation_id, &op.method, &op.path);
    let method_name = op_id.to_lower_camel_case();
    let response_type = format!("{}Response", op_id.to_pascal_case());
    let error_type = format!("{}Exception", op_id.to_pascal_case());

    let mut used_names: HashSet<String> = HashSet::new();

    let mut path_params = Vec::new();
    let mut query_params = Vec::new();
    let mut header_params = Vec::new();
    for p in &op.parameters {
        let var_name = unique_name(&java_ident(&p.name), &mut used_names);
        let java_type = if p.required {
            java_type_str(&p.type_expr)
        } else {
            java_boxed_type_str(&p.type_expr)
        };
        let binding = ParamBinding {
            param: p,
            var_name,
            java_type,
        };
        match p.location {
            ParameterLocation::Path => path_params.push(binding),
            ParameterLocation::Query => query_params.push(binding),
            ParameterLocation::Header => header_params.push(binding),
            ParameterLocation::Cookie => header_params.push(binding),
        }
    }

    let body = op
        .request_body
        .as_ref()
        .and_then(|b| plan_body(op, b, ir, request_inputs, &mut used_names));

    let mut typed_responses: Vec<TypedResponse> = op
        .responses
        .iter()
        .filter(|r| is_success_status(&r.status))
        .filter_map(plan_response)
        .collect();
    typed_responses.sort_by_key(|r| response_match_rank(&r.status));
    let error_responses = op
        .responses
        .iter()
        .filter(|r| !is_success_status(&r.status))
        .map(plan_error_response)
        .collect();
    let success_headers = collect_response_headers(
        op.responses
            .iter()
            .filter(|response| is_success_status(&response.status)),
        ir,
    );
    let error_headers = collect_response_headers(
        op.responses
            .iter()
            .filter(|response| !is_success_status(&response.status)),
        ir,
    );

    OpPlan {
        op,
        method_name,
        response_type,
        error_type,
        path_params,
        query_params,
        header_params,
        body,
        typed_responses,
        error_responses,
        success_headers,
        error_headers,
    }
}

fn plan_body(
    op: &IrOperation,
    b: &IrRequestBody,
    ir: &IrSpec,
    request_inputs: &RequestInputPlan,
    used_names: &mut HashSet<String>,
) -> Option<BodyBinding> {
    let (media_type, t) = pick_body_content(b)?;
    let encoding = body_encoding(&media_type);
    let java_type = match encoding {
        BodyEncoding::TextPlain => "String".to_string(),
        BodyEncoding::OctetStream => "byte[]".to_string(),
        BodyEncoding::Multipart => request_input_for_operation(request_inputs, op, &media_type)
            .map(|input| input.name.to_pascal_case())
            .unwrap_or_else(|| java_type_str(&t)),
        _ => java_type_str(&t),
    };
    let var_name = unique_name("body", used_names);
    let multipart_parts = if media_type_base(&media_type) == "multipart/form-data" {
        multipart_parts_for(b, &media_type, ir)
    } else {
        None
    };
    Some(BodyBinding {
        var_name,
        java_type,
        media_type,
        required: b.required,
        encoding,
        multipart_parts,
    })
}

fn plan_response(r: &IrResponse) -> Option<TypedResponse> {
    let (media_type, t) = pick_response_content(r)?;
    let decoding = response_decoding(&media_type);
    let java_type = match decoding {
        ResponseDecoding::Json => java_type_str(&t),
        ResponseDecoding::Text => "String".to_string(),
        ResponseDecoding::Bytes => "byte[]".to_string(),
    };
    Some(TypedResponse {
        status: r.status.clone(),
        field_name: response_field_name(&r.status),
        java_type,
        decoding,
    })
}

fn plan_error_response(r: &IrResponse) -> TypedResponse {
    match pick_response_content(r) {
        Some((media_type, t)) => {
            let decoding = response_decoding(&media_type);
            let java_type = match decoding {
                ResponseDecoding::Json => java_type_str(&t),
                ResponseDecoding::Text => "String".to_string(),
                ResponseDecoding::Bytes => "byte[]".to_string(),
            };
            TypedResponse {
                status: r.status.clone(),
                field_name: response_variant_name(&r.status),
                java_type,
                decoding,
            }
        }
        None => TypedResponse {
            status: r.status.clone(),
            field_name: response_variant_name(&r.status),
            java_type: "byte[]".to_string(),
            decoding: ResponseDecoding::Bytes,
        },
    }
}

fn is_success_status(status: &str) -> bool {
    status
        .parse::<u16>()
        .is_ok_and(|code| (200..300).contains(&code))
        || status.eq_ignore_ascii_case("2XX")
}

fn response_field_name(status: &str) -> String {
    if status == "default" {
        "default_".to_string()
    } else if let Ok(code) = status.parse::<u16>() {
        format!("status{code}")
    } else {
        format!("status{}", status.to_lowercase())
    }
}

fn wildcard_status_guard_java(status: &str) -> String {
    let upper = status.to_uppercase();
    if upper == "4XX" {
        "response.code() >= 400 && response.code() < 500".to_string()
    } else if upper == "5XX" {
        "response.code() >= 500 && response.code() < 600".to_string()
    } else if upper == "2XX" {
        "response.code() >= 200 && response.code() < 300".to_string()
    } else {
        // "default" or unknown wildcard: match everything (fallback response)
        "true".to_string()
    }
}

fn response_status_guard_java(status: &str) -> String {
    status
        .parse::<u16>()
        .map(|code| format!("response.code() == {code}"))
        .unwrap_or_else(|_| wildcard_status_guard_java(status))
}

fn body_encoding(media_type: &str) -> BodyEncoding {
    let base = media_type_base(media_type);
    if base == "multipart/form-data" {
        BodyEncoding::Multipart
    } else if is_json_media_type(media_type) {
        BodyEncoding::Json
    } else if base == "application/x-www-form-urlencoded" {
        BodyEncoding::FormUrlEncoded
    } else if is_xml_media_type(media_type) {
        BodyEncoding::Xml
    } else if base == "text/plain" {
        BodyEncoding::TextPlain
    } else if base == "application/octet-stream" {
        BodyEncoding::OctetStream
    } else {
        BodyEncoding::Other
    }
}

fn response_decoding(media_type: &str) -> ResponseDecoding {
    let base = media_type_base(media_type);
    if is_json_media_type(media_type) {
        ResponseDecoding::Json
    } else if base == "text/plain" || is_xml_media_type(media_type) {
        ResponseDecoding::Text
    } else {
        ResponseDecoding::Bytes
    }
}

fn pick_body_content(body: &IrRequestBody) -> Option<(String, IrTypeExpr)> {
    pick_media_type(&body.content, |media_type| {
        media_type_base(media_type) == "application/json"
    })
    .or_else(|| pick_media_type(&body.content, is_json_media_type))
    .or_else(|| {
        pick_media_type(&body.content, |media_type| {
            media_type_base(media_type) == "multipart/form-data"
        })
    })
    .or_else(|| {
        pick_media_type(&body.content, |media_type| {
            media_type_base(media_type) == "application/x-www-form-urlencoded"
        })
    })
    .or_else(|| pick_media_type(&body.content, is_xml_media_type))
    .or_else(|| {
        pick_media_type(&body.content, |media_type| {
            media_type_base(media_type) == "text/plain"
        })
    })
    .or_else(|| {
        pick_media_type(&body.content, |media_type| {
            media_type_base(media_type) == "application/octet-stream"
        })
    })
    .or_else(|| pick_first_content(&body.content))
}

fn pick_response_content(r: &IrResponse) -> Option<(String, IrTypeExpr)> {
    pick_media_type(&r.content, |media_type| {
        media_type_base(media_type) == "application/json"
    })
    .or_else(|| pick_media_type(&r.content, is_json_media_type))
    .or_else(|| {
        pick_media_type(&r.content, |media_type| {
            media_type_base(media_type) == "application/octet-stream"
        })
    })
    .or_else(|| {
        pick_media_type(&r.content, |media_type| {
            media_type_base(media_type) == "text/plain"
        })
    })
    .or_else(|| pick_media_type(&r.content, is_xml_media_type))
    .or_else(|| pick_first_content(&r.content))
}

fn pick_media_type(
    content: &indexmap::IndexMap<String, IrTypeExpr>,
    predicate: impl Fn(&str) -> bool,
) -> Option<(String, IrTypeExpr)> {
    content
        .iter()
        .find(|(media_type, _)| predicate(media_type))
        .map(|(media_type, t)| (media_type.clone(), t.clone()))
}

fn pick_first_content(
    content: &indexmap::IndexMap<String, IrTypeExpr>,
) -> Option<(String, IrTypeExpr)> {
    content
        .iter()
        .next()
        .map(|(media_type, t)| (media_type.clone(), t.clone()))
}

fn media_type_base(media_type: &str) -> String {
    media_type
        .split(';')
        .next()
        .unwrap_or(media_type)
        .trim()
        .to_ascii_lowercase()
}

fn is_json_media_type(media_type: &str) -> bool {
    let base = media_type_base(media_type);
    base == "application/json" || base.ends_with("+json")
}

fn is_xml_media_type(media_type: &str) -> bool {
    let base = media_type_base(media_type);
    base == "application/xml" || base == "text/xml" || base.ends_with("+xml")
}

fn multipart_parts_for(
    body: &IrRequestBody,
    media_type: &str,
    ir: &IrSpec,
) -> Option<Vec<MultipartPart>> {
    multipart_parts_for_request_body(body, media_type, ir).map(|parts| {
        parts
            .into_iter()
            .map(|part| MultipartPart {
                field_name: java_field_name(&part.wire_name),
                wire_name: part.wire_name,
                is_binary: part.is_binary,
                required: part.required,
                content_type: part.content_type,
                value_encoding: part.value_encoding,
            })
            .collect()
    })
}