oas3-gen 0.25.0

A rust type generator for OpenAPI v3.1.x specification.
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
use std::collections::{HashMap, HashSet};

use crate::generator::{
  ast::{ClientRootNode, StructToken},
  codegen::{GeneratedFileType, Visibility},
  converter::GenerationTarget,
  orchestrator::Orchestrator,
};

fn make_orchestrator(spec: oas3::Spec, all_schemas: bool) -> Orchestrator {
  Orchestrator::new(
    spec,
    Visibility::default(),
    all_schemas,
    None,
    None,
    false,
    false,
    false,
    false,
    GenerationTarget::default(),
    HashMap::new(),
  )
}

fn make_orchestrator_with_ops(
  spec: oas3::Spec,
  all_schemas: bool,
  only: Option<&HashSet<String>>,
  exclude: Option<&HashSet<String>>,
) -> Orchestrator {
  Orchestrator::new(
    spec,
    Visibility::default(),
    all_schemas,
    only,
    exclude,
    false,
    false,
    false,
    false,
    GenerationTarget::default(),
    HashMap::new(),
  )
}

#[test]
fn test_metadata_and_header_generation() {
  let spec_json = include_str!("../../../fixtures/basic_api.json");
  let spec: oas3::Spec = oas3::from_json(spec_json).unwrap();
  let metadata = ClientRootNode::builder()
    .name(StructToken::new("BasicTestApiClient"))
    .info(&spec.info)
    .servers(&spec.servers)
    .build();

  assert_eq!(metadata.title, "Basic Test API", "title mismatch");
  assert_eq!(metadata.version, "1.0.0", "version mismatch");
  assert_eq!(
    metadata.description.as_deref(),
    Some("A test API.\nWith multiple lines.\nFor testing documentation."),
    "description mismatch"
  );

  let orchestrator = make_orchestrator(spec, false);
  let result = orchestrator.generate_with_header("/path/to/spec.json");
  assert!(result.is_ok(), "generate_with_header failed");

  let output = result.unwrap();
  let code = output.code.code(&GeneratedFileType::Types).unwrap();
  let header_checks = [
    ("AUTO-GENERATED CODE - DO NOT EDIT!", "auto-generated marker"),
    ("//! Basic Test API", "title in header"),
    ("//! Source: /path/to/spec.json", "source path"),
    ("//! Version: 1.0.0", "version in header"),
    ("//! A test API.", "description in header"),
    ("#![allow(clippy::doc_markdown)]", "clippy allow"),
    (
      "A test API.\n//! With multiple lines.\n//! For testing documentation.",
      "multiline description formatting",
    ),
  ];
  for (expected, context) in header_checks {
    assert!(code.contains(expected), "missing {context}: expected '{expected}'");
  }
}

#[test]
fn test_operation_filtering() {
  let spec_json = include_str!("../../../fixtures/operation_filtering.json");

  let mut excluded = HashSet::new();
  excluded.insert("create_user".to_string());
  let spec: oas3::Spec = oas3::from_json(spec_json).unwrap();
  let orchestrator = make_orchestrator_with_ops(spec, false, None, Some(&excluded));
  let output = orchestrator.generate_with_header("test.json").unwrap();
  let code = output.code.code(&GeneratedFileType::Types).unwrap();
  let stats = &output.stats;
  assert_eq!(stats.operations_converted, 2, "excluded create_user should leave 2 ops");
  assert!(
    !code.contains("create_user"),
    "create_user should be excluded from code"
  );

  let spec_full: oas3::Spec = oas3::from_json(spec_json).unwrap();
  let orchestrator_full = make_orchestrator(spec_full, false);
  let output_full = orchestrator_full.generate_with_header("test.json").unwrap();
  let code_full = output_full.code.code(&GeneratedFileType::Types).unwrap();
  let stats_full = &output_full.stats;

  let spec_filtered: oas3::Spec = oas3::from_json(spec_json).unwrap();
  let mut excluded_admin = HashSet::new();
  excluded_admin.insert("admin_action".to_string());
  let orchestrator_filtered = make_orchestrator_with_ops(spec_filtered, false, None, Some(&excluded_admin));
  let output_filtered = orchestrator_filtered.generate_with_header("test.json").unwrap();
  let code_filtered = output_filtered.code.code(&GeneratedFileType::Types).unwrap();
  let stats_filtered = &output_filtered.stats;

  assert_eq!(stats_full.operations_converted, 3, "full spec should have 3 ops");
  assert_eq!(
    stats_filtered.operations_converted, 2,
    "filtered spec should have 2 ops"
  );
  assert!(
    code_full.contains("AdminResponse"),
    "full code should contain AdminResponse"
  );
  assert!(
    !code_filtered.contains("AdminResponse"),
    "filtered code should not contain AdminResponse"
  );
  assert!(
    code_filtered.contains("UserList"),
    "filtered code should still contain UserList"
  );
  assert!(
    code_filtered.contains("User"),
    "filtered code should still contain User"
  );
}

