openapi-to-rust 0.3.0

Generate strongly-typed Rust structs, HTTP clients, and SSE streaming clients from OpenAPI 3.1 specifications
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
//! Tests for enum improvements including:
//! - Inline enum extraction from properties
//! - Const + enum support
//! - Extensible enums (anyOf with const values)
//! - OneOf discriminated unions in properties
//! - Array union item naming

use openapi_to_rust::test_helpers::*;
use serde_json::json;

#[test]
fn test_inline_enum_extraction_from_property() {
    let spec = json!({
        "openapi": "3.1.0",
        "info": {
            "title": "Test API",
            "version": "1.0.0"
        },
        "components": {
            "schemas": {
                "Message": {
                    "type": "object",
                    "properties": {
                        "role": {
                            "type": "string",
                            "enum": ["user", "assistant", "system"],
                            "description": "The role of the message author"
                        },
                        "content": {
                            "type": "string"
                        }
                    },
                    "required": ["role", "content"]
                }
            }
        }
    });

    let result = test_generation("inline_enum_extraction", spec).expect("Generation failed");

    // Verify the generated code contains expected types
    assert!(result.contains("pub struct Message"));
    assert!(result.contains("pub enum MessageRole"));
    assert!(result.contains("User"));
    assert!(result.contains("Assistant"));
    assert!(result.contains("System"));
}

#[test]
fn test_property_with_const_and_enum() {
    let spec = json!({
        "openapi": "3.1.0",
        "info": {
            "title": "Test API",
            "version": "1.0.0"
        },
        "components": {
            "schemas": {
                "AssistantMessage": {
                    "type": "object",
                    "properties": {
                        "role": {
                            "type": "string",
                            "const": "assistant",
                            "enum": ["assistant"],
                            "description": "Always 'assistant' for this message type"
                        },
                        "content": {
                            "type": "string"
                        }
                    },
                    "required": ["role", "content"]
                }
            }
        }
    });

    let result = test_generation("const_and_enum", spec).expect("Generation failed");

    // Verify the const field is handled properly
    assert!(result.contains("pub struct AssistantMessage"));
    assert!(result.contains("pub enum AssistantMessageRole"));
    assert!(result.contains("Assistant"));
}

#[test]
fn test_extensible_enum_anyof_const_values() {
    let spec = json!({
        "openapi": "3.1.0",
        "info": {
            "title": "Test API",
            "version": "1.0.0"
        },
        "components": {
            "schemas": {
                "Model": {
                    "title": "Model",
                    "anyOf": [
                        {
                            "type": "string",
                            "const": "claude-3-opus"
                        },
                        {
                            "type": "string",
                            "const": "claude-3-sonnet"
                        },
                        {
                            "type": "string",
                            "const": "claude-3-haiku"
                        },
                        {
                            "type": "string",
                            "description": "Custom model identifier"
                        }
                    ],
                    "description": "Available model identifiers"
                },
                "CreateMessageParams": {
                    "type": "object",
                    "properties": {
                        "model": {
                            "$ref": "#/components/schemas/Model"
                        },
                        "content": {
                            "type": "string"
                        }
                    },
                    "required": ["model", "content"]
                }
            }
        }
    });

    let result = test_generation("extensible_enum", spec).expect("Generation failed");

    // Verify the extensible enum is generated correctly
    assert!(result.contains("pub enum Model"));
    assert!(result.contains("Claude3Opus"));
    assert!(result.contains("Claude3Sonnet"));
    assert!(result.contains("Claude3Haiku"));
    assert!(
        result.contains("Other") || result.contains("Custom"),
        "Should contain Other/Custom variant for extensible enum"
    );
    assert!(result.contains("pub struct CreateMessageParams"));
}

