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
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
use std::collections::BTreeMap;

use oas3::spec::{
  BooleanSchema, Components, Discriminator, Info, ObjectOrReference, ObjectSchema, Schema, SchemaType, SchemaTypeSet,
  Spec,
};

use crate::generator::schema_registry::{RefCollector, SchemaRegistry};

const SCHEMA_REF_PREFIX: &str = "#/components/schemas/";

fn create_test_spec_with_schemas(schemas: BTreeMap<String, ObjectOrReference<ObjectSchema>>) -> Spec {
  Spec {
    openapi: "3.0.0".to_string(),
    info: Info {
      title: "Test".to_string(),
      summary: None,
      version: "1.0.0".to_string(),
      description: None,
      terms_of_service: None,
      contact: None,
      license: None,
      extensions: BTreeMap::default(),
    },
    servers: vec![],
    paths: Option::default(),
    webhooks: BTreeMap::default(),
    components: Some(Components {
      schemas,
      ..Default::default()
    }),
    security: vec![],
    tags: vec![],
    external_docs: None,
    extensions: BTreeMap::default(),
  }
}

fn make_simple_schema() -> ObjectSchema {
  ObjectSchema {
    schema_type: None,
    properties: BTreeMap::new(),
    ..Default::default()
  }
}

fn make_schema_with_ref(ref_name: &str) -> ObjectSchema {
  let mut properties = BTreeMap::new();
  properties.insert(
    "related".to_string(),
    ObjectOrReference::Ref {
      ref_path: format!("{SCHEMA_REF_PREFIX}{ref_name}"),
      summary: None,
      description: None,
    },
  );
  ObjectSchema {
    schema_type: None,
    properties,
    ..Default::default()
  }
}

fn make_ref(name: &str) -> ObjectOrReference<ObjectSchema> {
  ObjectOrReference::Ref {
    ref_path: format!("{SCHEMA_REF_PREFIX}{name}"),
    summary: None,
    description: None,
  }
}

#[test]
fn test_parse_ref() {
  let cases = [
    ("#/components/schemas/User", Some("User")),
    ("#/components/schemas/NestedSchema", Some("NestedSchema")),
    ("#/other/path", None),
    ("InvalidRef", None),
  ];
  for (input, expected) in cases {
    let result = SchemaRegistry::parse_ref(input);
    assert_eq!(result.as_deref(), expected, "failed for input {input:?}");
  }
}

#[test]
fn test_ref_collector() {
  let collector = RefCollector::new(None);

  let schema = make_schema_with_ref("User");
  let refs = collector.collect(&schema);
  assert_eq!(refs.len(), 1, "simple ref: expected 1 ref");
  assert!(refs.contains("User"), "simple ref: should contain User");

  let mut properties = BTreeMap::new();
  properties.insert("author".to_string(), make_ref("User"));
  properties.insert("category".to_string(), make_ref("Category"));
  let schema = ObjectSchema {
    schema_type: None,
    properties,
    ..Default::default()
  };
  let refs = collector.collect(&schema);
  assert_eq!(refs.len(), 2, "multiple refs: expected 2 refs");
  assert!(refs.contains("User"), "multiple refs: should contain User");
  assert!(refs.contains("Category"), "multiple refs: should contain Category");

  let schema = ObjectSchema {
    schema_type: None,
    one_of: vec![make_ref("User")],
    any_of: vec![make_ref("Post")],
    all_of: vec![make_ref("Comment")],
    ..Default::default()
  };
  let refs = collector.collect(&schema);
  assert_eq!(refs.len(), 3, "combinators: expected 3 refs");
  assert!(refs.contains("User"), "combinators: should contain User");
  assert!(refs.contains("Post"), "combinators: should contain Post");
  assert!(refs.contains("Comment"), "combinators: should contain Comment");
}

#[test]
fn test_schema_registry() {
  let mut schemas = BTreeMap::new();
  schemas.insert("User".to_string(), ObjectOrReference::Object(make_simple_schema()));
  schemas.insert("Post".to_string(), ObjectOrReference::Object(make_simple_schema()));

  let spec = create_test_spec_with_schemas(schemas);
  let registry = SchemaRegistry::from_spec(spec).registry;

  assert!(registry.get("User").is_some(), "should have User schema");
  assert!(registry.get("Post").is_some(), "should have Post schema");
  assert!(registry.get("NonExistent").is_none(), "should not have NonExistent");
  assert_eq!(registry.keys().len(), 2, "should have 2 schemas");

  let mut schemas = BTreeMap::new();
  schemas.insert("User".to_string(), ObjectOrReference::Object(make_simple_schema()));
  schemas.insert(
    "Post".to_string(),
    ObjectOrReference::Object(make_schema_with_ref("User")),
  );

  let spec = create_test_spec_with_schemas(schemas);
  let mut graph = SchemaRegistry::from_spec(spec).registry;
  graph.build_dependencies();

  assert_eq!(graph.keys().len(), 2, "build deps: should have 2 schemas");
  assert!(graph.get("User").is_some(), "build deps: should have User");
  assert!(graph.get("Post").is_some(), "build deps: should have Post");
}

