oas3-gen 0.26.2

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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use std::collections::{BTreeMap, BTreeSet};

use indexmap::IndexMap;
use oas3::spec::ObjectSchema;

use super::hashing::CanonicalSchema;
use crate::{
  generator::{
    ast::{EnumToken, RustType, StructDef, StructToken, TypeRef},
    naming::{
      identifiers::{ensure_unique, to_rust_type_name},
      name_index::SchemaPrecomputed,
    },
  },
  utils::{SchemaExt, SchemaMap, UnionFingerprint, UnionFingerprints, build_union_fingerprints},
};

#[derive(Default, Debug, Clone)]
struct NameRegistry {
  used_names: BTreeSet<String>,
}

impl NameRegistry {
  /// Converts a base string to a valid Rust type name and appends a numeric suffix if
  /// the name already exists in the registry.
  ///
  /// The returned name is not reserved; call [`reserve`] to mark it as used.
  fn make_unique(&self, base: &str) -> String {
    let rust_name = to_rust_type_name(base);
    ensure_unique(&rust_name, &self.used_names)
  }

  /// Returns the preferred name if it is available and not yet allocated, otherwise
  /// generates a unique name from the fallback base string.
  fn determine_name(&self, preferred: Option<&str>, fallback_base: &str) -> String {
    if let Some(pref) = preferred
      && !self.used_names.contains(pref)
    {
      return pref.to_string();
    }
    self.make_unique(fallback_base)
  }

  /// Returns `true` if the given name has already been reserved in this registry.
  fn is_allocated(&self, name: &str) -> bool {
    self.used_names.contains(name)
  }

  /// Marks a name as used, preventing it from being assigned to future types.
  fn reserve(&mut self, name: String) {
    self.used_names.insert(name);
  }
}

#[derive(Default, Debug, Clone)]
struct SchemaIdentity {
  schema_to_type: BTreeMap<CanonicalSchema, String>,
  precomputed: IndexMap<CanonicalSchema, String>,
  metadata: IndexMap<CanonicalSchema, SchemaPrecomputed>,
}

impl SchemaIdentity {
  /// Stores a mapping of canonical schemas to their precomputed type names for
  /// deterministic naming across code generation runs.
  fn set_precomputed(
    &mut self,
    names: impl IntoIterator<Item = (CanonicalSchema, String)>,
    metadata: impl IntoIterator<Item = (CanonicalSchema, SchemaPrecomputed)>,
  ) {
    self.precomputed = names.into_iter().collect();
    self.metadata = metadata.into_iter().collect();
  }

  /// Returns the type name previously assigned to this canonical schema, or `None`
  /// if the schema has not been registered.
  fn lookup(&self, canonical: &CanonicalSchema) -> Option<&str> {
    self.schema_to_type.get(canonical).map(String::as_str)
  }

  /// Returns the precomputed type name for this canonical schema if one was set
  /// during initialization.
  fn get_precomputed(&self, canonical: &CanonicalSchema) -> Option<&str> {
    self.precomputed.get(canonical).map(String::as_str)
  }

  /// Returns the precomputed enum cache key for this canonical schema if one was
  /// computed during the pre-scan phase.
  fn get_precomputed_enum_cache_key(&self, canonical: &CanonicalSchema) -> Option<&Vec<String>> {
    self.metadata.get(canonical).and_then(|m| m.enum_cache_key.as_ref())
  }

  /// Records that a canonical schema has been assigned the given type name,
  /// enabling deduplication of identical schema structures.
  fn record(&mut self, canonical: CanonicalSchema, type_name: String) {
    self.schema_to_type.insert(canonical, type_name);
  }

  /// Returns `true` if this canonical schema is mapped to exactly the given name.
  fn has_mapping(&self, canonical: &CanonicalSchema, expected_name: &str) -> bool {
    self.schema_to_type.get(canonical).is_some_and(|n| n == expected_name)
  }
}

#[derive(Default, Debug, Clone)]
struct EnumRegistry {
  value_sets_to_type: BTreeMap<Vec<String>, String>,
  precomputed: IndexMap<Vec<String>, String>,
}