#[test]
fn test_oneof_discriminated_union_in_property() {
    let spec = json!({
        "openapi": "3.1.0",
            "info": {
                "title": "Test API",
                "version": "1.0.0"
            },
            "components": {
                "schemas": {
                    "ImageBlock": {
                        "type": "object",
                        "properties": {
                            "type": {
                                "type": "string",
                                "const": "image"
                            },
                            "source": {
                                "oneOf": [
                                    {"$ref": "#/components/schemas/Base64ImageSource"},
                                    {"$ref": "#/components/schemas/URLImageSource"}
                                ],
                                "discriminator": {
                                    "propertyName": "type",
                                    "mapping": {
                                        "base64": "#/components/schemas/Base64ImageSource",
                                        "url": "#/components/schemas/URLImageSource"
                                    }
                                }
                            }
                        },
                        "required": ["type", "source"]
                    },
                    "Base64ImageSource": {
                        "type": "object",
                        "properties": {
                            "type": {
                                "type": "string",
                                "const": "base64"
                            },
                            "data": {
                                "type": "string"
                            }
                        },
                        "required": ["type", "data"]
                    },
                    "URLImageSource": {
                        "type": "object",
                        "properties": {
                            "type": {
                                "type": "string",
                                "const": "url"
                            },
                            "url": {
                                "type": "string"
                            }
                        },
                        "required": ["type", "url"]
                    }
                }
            }
    });

    let result = test_generation("oneof_in_property", spec).expect("Generation failed");

    // Verify the discriminated union in property is handled
    assert!(result.contains("pub struct ImageBlock"));
    assert!(result.contains("pub enum ImageBlockSource"));
    assert!(result.contains("Base64ImageSource"));
    assert!(
        result.contains("URLImageSource") || result.contains("UrlImageSource"),
        "Should contain URL/Url ImageSource struct"
    );
}

#[test]
fn test_array_with_union_items() {
    let spec = json!({
        "openapi": "3.1.0",
            "info": {
                "title": "Test API",
                "version": "1.0.0"
            },
            "components": {
                "schemas": {
                    "ToolsRequest": {
                        "type": "object",
                        "properties": {
                            "tools": {
                                "type": "array",
                                "items": {
                                    "oneOf": [
                                        {"$ref": "#/components/schemas/TextTool"},
                                        {"$ref": "#/components/schemas/CodeTool"}
                                    ],
                                    "discriminator": {
                                        "propertyName": "type"
                                    }
                                }
                            }
                        },
                        "required": ["tools"]
                    },
                    "TextTool": {
                        "type": "object",
                        "properties": {
                            "type": {
                                "type": "string",
                                "const": "text"
                            },
                            "name": {
                                "type": "string"
                            }
                        },
                        "required": ["type", "name"]
                    },
                    "CodeTool": {
                        "type": "object",
                        "properties": {
                            "type": {
                                "type": "string",
                                "const": "code"
                            },
                            "language": {
                                "type": "string"
                            }
                        },
                        "required": ["type", "language"]
                    }
                }
            }
    });

    let result = test_generation("array_union_items", spec).expect("Generation failed");

    // Verify array with union items is handled
    assert!(result.contains("pub struct ToolsRequest"));
    assert!(result.contains("pub enum ToolsRequestToolsItem"));
    assert!(result.contains("Text") && result.contains("TextTool"));
    assert!(result.contains("Code") && result.contains("CodeTool"));
    assert!(result.contains("pub struct TextTool"));
    assert!(result.contains("pub struct CodeTool"));
}

#[test]
fn test_multiple_properties_with_enums() {
    // Test that multiple properties with enum values in the same schema get unique enum names
    let spec = json!({
        "openapi": "3.1.0",
            "info": {
                "title": "Test API",
                "version": "1.0.0"
            },
            "components": {
                "schemas": {
                    "Task": {
                        "type": "object",
                        "properties": {
                            "status": {
                                "type": "string",
                                "enum": ["pending", "running", "completed", "failed"],
                                "description": "Task status"
                            },
                            "priority": {
                                "type": "string",
                                "enum": ["low", "medium", "high", "critical"],
                                "description": "Task priority"
                            },
                            "type": {
                                "type": "string",
                                "enum": ["build", "test", "deploy"],
                                "description": "Task type"
                            }
                        },
                        "required": ["status", "priority", "type"]
                    }
                }
            }
    });

    let result = test_generation("multiple_enum_properties", spec).expect("Generation failed");

    // Verify multiple enum properties generate unique enum names
    assert!(result.contains("pub struct Task"));
    assert!(result.contains("pub enum TaskStatus"));
    assert!(result.contains("pub enum TaskPriority"));
    assert!(result.contains("pub enum TaskType"));
    assert!(result.contains("Pending"));
    assert!(result.contains("Running"));
    assert!(result.contains("Low"));
    assert!(result.contains("High"));
    assert!(result.contains("Build"));
    assert!(result.contains("Deploy"));
}

