openapi-to-rust 0.1.13

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
// Complete workflow example showing the entire code generation and usage process
//
// This example demonstrates:
// 1. Loading an OpenAPI spec
// 2. Configuring the generator with TOML (or Rust API)
// 3. Generating code
// 4. Using the generated HTTP client
// 5. Error handling and retry logic

use openapi_to_rust::{
    CodeGenerator, GeneratorConfig, SchemaAnalyzer, config::ConfigFile, http_config::RetryConfig,
};
use std::path::{Path, PathBuf};
use tempfile::TempDir;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== OpenAPI Generator - Complete Workflow Example ===\n");

    // Step 1: Create a simple OpenAPI spec for demonstration
    println!("Step 1: Creating sample OpenAPI specification...");
    let openapi_spec = serde_json::json!({
        "openapi": "3.0.0",
        "info": {
            "title": "Example API",
            "version": "1.0.0"
        },
        "paths": {
            "/items": {
                "get": {
                    "operationId": "listItems",
                    "summary": "List all items",
                    "responses": {
                        "200": {
                            "description": "Success",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "$ref": "#/components/schemas/ItemList"
                                    }
                                }
                            }
                        }
                    }
                },
                "post": {
                    "operationId": "createItem",
                    "summary": "Create a new item",
                    "requestBody": {
                        "required": true,
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/CreateItemRequest"
                                }
                            }
                        }
                    },
                    "responses": {
                        "201": {
                            "description": "Created",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "$ref": "#/components/schemas/Item"
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "/items/{id}": {
                "get": {
                    "operationId": "getItem",
                    "summary": "Get item by ID",
                    "parameters": [
                        {
                            "name": "id",
                            "in": "path",
                            "required": true,
                            "schema": {
                                "type": "string"
                            }
                        }
                    ],
                    "responses": {
                        "200": {
                            "description": "Success",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "$ref": "#/components/schemas/Item"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        "components": {
            "schemas": {
                "Item": {
                    "type": "object",
                    "required": ["id", "name"],
                    "properties": {
                        "id": {
                            "type": "string",
                            "description": "Unique identifier"
                        },
                        "name": {
                            "type": "string",
                            "description": "Item name"
                        },
                        "description": {
                            "type": "string",
                            "description": "Item description"
                        },
                        "created_at": {
                            "type": "integer",
                            "description": "Creation timestamp"
                        }
                    }
                },
                "CreateItemRequest": {
                    "type": "object",
                    "required": ["name"],
                    "properties": {
                        "name": {
                            "type": "string"
                        },
                        "description": {
                            "type": "string"
                        }
                    }
                },
                "ItemList": {
                    "type": "object",
                    "required": ["items"],
                    "properties": {
                        "items": {
                            "type": "array",
                            "items": {
                                "$ref": "#/components/schemas/Item"
                            }
                        },
                        "total": {
                            "type": "integer"
                        }
                    }
                }
            }
        }
    });

    // Create temporary directory for output
    let temp_dir = TempDir::new()?;
    let spec_path = temp_dir.path().join("openapi.json");
    let output_dir = temp_dir.path().join("generated");

    std::fs::write(&spec_path, serde_json::to_string_pretty(&openapi_spec)?)?;
    println!("  Created spec at: {}", spec_path.display());

    // Step 2: Option A - Using TOML Configuration
    println!("\nStep 2A: Demonstrating TOML configuration approach...");
    demonstrate_toml_config(&spec_path, &output_dir)?;

    // Step 2: Option B - Using Rust API
    println!("\nStep 2B: Demonstrating Rust API approach...");
    demonstrate_rust_api(&spec_path, &output_dir)?;

    // Step 3: Show the generated code structure
    println!("\nStep 3: Generated code structure:");
    show_generated_structure(&output_dir)?;

    // Step 4: Demonstrate usage patterns
    println!("\nStep 4: Usage patterns (pseudocode):");
    show_usage_patterns();

    // Step 5: Error handling examples
    println!("\nStep 5: Error handling patterns:");
    show_error_handling();

    println!("\n=== Workflow Complete ===");
    println!("Generated code is in: {}", output_dir.display());
    println!("\nNext steps:");
    println!("1. Review generated files in the output directory");
    println!("2. Add generated module to your project");
    println!("3. Configure HttpClient with your API credentials");
    println!("4. Make API calls using the generated methods");

    Ok(())
}

fn demonstrate_toml_config(
    spec_path: &Path,
    output_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("  Creating TOML configuration...");

    let toml_config = format!(
        r#"
[generator]
spec_path = "{}"
output_dir = "{}"
module_name = "api"

[features]
enable_async_client = true
enable_specta = false

[http_client]
base_url = "https://api.example.com"
timeout_seconds = 30

[http_client.retry]
max_retries = 3
initial_delay_ms = 500
max_delay_ms = 16000

[http_client.tracing]
enabled = true

[http_client.auth]
type = "Bearer"
header_name = "Authorization"

[[http_client.headers]]
name = "content-type"
value = "application/json"
"#,
        spec_path.display(),
        output_dir.display()
    );

    let config_path = output_dir.parent().unwrap().join("config.toml");
    std::fs::write(&config_path, toml_config)?;
    println!("  Created config at: {}", config_path.display());

    // Load and use the config
    let config_file = ConfigFile::load(&config_path)?;
    let generator_config = config_file.into_generator_config();

    println!("  Loaded configuration:");
    println!(
        "    - Base URL: {:?}",
        generator_config
            .http_client_config
            .as_ref()
            .and_then(|c| c.base_url.as_ref())
    );
    println!(
        "    - Retry enabled: {}",
        generator_config.retry_config.is_some()
    );
    println!(
        "    - Tracing enabled: {}",
        generator_config.tracing_enabled
    );

    Ok(())
}

fn demonstrate_rust_api(
    spec_path: &PathBuf,
    output_dir: &PathBuf,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("  Configuring with Rust API...");

    // Read and parse the spec
    let spec_content = std::fs::read_to_string(spec_path)?;
    let spec_value: serde_json::Value = serde_json::from_str(&spec_content)?;

    // Analyze the schema
    println!("  Analyzing OpenAPI schema...");
    let mut analyzer = SchemaAnalyzer::new(spec_value)?;
    let mut analysis = analyzer.analyze()?;

    println!("    - Found {} schemas", analysis.schemas.len());
    println!("    - Found {} operations", analysis.operations.len());

    // Configure the generator
    use openapi_to_rust::http_config::{AuthConfig, HttpClientConfig};

    let config = GeneratorConfig {
        spec_path: spec_path.clone(),
        output_dir: output_dir.clone(),
        module_name: "api".to_string(),
        enable_sse_client: false,
        enable_async_client: true,
        enable_specta: false,
        http_client_config: Some(HttpClientConfig {
            base_url: Some("https://api.example.com".to_string()),
            timeout_seconds: Some(30),
            default_headers: {
                let mut headers = std::collections::HashMap::new();
                headers.insert("content-type".to_string(), "application/json".to_string());
                headers
            },
        }),
        retry_config: Some(RetryConfig {
            max_retries: 3,
            initial_delay_ms: 500,
            max_delay_ms: 16000,
        }),
        tracing_enabled: true,
        auth_config: Some(AuthConfig::Bearer {
            header_name: "Authorization".to_string(),
        }),
        type_mappings: std::collections::BTreeMap::new(),
        streaming_config: None,
        nullable_field_overrides: std::collections::BTreeMap::new(),
        schema_extensions: vec![],
        enable_registry: false,
        registry_only: false,
    };

    // Generate code
    println!("  Generating code...");
    let generator = CodeGenerator::new(config);
    let result = generator.generate_all(&mut analysis)?;

    // Write files
    std::fs::create_dir_all(output_dir)?;
    generator.write_files(&result)?;

    println!("  Generated {} files:", result.files.len());
    for file in &result.files {
        println!("    - {}", file.path.display());
    }

    Ok(())
}

fn show_generated_structure(output_dir: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
    if !output_dir.exists() {
        println!("  Output directory not found (example only)");
        return Ok(());
    }

    println!("  Generated files:");
    for entry in std::fs::read_dir(output_dir)? {
        let entry = entry?;
        let path = entry.path();
        if path.is_file() {
            let size = entry.metadata()?.len();
            println!(
                "    - {} ({} bytes)",
                path.file_name().unwrap().to_string_lossy(),
                size
            );
        }
    }

    Ok(())
}

fn show_usage_patterns() {
    println!(
        r#"
  Basic Usage:
  ------------
  use crate::generated::{{client::HttpClient, types::*}};

  #[tokio::main]
  async fn main() -> Result<(), Box<dyn std::error::Error>> {{
      // Create client
      let client = HttpClient::new()
          .with_base_url("https://api.example.com")
          .with_api_key("your-api-key")
          .with_header("X-Custom-Header".to_string(), "value".to_string());

      // List items (GET /items)
      let items = client.list_items().await?;
      println!("Found {{}} items", items.items.len());

      // Create item (POST /items)
      let new_item = CreateItemRequest {{
          name: "New Item".to_string(),
          description: Some("Description".to_string()),
      }};
      let created = client.create_item(new_item).await?;
      println!("Created item: {{}}", created.id);

      // Get item by ID (GET /items/{{id}})
      let item = client.get_item("item-123").await?;
      println!("Item: {{:?}}", item);

      Ok(())
  }}

  Advanced Configuration:
  -----------------------
  use crate::generated::client::{{HttpClient, RetryConfig}};

  let client = HttpClient::with_config(
      Some(RetryConfig {{
          max_retries: 5,
          initial_delay_ms: 1000,
          max_delay_ms: 30000,
      }}),
      true, // enable tracing
  )
  .with_base_url("https://api.example.com")
  .with_api_key("your-api-key");
"#
    );
}

fn show_error_handling() {
    println!(
        r#"
  Error Handling:
  ---------------
  use crate::generated::http_error::HttpError;

  match client.create_item(request).await {{
      Ok(item) => {{
          println!("Success: {{:?}}", item);
      }}
      Err(HttpError::Network(e)) => {{
          eprintln!("Network error: {{}}", e);
          // Will be retried automatically if retry is configured
      }}
      Err(HttpError::Http {{ status, message, .. }}) => {{
          match status {{
              400 => eprintln!("Bad request: {{}}", message),
              401 => eprintln!("Unauthorized - check API key"),
              404 => eprintln!("Not found: {{}}", message),
              429 => eprintln!("Rate limited - will retry"),
              500..=599 => eprintln!("Server error: {{}}", message),
              _ => eprintln!("HTTP error {{}}: {{}}", status, message),
          }}
      }}
      Err(HttpError::Timeout) => {{
          eprintln!("Request timeout - will retry");
      }}
      Err(HttpError::Auth(msg)) => {{
          eprintln!("Authentication error: {{}}", msg);
      }}
      Err(e) => {{
          eprintln!("Other error: {{}}", e);
      }}
  }}

  Retry Detection:
  ----------------
  match client.request().await {{
      Err(e) if e.is_retryable() => {{
          // Error will be automatically retried by middleware
          println!("Retryable error detected");
      }}
      Err(e) if e.is_client_error() => {{
          // 4xx errors - fix request and retry
          println!("Client error: fix request");
      }}
      Err(e) if e.is_server_error() => {{
          // 5xx errors - may be retried automatically
          println!("Server error");
      }}
      _ => {{}}
  }}
"#
    );
}