forge-gen 0.1.3

Unified code generator — SDKs, IaC providers, schemas, and docs from OpenAPI specs
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
use std::path::Path;

use anyhow::{Context, Result, bail};
use clap::Args as ClapArgs;
use colored::Colorize;

/// CLI arguments for the `validate` subcommand.
#[derive(Debug, ClapArgs)]
pub struct Args {
    /// Path to the `OpenAPI` spec (YAML or JSON)
    #[arg(long)]
    pub spec: String,
}

/// Parse the spec file and print a summary of endpoints, schemas, and any
/// warnings.
///
/// # Errors
///
/// Returns an error if the spec cannot be read or parsed.
pub fn run(args: &Args) -> Result<()> {
    let path = Path::new(&args.spec);
    if !path.exists() {
        bail!("spec file not found: {}", args.spec);
    }

    let content =
        std::fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;

    // Parse as generic JSON value. If the file is YAML we try serde_json first
    // (works for JSON specs), then fall back to treating it as YAML-flavoured
    // JSON.  For a full YAML parser we would need `serde_yaml`, but for a v0.1
    // validation summary serde_json covers JSON specs and gives a clear error
    // for YAML-only specs.
    let spec: serde_json::Value = if path
        .extension()
        .is_some_and(|ext| ext == "yaml" || ext == "yml")
    {
        // Attempt a simple YAML parse by looking for JSON subset.  Full YAML
        // support would require an extra dep; for now we accept JSON specs and
        // YAML specs that happen to be valid JSON.
        serde_json::from_str(&content).unwrap_or_else(|_| {
            // Provide a minimal placeholder so the rest of the function can
            // still report what it can.
            tracing::warn!("spec appears to be YAML — install serde_yaml for full parsing; showing limited info");
            serde_json::json!({})
        })
    } else {
        serde_json::from_str(&content).with_context(|| "parsing spec as JSON")?
    };

    println!("\n{} Validating {}", "=>".blue().bold(), path.display());

    // ── Info section ─────────────────────────────────────────────────
    if let Some(info) = spec.get("info") {
        let title = info
            .get("title")
            .and_then(serde_json::Value::as_str)
            .unwrap_or("(untitled)");
        let version = info
            .get("version")
            .and_then(serde_json::Value::as_str)
            .unwrap_or("(unknown)");
        println!("  Title:   {title}");
        println!("  Version: {version}");
    }

    // ── Paths / endpoints ────────────────────────────────────────────
    let mut endpoint_count = 0u64;
    let mut warnings: Vec<String> = Vec::new();

    if let Some(paths) = spec.get("paths").and_then(serde_json::Value::as_object) {
        for (path_str, methods) in paths {
            if let Some(methods_obj) = methods.as_object() {
                for (method, op) in methods_obj {
                    endpoint_count += 1;

                    // Warn if operationId is missing.
                    if op.get("operationId").is_none() {
                        warnings.push(format!(
                            "{} {} is missing operationId",
                            method.to_uppercase(),
                            path_str
                        ));
                    }
                }
            }
        }
    }

    // ── Schemas ──────────────────────────────────────────────────────
    let schema_count = spec
        .get("components")
        .and_then(|c| c.get("schemas"))
        .and_then(serde_json::Value::as_object)
        .map_or(0, serde_json::Map::len);

    println!("  Endpoints: {endpoint_count}");
    println!("  Schemas:   {schema_count}");

    if warnings.is_empty() {
        println!("\n  {} No warnings\n", "ok".green().bold());
    } else {
        println!("\n  {} {} warning(s):", "!".yellow().bold(), warnings.len());
        for w in &warnings {
            println!("    - {w}");
        }
        println!();
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn validate_spec_parses_json() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_json");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("spec.json");
        std::fs::write(
            &path,
            r#"{
  "openapi": "3.0.3",
  "info": { "title": "Test API", "version": "1.0.0" },
  "paths": {
    "/pets": {
      "get": { "operationId": "listPets", "responses": { "200": { "description": "ok" } } }
    }
  },
  "components": {
    "schemas": {
      "Pet": { "type": "object", "properties": { "name": { "type": "string" } } }
    }
  }
}"#,
        )
        .unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        let result = run(&args);
        assert!(
            result.is_ok(),
            "validate should succeed for valid JSON spec"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_parses_yaml_extension_gracefully() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_yaml");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("spec.yaml");
        std::fs::write(
            &path,
            "openapi: '3.0.3'\ninfo:\n  title: Test\n  version: '1.0'\npaths: {}\n",
        )
        .unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        let result = run(&args);
        assert!(
            result.is_ok(),
            "validate should not error for YAML specs (degrades gracefully)"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_missing_file_errors() {
        let args = Args {
            spec: "/tmp/forge_gen_nonexistent_spec.json".to_string(),
        };
        let result = run(&args);
        assert!(result.is_err());
    }

    #[test]
    fn validate_counts_endpoints() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_count");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("multi.json");
        std::fs::write(
            &path,
            r#"{
  "openapi": "3.0.3",
  "info": { "title": "Multi", "version": "2.0" },
  "paths": {
    "/a": {
      "get": { "operationId": "getA", "responses": {} },
      "post": { "operationId": "createA", "responses": {} }
    },
    "/b": {
      "delete": { "operationId": "deleteB", "responses": {} }
    }
  }
}"#,
        )
        .unwrap();

        // The function prints endpoint counts but does not expose them
        // directly; we verify it runs without error (3 endpoints, 0 warnings).
        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(run(&args).is_ok());

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_detects_missing_operation_id() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_warn");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("warn.json");
        std::fs::write(
            &path,
            r#"{
  "openapi": "3.0.3",
  "info": { "title": "Warn", "version": "1.0" },
  "paths": {
    "/x": {
      "get": { "responses": {} }
    }
  }
}"#,
        )
        .unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(run(&args).is_ok());

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_no_info_section() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_no_info");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("no-info.json");
        std::fs::write(&path, r#"{ "openapi": "3.0.3", "paths": {} }"#).unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(run(&args).is_ok(), "missing info section should not error");

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_no_paths_section() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_no_paths");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("no-paths.json");
        std::fs::write(
            &path,
            r#"{ "openapi": "3.0.3", "info": { "title": "T", "version": "1.0" } }"#,
        )
        .unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(run(&args).is_ok(), "missing paths section should not error");

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_no_components_section() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_no_comp");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("no-comp.json");
        std::fs::write(
            &path,
            r#"{
  "openapi": "3.0.3",
  "info": { "title": "No Components", "version": "1.0" },
  "paths": {}
}"#,
        )
        .unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(
            run(&args).is_ok(),
            "missing components section should not error"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_empty_json_object() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_empty_obj");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("empty.json");
        std::fs::write(&path, "{}").unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(run(&args).is_ok(), "empty JSON object should not error");

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_invalid_json_errors() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_bad_json");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("bad.json");
        std::fs::write(&path, "not json at all {{{").unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(
            run(&args).is_err(),
            "invalid JSON file (not .yaml) should error"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_yml_extension_gracefully() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_yml");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("spec.yml");
        std::fs::write(
            &path,
            "openapi: '3.0.3'\ninfo:\n  title: T\n  version: '1'\n",
        )
        .unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(
            run(&args).is_ok(),
            ".yml extension should also be handled gracefully"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_json_with_yaml_extension() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_json_yaml_ext");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("spec.yaml");
        std::fs::write(
            &path,
            r#"{ "openapi": "3.0.3", "info": { "title": "JSON in YAML", "version": "1.0" }, "paths": {} }"#,
        )
        .unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(
            run(&args).is_ok(),
            "JSON content with .yaml extension should parse normally"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_with_schemas() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_schemas");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("schemas.json");
        std::fs::write(
            &path,
            r#"{
  "openapi": "3.0.3",
  "info": { "title": "Schema Test", "version": "1.0" },
  "paths": {},
  "components": {
    "schemas": {
      "Alpha": { "type": "object" },
      "Beta": { "type": "string" },
      "Gamma": { "type": "integer" }
    }
  }
}"#,
        )
        .unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(run(&args).is_ok());

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_info_missing_title_and_version() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_no_title");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("notitle.json");
        std::fs::write(&path, r#"{ "info": {}, "paths": {} }"#).unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(
            run(&args).is_ok(),
            "missing title/version in info should use defaults, not error"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn validate_spec_multiple_missing_operation_ids() {
        let dir = std::env::temp_dir().join("forge_gen_test_validate_multi_warn");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("multi-warn.json");
        std::fs::write(
            &path,
            r#"{
  "openapi": "3.0.3",
  "info": { "title": "Multi", "version": "1.0" },
  "paths": {
    "/a": { "get": { "responses": {} }, "post": { "responses": {} } },
    "/b": { "delete": { "responses": {} } }
  }
}"#,
        )
        .unwrap();

        let args = Args {
            spec: path.to_str().unwrap().to_string(),
        };
        assert!(run(&args).is_ok());

        let _ = std::fs::remove_dir_all(&dir);
    }
}