#[test]
fn test_extensible_enum_serialization() {
    // Test that extensible enums serialize correctly to their string values
    let spec = json!({
        "openapi": "3.1.0",
            "info": {
                "title": "Test API",
                "version": "1.0.0"
            },
            "components": {
                "schemas": {
                    "Model": {
                        "title": "Model",
                        "anyOf": [
                            {
                                "type": "string",
                                "const": "gpt-4"
                            },
                            {
                                "type": "string",
                                "const": "gpt-3.5-turbo"
                            },
                            {
                                "type": "string",
                                "const": "claude-3-opus"
                            },
                            {
                                "type": "string",
                                "description": "Custom model identifier"
                            }
                        ],
                        "description": "Available model identifiers"
                    },
                    "Request": {
                        "type": "object",
                        "properties": {
                            "model": {
                                "$ref": "#/components/schemas/Model"
                            },
                            "prompt": {
                                "type": "string"
                            }
                        },
                        "required": ["model", "prompt"]
                    }
                }
            }
    });

    let result = test_generation("extensible_enum_serialization", spec).expect("Generation failed");

    // Check that Model enum has custom Serialize implementation
    assert!(
        result.contains("impl serde::Serialize for Model"),
        "Model enum should have custom Serialize implementation"
    );
    assert!(
        result.contains("impl<'de> serde::Deserialize<'de> for Model"),
        "Model enum should have custom Deserialize implementation"
    );

    // Verify it's not using untagged enum
    assert!(
        !result.contains("#[serde(untagged)]"),
        "Model enum should not use untagged serde attribute"
    );

    // Verify the enum variants
    assert!(result.contains("Gpt4"));
    assert!(result.contains("Gpt35Turbo"));
    assert!(result.contains("Claude3Opus"));
    assert!(
        result.contains("Other") || result.contains("Custom"),
        "Should contain Other/Custom variant for extensible enum"
    );
}