impl EnumRegistry {
  /// Stores a mapping of enum value sets to their precomputed type names for
  /// deterministic deduplication of identical enums.
  fn set_precomputed(&mut self, names: impl IntoIterator<Item = (Vec<String>, String)>) {
    self.precomputed = names.into_iter().collect();
  }

  /// Returns the type name for an enum with the given values, checking the runtime
  /// cache first, then falling back to precomputed names.
  fn lookup(&self, values: &[String]) -> Option<&str> {
    self
      .value_sets_to_type
      .get(values)
      .or_else(|| self.precomputed.get(values))
      .map(String::as_str)
  }

  /// Returns `true` if an enum with these values has been registered at runtime
  /// (as opposed to only existing in precomputed names).
  fn is_registered(&self, values: &[String]) -> bool {
    self.value_sets_to_type.contains_key(values)
  }

  /// Records that an enum with the given values has been generated with the specified
  /// type name.
  fn register(&mut self, values: Vec<String>, type_name: String) {
    self.value_sets_to_type.insert(values, type_name);
  }
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone)]
struct UnionKey {
  refs: UnionFingerprint,
  discriminator: Option<String>,
}

impl UnionKey {
  /// Creates a composite key for union deduplication from variant schema references
  /// and an optional discriminator property name.
  fn new(refs: UnionFingerprint, discriminator: Option<String>) -> Self {
    Self { refs, discriminator }
  }
}

#[derive(Default, Debug, Clone)]
struct UnionRegistry {
  union_keys_to_type: IndexMap<UnionKey, String>,
}

impl UnionRegistry {
  /// Returns the type name for a union with the given variant references and
  /// discriminator, or `None` if no such union has been registered.
  fn lookup(&self, refs: &[String], discriminator: Option<&str>) -> Option<&str> {
    let key = UnionKey::new(refs.to_vec(), discriminator.map(String::from));
    self.union_keys_to_type.get(&key).map(String::as_str)
  }

  /// Records that a union with the given variant references and discriminator has
  /// been generated with the specified type name.
  fn register(&mut self, refs: UnionFingerprint, discriminator: Option<String>, type_name: String) {
    let key = UnionKey::new(refs, discriminator);
    self.union_keys_to_type.insert(key, type_name);
  }
}

#[derive(Default, Debug, Clone)]
pub(crate) struct TypeCollector {
  pub(crate) types: Vec<RustType>,
}

impl TypeCollector {
  /// Appends nested dependency types followed by the main type, ensuring dependencies
  /// appear before their dependents in the output.
  fn add(&mut self, mut nested: Vec<RustType>, main: RustType) {
    self.types.append(&mut nested);
    self.types.push(main);
  }

  /// Takes all accumulated types, leaving the collector empty for reuse.
  pub(crate) fn take_types(&mut self) -> Vec<RustType> {
    std::mem::take(&mut self.types)
  }
}

#[derive(Default, Debug, Clone)]
struct StructIndex {
  structs: BTreeMap<String, StructDef>,
}

impl StructIndex {
  /// Stores a struct definition indexed by its type name for later retrieval.
  fn register(&mut self, type_name: String, def: StructDef) {
    self.structs.insert(type_name, def);
  }

  /// Returns the struct definition for the given type name, or `None` if not registered.
  fn get(&self, type_name: &str) -> Option<&StructDef> {
    self.structs.get(type_name)
  }
}

#[derive(Default, Debug, Clone)]
struct TypeRefRegistry {
  resolved_types: BTreeMap<CanonicalSchema, TypeRef>,
}

impl TypeRefRegistry {
  /// Returns the cached TypeRef for a canonical schema, or `None` if not yet resolved.
  fn lookup(&self, canonical: &CanonicalSchema) -> Option<&TypeRef> {
    self.resolved_types.get(canonical)
  }

  /// Caches the resolved TypeRef for a canonical schema to avoid redundant resolution.
  fn register(&mut self, canonical: CanonicalSchema, type_ref: TypeRef) {
    self.resolved_types.insert(canonical, type_ref);
  }
}