#[test]
fn test_schema_graph_cycle_detection() {
  {
    let mut schemas = BTreeMap::new();
    schemas.insert("A".to_string(), ObjectOrReference::Object(make_simple_schema()));
    schemas.insert("B".to_string(), ObjectOrReference::Object(make_schema_with_ref("A")));
    let mut c_schema = make_simple_schema();
    c_schema.properties.insert("b".to_string(), make_ref("B"));
    schemas.insert("C".to_string(), ObjectOrReference::Object(c_schema));

    let spec = create_test_spec_with_schemas(schemas);
    let mut graph = SchemaRegistry::from_spec(spec).registry;
    graph.build_dependencies();
    let cycles = graph.detect_cycles();

    assert!(cycles.is_empty(), "linear deps: should have no cycles");
    assert!(!graph.is_cyclic("A"), "linear deps: A should not be cyclic");
    assert!(!graph.is_cyclic("B"), "linear deps: B should not be cyclic");
    assert!(!graph.is_cyclic("C"), "linear deps: C should not be cyclic");
  }

  {
    let mut a_schema = make_simple_schema();
    a_schema.properties.insert("b".to_string(), make_ref("B"));
    let mut b_schema = make_simple_schema();
    b_schema.properties.insert("a".to_string(), make_ref("A"));

    let mut schemas = BTreeMap::new();
    schemas.insert("A".to_string(), ObjectOrReference::Object(a_schema));
    schemas.insert("B".to_string(), ObjectOrReference::Object(b_schema));

    let spec = create_test_spec_with_schemas(schemas);
    let mut graph = SchemaRegistry::from_spec(spec).registry;
    graph.build_dependencies();
    let cycles = graph.detect_cycles();

    assert_eq!(cycles.len(), 1, "simple cycle: should detect 1 cycle");
    assert!(!cycles[0].is_empty(), "simple cycle: cycle should not be empty");
    assert!(graph.is_cyclic("A"), "simple cycle: A should be cyclic");
    assert!(graph.is_cyclic("B"), "simple cycle: B should be cyclic");
  }

  {
    let mut a_schema = make_simple_schema();
    a_schema.properties.insert("self_ref".to_string(), make_ref("A"));

    let mut schemas = BTreeMap::new();
    schemas.insert("A".to_string(), ObjectOrReference::Object(a_schema));

    let spec = create_test_spec_with_schemas(schemas);
    let mut graph = SchemaRegistry::from_spec(spec).registry;
    graph.build_dependencies();
    let cycles = graph.detect_cycles();

    assert_eq!(cycles.len(), 1, "self-ref: should detect 1 cycle");
    assert!(graph.is_cyclic("A"), "self-ref: A should be cyclic");
  }

  {
    let mut user_schema = make_simple_schema();
    user_schema.properties.insert("posts".to_string(), make_ref("Post"));
    let mut post_schema = make_simple_schema();
    post_schema.properties.insert("author".to_string(), make_ref("User"));

    let mut schemas = BTreeMap::new();
    schemas.insert("User".to_string(), ObjectOrReference::Object(user_schema));
    schemas.insert("Post".to_string(), ObjectOrReference::Object(post_schema));

    let spec = create_test_spec_with_schemas(schemas);
    let mut graph = SchemaRegistry::from_spec(spec).registry;
    graph.build_dependencies();
    let cycles = graph.detect_cycles();

    assert!(!cycles.is_empty(), "user-post cycle: should detect cycles");
    assert!(graph.is_cyclic("User"), "user-post cycle: User should be cyclic");
    assert!(graph.is_cyclic("Post"), "user-post cycle: Post should be cyclic");
  }
}

#[test]
fn test_schema_graph_integration() {
  let mut schemas = BTreeMap::new();
  schemas.insert("User".to_string(), ObjectOrReference::Object(make_simple_schema()));
  schemas.insert(
    "Post".to_string(),
    ObjectOrReference::Object(make_schema_with_ref("User")),
  );

  let spec = create_test_spec_with_schemas(schemas);
  let mut graph = SchemaRegistry::from_spec(spec).registry;

  assert!(graph.get("User").is_some(), "integration: should have User");
  assert!(graph.get("Post").is_some(), "integration: should have Post");
  assert_eq!(graph.keys().len(), 2, "integration: should have 2 schemas");

  graph.build_dependencies();
  let cycles = graph.detect_cycles();

  assert!(cycles.is_empty(), "integration: should have no cycles");
  assert!(!graph.is_cyclic("User"), "integration: User should not be cyclic");
}