#[test]
fn test_inline_enum_collision_at_different_nesting_levels() {
    // Real-world case from Latitude.sh's `plan_data` schema:
    //
    //   plan_data.type                                              -> ["plans"]
    //   plan_data.attributes.specs.drives[].type                    -> ["SSD","HDD","NVME"]
    //
    // Both want the synthetic name `PlanDataType`. Before the fix,
    // the second registration overwrote the first in `resolved_cache`,
    // so the top-level `type` field's reference still pointed at
    // `PlanDataType` but that enum was now `{SSD, HDD, NVME}` — drives
    // deserialization was fine but every plan response failed to
    // deserialize the top-level `type: "plans"` (and vice versa,
    // depending on processing order).
    let spec = json!({
        "openapi": "3.1.0",
        "info": {"title": "Test API", "version": "1.0.0"},
        "components": {
            "schemas": {
                "PlanData": {
                    "type": "object",
                    "properties": {
                        "id": {"type": "string"},
                        "type": {
                            "type": "string",
                            "enum": ["plans"]
                        },
                        "attributes": {
                            "type": "object",
                            "properties": {
                                "specs": {
                                    "type": "object",
                                    "properties": {
                                        "drives": {
                                            "type": "array",
                                            "items": {
                                                "type": "object",
                                                "properties": {
                                                    "type": {
                                                        "type": "string",
                                                        "enum": ["SSD", "HDD", "NVME"]
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    });

    let result = test_generation("inline_enum_collision_nesting", spec).expect("Generation failed");

    // The exact assignment of "primary" vs "disambiguated" depends on
    // schema-walk order. What we MUST verify is:
    //   1. Two distinct enums got emitted (one for each value-set), and
    //   2. The struct field types route to the correct enums.
    //
    // Both enums must exist somewhere:
    let plans_enum_name = if result.contains("pub enum PlanDataType")
        && extract_enum_variants(&result, "PlanDataType")
            .iter()
            .any(|v| v == "Plans")
    {
        "PlanDataType".to_string()
    } else {
        let disambiguated = ["PlanDataTypePlans"];
        disambiguated
            .iter()
            .find(|name| result.contains(&format!("pub enum {name}")))
            .map(|s| s.to_string())
            .unwrap_or_else(|| panic!("could not find resource-type enum in: {result}"))
    };
    let drives_enum_name = if result.contains("pub enum PlanDataType")
        && extract_enum_variants(&result, "PlanDataType")
            .iter()
            .any(|v| v == "Nvme")
    {
        "PlanDataType".to_string()
    } else {
        let disambiguated = ["PlanDataTypeSsd", "PlanDataTypeHdd", "PlanDataTypeNvme"];
        disambiguated
            .iter()
            .find(|name| result.contains(&format!("pub enum {name}")))
            .map(|s| s.to_string())
            .unwrap_or_else(|| panic!("could not find drives-media-type enum in: {result}"))
    };

    assert_ne!(
        plans_enum_name, drives_enum_name,
        "two distinct inline `type` enums must NOT collapse to the same name: {result}"
    );

    // Resource-type field on `PlanData` references the plans enum.
    let plan_data_struct =
        extract_struct_block(&result, "PlanData").expect("PlanData struct must be present");
    assert!(
        plan_data_struct.contains(&format!("Option<{plans_enum_name}>")),
        "PlanData.type must reference {plans_enum_name}, got: {plan_data_struct}"
    );

    // Drive-item field references the drives enum.
    let drives_struct = extract_struct_block(&result, "PlanDataDrivesItem")
        .expect("PlanDataDrivesItem struct must be present");
    assert!(
        drives_struct.contains(&format!("Option<{drives_enum_name}>")),
        "PlanDataDrivesItem.type must reference {drives_enum_name}, got: {drives_struct}"
    );

    // Drive-media-type variants
    assert!(result.contains("Ssd"), "missing Ssd variant: {result}");
    assert!(result.contains("Hdd"), "missing Hdd variant: {result}");
    assert!(result.contains("Nvme"), "missing Nvme variant: {result}");
}

fn extract_struct_block(source: &str, name: &str) -> Option<String> {
    let header = format!("pub struct {name} {{");
    let start = source.find(&header)?;
    let rest = &source[start..];
    let end = rest.find("\n}\n")?;
    Some(rest[..end].to_string())
}

fn extract_enum_variants(source: &str, name: &str) -> Vec<String> {
    let header = format!("pub enum {name} {{");
    let Some(start) = source.find(&header) else {
        return Vec::new();
    };
    let rest = &source[start + header.len()..];
    let Some(end) = rest.find('}') else {
        return Vec::new();
    };
    rest[..end]
        .lines()
        .filter_map(|line| {
            let trimmed = line.trim();
            // Skip attributes / blank lines / comments
            if trimmed.is_empty() || trimmed.starts_with('#') || trimmed.starts_with("//") {
                return None;
            }
            // Variant lines look like "Plans," or "Plans"
            trimmed
                .trim_end_matches(',')
                .split('(')
                .next()
                .map(|v| v.to_string())
        })
        .collect()
}

#[test]
fn test_inline_enum_dedup_when_values_identical() {
    // Sibling fields that both happen to have the same enum should
    // continue to share a single named type — the disambiguation is
    // strictly for *value* mismatches, not name collisions per se.
    //
    // Two different parent schemas declare a `status` field with the
    // exact same set of values. We expect ONE enum, referenced from
    // both structs.
    let spec = json!({
        "openapi": "3.1.0",
        "info": {"title": "Test API", "version": "1.0.0"},
        "components": {
            "schemas": {
                "Job": {
                    "type": "object",
                    "properties": {
                        "status": {
                            "type": "string",
                            "enum": ["queued", "running", "done"]
                        }
                    }
                },
                "Task": {
                    "type": "object",
                    "properties": {
                        "status": {
                            "type": "string",
                            "enum": ["queued", "running", "done"]
                        }
                    }
                }
            }
        }
    });

    let result =
        test_generation("inline_enum_dedup_identical_values", spec).expect("Generation failed");

    // JobStatus is the canonical name (alphabetical first). TaskStatus
    // either re-uses JobStatus or has its own definition — but if it has
    // its own definition, the values must match. The important check is
    // that whichever shape we end up with, NO disambiguated suffix
    // appears (which would only show up on a value mismatch).
    assert!(
        !result.contains("StatusQueued") && !result.contains("StatusRunning"),
        "values match — should not disambiguate: {result}"
    );
}