#[derive(Default, Debug, Clone)]
struct SchemaNameRegistry {
  names: BTreeSet<String>,
}

impl SchemaNameRegistry {
  /// Returns `true` if the given schema name exists in the registry.
  ///
  /// Checks both original schema names and Rust-converted type names.
  fn contains(&self, name: &str) -> bool {
    self.names.contains(name)
  }

  /// Extends the registry with multiple names.
  fn extend(&mut self, names: impl IntoIterator<Item = String>) {
    self.names.extend(names);
  }
}

pub(crate) struct TypeRegistration {
  pub(crate) assigned_name: String,
  pub(crate) canonical: CanonicalSchema,
  pub(crate) should_register_enum: bool,
  pub(crate) enum_values: Option<Vec<String>>,
}

#[derive(Debug, Clone)]
pub(crate) struct SharedSchemaCache {
  names: NameRegistry,
  schemas: SchemaIdentity,
  enums: EnumRegistry,
  unions: UnionRegistry,
  pub(crate) types: TypeCollector,
  structs: StructIndex,
  type_refs: TypeRefRegistry,
  schema_names: SchemaNameRegistry,
  union_fingerprints: UnionFingerprints,
}

impl SharedSchemaCache {
  /// Creates an empty schema cache with no registered types, names, or precomputed mappings.
  pub(crate) fn new() -> Self {
    Self {
      names: NameRegistry::default(),
      schemas: SchemaIdentity::default(),
      enums: EnumRegistry::default(),
      unions: UnionRegistry::default(),
      types: TypeCollector::default(),
      structs: StructIndex::default(),
      type_refs: TypeRefRegistry::default(),
      schema_names: SchemaNameRegistry::default(),
      union_fingerprints: UnionFingerprints::new(),
    }
  }

  /// Configures precomputed type names for schemas and enums, enabling deterministic
  /// naming that persists across code generation runs.
  pub(crate) fn set_precomputed_names(
    &mut self,
    schema_names: impl IntoIterator<Item = (CanonicalSchema, String)>,
    enum_names: impl IntoIterator<Item = (Vec<String>, String)>,
    schema_metadata: impl IntoIterator<Item = (CanonicalSchema, SchemaPrecomputed)>,
  ) {
    self.schemas.set_precomputed(schema_names, schema_metadata);
    self.enums.set_precomputed(enum_names);
  }

  /// Returns the precomputed enum cache key for this schema if one was computed
  /// during the pre-scan phase.
  pub(crate) fn get_precomputed_enum_cache_key(&self, schema: &ObjectSchema) -> anyhow::Result<Option<Vec<String>>> {
    let canonical = CanonicalSchema::from_schema(schema)?;
    Ok(self.schemas.get_precomputed_enum_cache_key(&canonical).cloned())
  }

  /// Returns the Rust type name assigned to this schema, or `None` if the schema
  /// has not been registered.
  pub(crate) fn get_type_name(&self, schema: &ObjectSchema) -> anyhow::Result<Option<String>> {
    let canonical = CanonicalSchema::from_schema(schema)?;
    Ok(self.schemas.lookup(&canonical).map(String::from))
  }

  /// Returns the type name for an enum with the given values, or `None` if no such
  /// enum has been registered.
  pub(crate) fn get_enum_name(&self, values: &[String]) -> Option<String> {
    self.enums.lookup(values).map(String::from)
  }

  /// Returns `true` if an enum with these values has already been generated
  /// (not just precomputed).
  pub(crate) fn is_enum_generated(&self, values: &[String]) -> bool {
    self.enums.is_registered(values)
  }

  /// Returns the type name for an enum only if it has already been generated.
  ///
  /// Unlike [`get_enum_name`], this excludes precomputed names for enums that
  /// have not yet been emitted to the type collector.
  pub(crate) fn get_generated_enum_name(&self, values: &[String]) -> Option<String> {
    self
      .is_enum_generated(values)
      .then(|| self.get_enum_name(values))
      .flatten()
  }