#[test]
fn test_schema_registry_merges_all_of_properties_and_required() {
  let mut parent = make_simple_schema();
  parent.schema_type = Some(SchemaTypeSet::Single(SchemaType::Object));
  parent.required.push("id".to_string());
  parent.properties.insert(
    "id".to_string(),
    ObjectOrReference::Object(ObjectSchema {
      schema_type: Some(SchemaTypeSet::Single(SchemaType::Integer)),
      ..Default::default()
    }),
  );
  parent.additional_properties = Some(Schema::Boolean(BooleanSchema(true)));

  let mut child = make_simple_schema();
  child.schema_type = Some(SchemaTypeSet::Single(SchemaType::Object));
  child.required.push("name".to_string());
  child.properties.insert(
    "name".to_string(),
    ObjectOrReference::Object(ObjectSchema {
      schema_type: Some(SchemaTypeSet::Single(SchemaType::String)),
      ..Default::default()
    }),
  );
  child.all_of.push(make_ref("Parent"));

  let spec = create_test_spec_with_schemas(BTreeMap::from([
    ("Parent".to_string(), ObjectOrReference::Object(parent.clone())),
    ("Child".to_string(), ObjectOrReference::Object(child.clone())),
  ]));

  let mut graph = SchemaRegistry::from_spec(spec).registry;
  graph.build_dependencies();
  graph.detect_cycles();

  let merged = graph.merged("Child").expect("merged schema should exist for Child");

  assert!(merged.schema.properties.contains_key("id"));
  assert!(merged.schema.properties.contains_key("name"));
  assert!(merged.schema.required.contains(&"id".to_string()));
  assert!(merged.schema.required.contains(&"name".to_string()));
  assert!(merged.schema.additional_properties.is_some());
}

#[test]
fn test_schema_registry_merges_and_tracks_discriminator_parents() {
  let mut parent_schema = make_simple_schema();
  parent_schema.properties.insert(
    "kind".to_string(),
    ObjectOrReference::Object(ObjectSchema {
      schema_type: Some(SchemaTypeSet::Single(SchemaType::String)),
      ..Default::default()
    }),
  );
  parent_schema.discriminator = Some(Discriminator {
    property_name: "kind".to_string(),
    mapping: Some(BTreeMap::from([(
      "child".to_string(),
      format!("{SCHEMA_REF_PREFIX}Child"),
    )])),
  });

  let mut child_schema = make_simple_schema();
  child_schema.properties.insert(
    "child_prop".to_string(),
    ObjectOrReference::Object(ObjectSchema {
      schema_type: Some(SchemaTypeSet::Single(SchemaType::Integer)),
      ..Default::default()
    }),
  );
  child_schema.all_of.push(make_ref("Parent"));

  let spec = create_test_spec_with_schemas(BTreeMap::from([
    ("Parent".to_string(), ObjectOrReference::Object(parent_schema.clone())),
    ("Child".to_string(), ObjectOrReference::Object(child_schema.clone())),
  ]));

  let mut graph = SchemaRegistry::from_spec(spec).registry;
  graph.build_dependencies();
  graph.detect_cycles();

  let merged_child = graph.merged("Child").expect("merged schema should exist for Child");

  assert_eq!(merged_child.discriminator_parent.as_deref(), Some("Parent"));
  assert!(merged_child.schema.properties.contains_key("kind"));
  assert!(merged_child.schema.properties.contains_key("child_prop"));

  let discriminator = graph.parent("Child").expect("discriminator parent should be tracked");

  assert_eq!(discriminator.parent_name, "Parent");

  let effective = graph.resolved("Child").unwrap();
  assert_eq!(effective.properties.len(), merged_child.schema.properties.len());
}

#[test]
fn schema_merger_merge_child_with_parent() {
  let mut parent = make_simple_schema();
  parent.schema_type = Some(SchemaTypeSet::Single(SchemaType::Object));
  parent.properties.insert(
    "parent_prop".to_string(),
    ObjectOrReference::Object(ObjectSchema {
      schema_type: Some(SchemaTypeSet::Single(SchemaType::String)),
      ..Default::default()
    }),
  );
  parent.required.push("parent_prop".to_string());

  let mut child = make_simple_schema();
  child.schema_type = Some(SchemaTypeSet::Single(SchemaType::Object));
  child.properties.insert(
    "child_prop".to_string(),
    ObjectOrReference::Object(ObjectSchema {
      schema_type: Some(SchemaTypeSet::Single(SchemaType::Integer)),
      ..Default::default()
    }),
  );
  child.all_of.push(make_ref("Parent"));

  let spec = create_test_spec_with_schemas(BTreeMap::from([
    ("Parent".to_string(), ObjectOrReference::Object(parent)),
    ("Child".to_string(), ObjectOrReference::Object(child)),
  ]));

  let mut graph = SchemaRegistry::from_spec(spec).registry;
  graph.build_dependencies();
  graph.detect_cycles();

  let merged = graph.merged("Child").expect("merged schema should exist for Child");

  assert!(
    merged.schema.properties.contains_key("parent_prop"),
    "should have parent_prop"
  );
  assert!(
    merged.schema.properties.contains_key("child_prop"),
    "should have child_prop"
  );
  assert!(
    merged.schema.required.contains(&"parent_prop".to_string()),
    "parent_prop should be required"
  );

  let effective = graph.resolved("Child").unwrap();
  assert_eq!(
    effective.properties.len(),
    merged.schema.properties.len(),
    "resolved should match merged"
  );
}