#[test]
fn test_all_schemas_overrides_operation_filtering() {
  let spec_json = include_str!("../../../fixtures/operation_filtering.json");

  let mut only = HashSet::new();
  only.insert("list_users".to_string());

  let spec_without: oas3::Spec = oas3::from_json(spec_json).unwrap();
  let orchestrator_without = make_orchestrator_with_ops(spec_without, false, Some(&only), None);
  let output_without = orchestrator_without.generate_with_header("test.json").unwrap();
  let code_without = output_without.code.code(&GeneratedFileType::Types).unwrap();
  let stats_without = &output_without.stats;

  let spec_with: oas3::Spec = oas3::from_json(spec_json).unwrap();
  let orchestrator_with = make_orchestrator_with_ops(spec_with, true, Some(&only), None);
  let output_with = orchestrator_with.generate_with_header("test.json").unwrap();
  let code_with = output_with.code.code(&GeneratedFileType::Types).unwrap();
  let stats_with = &output_with.stats;

  assert_eq!(stats_without.operations_converted, 1, "without all_schemas: 1 op");
  assert_eq!(stats_with.operations_converted, 1, "with all_schemas: still 1 op");

  let without_checks = [
    (true, "UserList", "should contain UserList"),
    (true, "User", "should contain User"),
    (false, "AdminResponse", "should not contain AdminResponse"),
    (false, "UnreferencedSchema", "should not contain UnreferencedSchema"),
  ];
  for (should_contain, schema, context) in without_checks {
    assert_eq!(
      code_without.contains(schema),
      should_contain,
      "without all_schemas: {context}"
    );
  }

  let with_checks = ["UserList", "User", "AdminResponse", "UnreferencedSchema"];
  for schema in with_checks {
    assert!(code_with.contains(schema), "with all_schemas: should contain {schema}");
  }

  assert_eq!(
    stats_without.orphaned_schemas_count, 2,
    "without all_schemas: 2 orphaned"
  );
  assert_eq!(stats_with.orphaned_schemas_count, 0, "with all_schemas: 0 orphaned");
}

#[test]
fn test_content_types_generation() {
  let spec_json = include_str!("../../../fixtures/content_types.json");
  let spec: oas3::Spec = oas3::from_json(spec_json).unwrap();
  let orchestrator = make_orchestrator(spec, false);

  let output = orchestrator.generate_with_header("test.json").unwrap();
  let code = output.code.code(&GeneratedFileType::Types).unwrap();

  let content_type_checks = [
    ("json_with_diagnostics", "JSON handling for application/json"),
    ("req.text().await?", "text handling for text/plain"),
    ("req.bytes().await?", "binary handling for image/png"),
  ];
  for (expected, context) in content_type_checks {
    assert!(code.contains(expected), "missing {context}");
  }
}

#[test]
fn test_enum_deduplication() {
  let cases = [
    (
      include_str!("../../../fixtures/enum_deduplication.json"),
      vec![
        ("pub enum Status", 1, "Status enum should be defined exactly once"),
        ("pub status: Option<Status>", 1, "StructA should use Status"),
        ("status: Option<Status>", 3, "Multiple structs should use Status"),
      ],
      vec![],
    ),
    (
      include_str!("../../../fixtures/relaxed_enum_deduplication.json"),
      vec![
        ("pub enum Status", 1, "Status enum should be defined"),
        ("pub enum ComplexStatusStatus", 1, "Outer enum should be defined"),
        ("Known(Status)", 1, "Outer enum should wrap Status"),
      ],
      vec![("pub enum ComplexStatusStatusKnown", "Inner enum should be deduplicated")],
    ),
  ];

  for (spec_json, presence_checks, absence_checks) in cases {
    let spec: oas3::Spec = oas3::from_json(spec_json).unwrap();
    let orchestrator = make_orchestrator(spec, true);
    let output = orchestrator.generate_with_header("test.json").unwrap();
    let code = output.code.code(&GeneratedFileType::Types).unwrap();

    for (pattern, expected_count, context) in &presence_checks {
      let actual_count = code.matches(pattern).count();
      assert!(
        actual_count >= *expected_count,
        "{context}: expected at least {expected_count} occurrences of '{pattern}', found {actual_count}"
      );
    }

    for (pattern, context) in &absence_checks {
      assert!(!code.contains(pattern), "{context}: '{pattern}' should not appear");
    }
  }
}

fn make_orchestrator_with_customizations(
  spec: oas3::Spec,
  all_schemas: bool,
  customizations: HashMap<String, String>,
) -> Orchestrator {
  Orchestrator::new(
    spec,
    Visibility::default(),
    all_schemas,
    None,
    None,
    false,
    false,
    false,
    false,
    GenerationTarget::default(),
    customizations,
  )
}