  /// Records that an enum with the given values has been generated with the
  /// specified type name.
  pub(crate) fn register_enum(&mut self, values: Vec<String>, name: String) {
    self.enums.register(values, name);
  }

  /// Returns the type name for a union with the given variant references and
  /// discriminator, or `None` if no such union has been registered.
  pub(crate) fn get_union_name(&self, refs: &[String], discriminator: Option<&str>) -> Option<String> {
    self.unions.lookup(refs, discriminator).map(String::from)
  }

  /// Records that a union with the given variant references and discriminator has
  /// been generated with the specified type name.
  pub(crate) fn register_union(&mut self, refs: UnionFingerprint, discriminator: Option<String>, name: String) {
    self.unions.register(refs, discriminator, name);
  }

  /// Reserves a type name without associating it with a schema, preventing it from
  /// being assigned to future types.
  pub(crate) fn mark_name_used(&mut self, name: String) {
    self.names.reserve(name);
  }

  /// Registers a top-level schema from `components/schemas` so that inline schemas
  /// with identical structure will reference this type instead of creating duplicates.
  pub(crate) fn register_top_level_schema(&mut self, schema: &ObjectSchema, name: &str) -> anyhow::Result<()> {
    let canonical = CanonicalSchema::from_schema(schema)?;
    let rust_name = to_rust_type_name(name);
    self.schemas.record(canonical, rust_name.clone());
    self.names.reserve(rust_name);
    Ok(())
  }

  /// Returns the precomputed name for this schema if one exists, otherwise generates
  /// a unique name from the base string.
  pub(crate) fn get_preferred_name(&self, schema: &ObjectSchema, base_name: &str) -> anyhow::Result<String> {
    let canonical = CanonicalSchema::from_schema(schema)?;
    if let Some(precomputed) = self.schemas.get_precomputed(&canonical) {
      Ok(precomputed.to_string())
    } else {
      Ok(self.make_unique_name(base_name))
    }
  }

  /// Prepares type registration by checking for enum reuse, resolving name conflicts,
  /// and determining whether this schema needs enum registration.
  ///
  /// The `enum_cache_key` should be provided for schemas with enum values to enable
  /// deduplication across schemas with identical value sets. Does not mutate cache
  /// state; call [`commit_registration`] to finalize.
  pub(crate) fn prepare_registration(
    &self,
    schema: &ObjectSchema,
    base_name: &str,
    enum_cache_key: Option<Vec<String>>,
  ) -> anyhow::Result<TypeRegistration> {
    let canonical = CanonicalSchema::from_schema(schema)?;

    if !schema.is_relaxed_enum_pattern()
      && let Some(ref values) = enum_cache_key
      && let Some(existing_name) = self.enums.lookup(values)
    {
      let should_register_enum = !self.enums.is_registered(values);
      return Ok(TypeRegistration {
        assigned_name: existing_name.to_string(),
        canonical,
        should_register_enum,
        enum_values: should_register_enum.then(|| values.clone()),
      });
    }

    let assigned_name = if let Some(existing_name) = self.schemas.lookup(&canonical) {
      existing_name.to_string()
    } else {
      let preferred = self.schemas.get_precomputed(&canonical);
      self.names.determine_name(preferred, base_name)
    };

    let (should_register_enum, enum_values) = if schema.has_relaxed_anyof_enum() {
      (false, None)
    } else if let Some(values) = enum_cache_key {
      (true, Some(values))
    } else {
      (false, None)
    };

    Ok(TypeRegistration {
      assigned_name,
      canonical,
      should_register_enum,
      enum_values,
    })
  }

  /// Commits a prepared registration by reserving the name, recording schema and
  /// enum mappings, and storing the type definitions.
  pub(crate) fn commit_registration(
    &mut self,
    registration: TypeRegistration,
    nested_types: Vec<RustType>,
    type_def: RustType,
  ) {
    self.names.reserve(registration.assigned_name.clone());
    self
      .schemas
      .record(registration.canonical, registration.assigned_name.clone());

    if registration.should_register_enum
      && let Some(values) = registration.enum_values
    {
      self.enums.register(values, registration.assigned_name.clone());
    }

    self.types.add(nested_types, type_def);
  }