#[test]
fn schema_merger_conflict_resolution() {
  let mut parent = make_simple_schema();
  parent.schema_type = Some(SchemaTypeSet::Single(SchemaType::Object));
  parent.properties.insert(
    "prop".to_string(),
    ObjectOrReference::Object(ObjectSchema {
      schema_type: Some(SchemaTypeSet::Single(SchemaType::String)),
      ..Default::default()
    }),
  );

  let mut child = make_simple_schema();
  child.schema_type = Some(SchemaTypeSet::Single(SchemaType::Object));
  child.properties.insert(
    "prop".to_string(),
    ObjectOrReference::Object(ObjectSchema {
      schema_type: Some(SchemaTypeSet::Single(SchemaType::Integer)),
      ..Default::default()
    }),
  );
  child.all_of.push(make_ref("Parent"));

  let spec = create_test_spec_with_schemas(BTreeMap::from([
    ("Parent".to_string(), ObjectOrReference::Object(parent)),
    ("Child".to_string(), ObjectOrReference::Object(child)),
  ]));

  let mut graph = SchemaRegistry::from_spec(spec).registry;
  graph.build_dependencies();
  graph.detect_cycles();

  let merged = graph.merged("Child").expect("merged schema should exist for Child");

  let prop = merged.schema.properties.get("prop").unwrap();
  if let ObjectOrReference::Object(schema) = prop {
    assert_eq!(
      schema.schema_type,
      Some(SchemaTypeSet::Single(SchemaType::Integer)),
      "child property should override parent"
    );
  } else {
    panic!("Expected Object schema");
  }
}

#[test]
fn schema_merger_merge_multiple_all_of() {
  let mut base = make_simple_schema();
  base.schema_type = Some(SchemaTypeSet::Single(SchemaType::Object));
  base.properties.insert(
    "base_prop".to_string(),
    ObjectOrReference::Object(ObjectSchema {
      schema_type: Some(SchemaTypeSet::Single(SchemaType::String)),
      ..Default::default()
    }),
  );
  base.required.push("base_prop".to_string());

  let mut mixin = make_simple_schema();
  mixin.schema_type = Some(SchemaTypeSet::Single(SchemaType::Object));
  mixin.properties.insert(
    "mixin_prop".to_string(),
    ObjectOrReference::Object(ObjectSchema {
      schema_type: Some(SchemaTypeSet::Single(SchemaType::Integer)),
      ..Default::default()
    }),
  );

  let mut composite = make_simple_schema();
  composite.schema_type = Some(SchemaTypeSet::Single(SchemaType::Object));
  composite.all_of.push(make_ref("Base"));
  composite.all_of.push(make_ref("Mixin"));
  composite.properties.insert(
    "own_prop".to_string(),
    ObjectOrReference::Object(ObjectSchema {
      schema_type: Some(SchemaTypeSet::Single(SchemaType::Boolean)),
      ..Default::default()
    }),
  );

  let spec = create_test_spec_with_schemas(BTreeMap::from([
    ("Base".to_string(), ObjectOrReference::Object(base)),
    ("Mixin".to_string(), ObjectOrReference::Object(mixin)),
    ("Composite".to_string(), ObjectOrReference::Object(composite)),
  ]));

  let mut graph = SchemaRegistry::from_spec(spec).registry;
  graph.build_dependencies();
  graph.detect_cycles();

  let merged = graph
    .merged("Composite")
    .expect("merged schema should exist for Composite");

  assert!(
    merged.schema.properties.contains_key("base_prop"),
    "should have base_prop"
  );
  assert!(
    merged.schema.properties.contains_key("mixin_prop"),
    "should have mixin_prop"
  );
  assert!(
    merged.schema.properties.contains_key("own_prop"),
    "should have own_prop"
  );
  assert!(
    merged.schema.required.contains(&"base_prop".to_string()),
    "base_prop should be required"
  );
}