#[test]
fn test_customization_generates_serde_as_attributes() {
  let spec_json = r#"{
    "openapi": "3.0.0",
    "info": { "title": "Test API", "version": "1.0.0" },
    "paths": {},
    "components": {
      "schemas": {
        "Event": {
          "type": "object",
          "properties": {
            "id": { "type": "string" },
            "created_at": { "type": "string", "format": "date-time" },
            "updated_at": { "type": "string", "format": "date-time" }
          },
          "required": ["id", "created_at"]
        }
      }
    }
  }"#;

  let spec: oas3::Spec = oas3::from_json(spec_json).unwrap();
  let customizations = HashMap::from([("date_time".to_string(), "crate::MyDateTime".to_string())]);
  let orchestrator = make_orchestrator_with_customizations(spec, true, customizations);
  let output = orchestrator.generate_with_header("test.json").unwrap();
  let code = output.code.code(&GeneratedFileType::Types).unwrap();

  assert!(
    code.contains("#[serde_with::serde_as]"),
    "Struct should have #[serde_with::serde_as] outer attribute"
  );
  assert!(
    code.contains(r#"#[serde_as(as = "crate::MyDateTime")]"#),
    "Required field should have serde_as attribute with custom type"
  );
  assert!(
    code.contains(r#"#[serde_as(as = "Option<crate::MyDateTime>")]"#),
    "Optional field should have serde_as attribute wrapped in Option"
  );
}

#[test]
fn test_customization_for_multiple_types() {
  let spec_json = r#"{
    "openapi": "3.0.0",
    "info": { "title": "Test API", "version": "1.0.0" },
    "paths": {},
    "components": {
      "schemas": {
        "Entity": {
          "type": "object",
          "properties": {
            "id": { "type": "string", "format": "uuid" },
            "created_at": { "type": "string", "format": "date-time" },
            "birth_date": { "type": "string", "format": "date" }
          },
          "required": ["id", "created_at", "birth_date"]
        }
      }
    }
  }"#;

  let spec: oas3::Spec = oas3::from_json(spec_json).unwrap();
  let customizations = HashMap::from([
    ("date_time".to_string(), "crate::MyDateTime".to_string()),
    ("date".to_string(), "crate::MyDate".to_string()),
    ("uuid".to_string(), "crate::MyUuid".to_string()),
  ]);
  let orchestrator = make_orchestrator_with_customizations(spec, true, customizations);
  let output = orchestrator.generate_with_header("test.json").unwrap();
  let code = output.code.code(&GeneratedFileType::Types).unwrap();

  assert!(
    code.contains(r#"#[serde_as(as = "crate::MyDateTime")]"#),
    "date-time field should have custom type"
  );
  assert!(
    code.contains(r#"#[serde_as(as = "crate::MyDate")]"#),
    "date field should have custom type"
  );
  assert!(
    code.contains(r#"#[serde_as(as = "crate::MyUuid")]"#),
    "uuid field should have custom type"
  );
}

#[test]
fn test_customization_for_array_types() {
  let spec_json = r#"{
    "openapi": "3.0.0",
    "info": { "title": "Test API", "version": "1.0.0" },
    "paths": {},
    "components": {
      "schemas": {
        "Timeline": {
          "type": "object",
          "properties": {
            "timestamps": {
              "type": "array",
              "items": { "type": "string", "format": "date-time" }
            }
          },
          "required": ["timestamps"]
        }
      }
    }
  }"#;

  let spec: oas3::Spec = oas3::from_json(spec_json).unwrap();
  let customizations = HashMap::from([("date_time".to_string(), "crate::MyDateTime".to_string())]);
  let orchestrator = make_orchestrator_with_customizations(spec, true, customizations);
  let output = orchestrator.generate_with_header("test.json").unwrap();
  let code = output.code.code(&GeneratedFileType::Types).unwrap();

  assert!(
    code.contains(r#"#[serde_as(as = "Vec<crate::MyDateTime>")]"#),
    "Array field should have serde_as with Vec wrapper"
  );
}

#[test]
fn test_no_customization_no_serde_as() {
  let spec_json = r#"{
    "openapi": "3.0.0",
    "info": { "title": "Test API", "version": "1.0.0" },
    "paths": {},
    "components": {
      "schemas": {
        "Event": {
          "type": "object",
          "properties": {
            "id": { "type": "string" },
            "created_at": { "type": "string", "format": "date-time" }
          },
          "required": ["id", "created_at"]
        }
      }
    }
  }"#;

  let spec: oas3::Spec = oas3::from_json(spec_json).unwrap();
  let orchestrator = make_orchestrator(spec, true);
  let output = orchestrator.generate_with_header("test.json").unwrap();
  let code = output.code.code(&GeneratedFileType::Types).unwrap();

  assert!(
    !code.contains("#[serde_as(as ="),
    "Code should not contain serde_as field attribute without customizations"
  );
  assert!(
    !code.contains("#[serde_with::serde_as]") || !code.contains("Event"),
    "Event struct should not have serde_as outer attribute without customizations"
  );
}