  /// Generates a unique type name from a base string without reserving it.
  ///
  /// The returned name will not conflict with any currently reserved names.
  pub(crate) fn make_unique_name(&self, base: &str) -> String {
    self.names.make_unique(base)
  }

  /// Returns `true` if the given name is already allocated to a structurally different
  /// schema, indicating a naming conflict that requires a unique suffix.
  pub(crate) fn name_conflicts_with_different_schema(&self, name: &str, schema: &ObjectSchema) -> anyhow::Result<bool> {
    if !self.names.is_allocated(name) {
      return Ok(false);
    }
    let canonical = CanonicalSchema::from_schema(schema)?;
    Ok(!self.schemas.has_mapping(&canonical, name))
  }

  /// Takes all accumulated type definitions, leaving the cache empty for reuse.
  pub(crate) fn take_types(&mut self) -> Vec<RustType> {
    self.types.take_types()
  }

  /// Stores a struct definition indexed by type name for later retrieval when
  /// building type references or validating field access.
  pub(crate) fn register_struct_def(&mut self, type_name: &str, struct_def: StructDef) {
    self.structs.register(type_name.to_string(), struct_def);
  }

  /// Returns the struct definition for the given type name, or `None` if not registered.
  pub(crate) fn get_struct_def(&self, type_name: &str) -> Option<&StructDef> {
    self.structs.get(type_name)
  }

  /// Returns the cached TypeRef for this schema, or `None` if not yet resolved.
  pub(crate) fn get_type_ref(&self, schema: &ObjectSchema) -> anyhow::Result<Option<TypeRef>> {
    let canonical = CanonicalSchema::from_schema(schema)?;
    Ok(self.type_refs.lookup(&canonical).cloned())
  }

  /// Caches a resolved TypeRef for this schema to avoid redundant resolution on
  /// subsequent encounters.
  pub(crate) fn register_type_ref(&mut self, schema: &ObjectSchema, type_ref: TypeRef) -> anyhow::Result<()> {
    let canonical = CanonicalSchema::from_schema(schema)?;
    self.type_refs.register(canonical, type_ref);
    Ok(())
  }

  /// Updates the name field of a struct or enum definition to the given name,
  /// leaving other type variants unchanged.
  pub(crate) fn apply_name_to_type(mut type_def: RustType, name: &str) -> RustType {
    match &mut type_def {
      RustType::Struct(s) => s.name = StructToken::from(name.to_string()),
      RustType::Enum(e) => e.name = EnumToken::new(name),
      _ => {}
    }
    type_def
  }

  /// Returns `true` if the given schema name exists in the registry.
  ///
  /// Checks both original schema names and Rust-converted type names.
  pub(crate) fn contains_schema_name(&self, name: &str) -> bool {
    self.schema_names.contains(name)
  }

  /// Initializes the cache with schema names and union fingerprints from the spec.
  ///
  /// This should be called after parsing the OpenAPI spec to populate the cache
  /// with top-level schema names and union deduplication mappings.
  pub(crate) fn initialize_from_schemas(&mut self, schemas: &SchemaMap) {
    let schema_names = schemas
      .keys()
      .flat_map(|schema_name| {
        let rust_name = to_rust_type_name(schema_name);
        vec![schema_name.clone(), rust_name]
      })
      .collect::<Vec<_>>();
    self.schema_names.extend(schema_names);
    self.union_fingerprints = build_union_fingerprints(schemas);
  }

  /// Returns the schema name for a union with the given variant references, or `None` if not registered.
  pub(crate) fn find_union(&self, refs: &[String]) -> Option<&str> {
    self.union_fingerprints.get(refs).map(String::as_str)
  }

  /// Returns a reference to the union fingerprints map.
  pub(crate) fn union_fingerprints(&self) -> &UnionFingerprints {
    &self.union_fingerprints
  }
}