oas3-gen 0.23.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
mod derives;
pub mod lints;
mod outer_attrs;
pub(super) mod serde_attrs;
mod status_codes;
pub mod tokens;
pub(super) mod types;
pub(super) mod validation_attrs;

#[cfg(test)]
mod tests;

use derive_builder::Builder;
pub use derives::{DeriveTrait, DerivesProvider, SerdeImpl};
use http::Method;
pub use lints::LintConfig;
pub use outer_attrs::{OuterAttr, SerdeAsFieldAttr, SerdeAsSeparator};
pub use serde_attrs::SerdeAttribute;
pub use status_codes::{StatusCodeToken, status_code_to_variant_name};
pub use tokens::{
  DefaultAtom, EnumToken, EnumVariantToken, FieldNameToken, MethodNameToken, StructToken, TypeAliasToken,
};
pub use types::{RustPrimitive, TypeRef};
pub use validation_attrs::{RegexKey, ValidationAttribute};

/// Discriminated enum variant mapping
#[derive(Debug, Clone)]
pub struct DiscriminatedVariant {
  pub discriminator_value: String,
  pub variant_name: String,
  pub type_name: TypeRef,
}

/// Serde mode for discriminated enums
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SerdeMode {
  #[default]
  Both,
  SerializeOnly,
  DeserializeOnly,
}

/// Discriminated enum definition (uses macro for custom ser/de)
#[derive(Debug, Clone, Default, Builder)]
#[builder(default, setter(into))]
pub struct DiscriminatedEnumDef {
  pub name: EnumToken,
  pub docs: Vec<String>,
  pub discriminator_field: String,
  pub variants: Vec<DiscriminatedVariant>,
  pub fallback: Option<DiscriminatedVariant>,
  pub serde_mode: SerdeMode,
  pub methods: Vec<EnumMethod>,
}

impl DiscriminatedEnumDef {
  #[must_use]
  pub fn default_variant(&self) -> Option<&DiscriminatedVariant> {
    self.fallback.as_ref().or_else(|| self.variants.first())
  }

  pub fn all_variants(&self) -> impl Iterator<Item = &DiscriminatedVariant> {
    self.variants.iter().chain(self.fallback.as_ref())
  }
}

/// Response enum variant definition
#[derive(Debug, Clone)]
pub struct ResponseVariant {
  pub status_code: StatusCodeToken,
  pub variant_name: EnumVariantToken,
  pub description: Option<String>,
  pub schema_type: Option<TypeRef>,
  pub content_category: ContentCategory,
}

impl ResponseVariant {
  #[must_use]
  pub fn doc_line(&self) -> String {
    match &self.description {
      Some(desc) => format!("{}: {desc}", self.status_code),
      None => self.status_code.to_string(),
    }
  }
}

/// Response enum definition for operation responses
#[derive(Debug, Clone)]
pub struct ResponseEnumDef {
  pub name: EnumToken,
  pub docs: Vec<String>,
  pub variants: Vec<ResponseVariant>,
  pub request_type: Option<StructToken>,
}

/// Top-level Rust type representation
#[derive(Debug, Clone)]
pub enum RustType {
  Struct(StructDef),
  Enum(EnumDef),
  TypeAlias(TypeAliasDef),
  DiscriminatedEnum(DiscriminatedEnumDef),
  ResponseEnum(ResponseEnumDef),
}

impl RustType {
  pub fn type_name(&self) -> DefaultAtom {
    match self {
      RustType::Struct(def) => def.name.to_atom(),
      RustType::Enum(def) => def.name.to_atom(),
      RustType::TypeAlias(def) => def.name.to_atom(),
      RustType::DiscriminatedEnum(def) => def.name.to_atom(),
      RustType::ResponseEnum(def) => def.name.to_atom(),
    }
  }

  #[must_use]
  pub fn is_serializable(&self) -> SerdeImpl {
    match self {
      RustType::Struct(def) => def.is_serializable(),
      RustType::Enum(def) => def.is_serializable(),
      RustType::DiscriminatedEnum(def) => def.is_serializable(),
      RustType::ResponseEnum(def) => def.is_serializable(),
      RustType::TypeAlias(_) => SerdeImpl::None,
    }
  }

  #[must_use]
  pub fn is_deserializable(&self) -> SerdeImpl {
    match self {
      RustType::Struct(def) => def.is_deserializable(),
      RustType::Enum(def) => def.is_deserializable(),
      RustType::DiscriminatedEnum(def) => def.is_deserializable(),
      RustType::ResponseEnum(def) => def.is_deserializable(),
      RustType::TypeAlias(_) => SerdeImpl::None,
    }
  }
}

/// Metadata about an API operation (for tracking, not direct code generation)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OperationKind {
  Http,
  Webhook,
}

#[derive(Debug, Clone)]
pub struct OperationInfo {
  pub stable_id: String,
  pub operation_id: String,
  pub method: Method,
  pub path: ParsedPath,
  pub path_template: String,
  pub kind: OperationKind,
  pub summary: Option<String>,
  pub description: Option<String>,
  pub request_type: Option<StructToken>,
  pub response_type: Option<String>,
  pub response_enum: Option<EnumToken>,
  pub response_content_category: ContentCategory,
  pub success_response_types: Vec<String>,
  pub error_response_types: Vec<String>,
  pub warnings: Vec<String>,
  pub parameters: Vec<OperationParameter>,
  pub body: Option<OperationBody>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ParameterLocation {
  #[default]
  Path,
  Query,
  Header,
  Cookie,
}

#[derive(Debug, Clone, Default)]
pub struct OperationParameter {
  pub original_name: String,
  pub rust_field: FieldNameToken,
  pub location: ParameterLocation,
  pub required: bool,
  pub rust_type: TypeRef,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum ContentCategory {
  #[default]
  Json,
  FormUrlEncoded,
  Multipart,
  Text,
  Binary,
  Xml,
}

impl ContentCategory {
  #[must_use]
  pub fn from_content_type(content_type: &str) -> Self {
    let ct = content_type.to_ascii_lowercase();
    if ct.contains("json") {
      Self::Json
    } else if ct.contains("x-www-form-urlencoded") {
      Self::FormUrlEncoded
    } else if ct.contains("multipart") {
      Self::Multipart
    } else if ct.contains("text/plain") || ct.contains("text/html") {
      Self::Text
    } else if ct.contains("xml") {
      Self::Xml
    } else if ct.contains("octet-stream")
      || ct.starts_with("image/")
      || ct.starts_with("video/")
      || ct.starts_with("audio/")
      || ct.starts_with("application/pdf")
      || (ct.starts_with("application/") && !ct.contains("json"))
    {
      Self::Binary
    } else {
      Self::Json
    }
  }
}

#[derive(Debug, Clone, Default)]
pub struct OperationBody {
  pub field_name: FieldNameToken,
  pub optional: bool,
  pub content_category: ContentCategory,
}

#[derive(Debug, Clone)]
pub enum PathSegment {
  Literal(String),
  Param(FieldNameToken),
  Mixed {
    format: String,
    params: Vec<FieldNameToken>,
  },
}

impl PathSegment {
  fn parse(segment: &str, params: &std::collections::HashMap<&str, &FieldNameToken>) -> Self {
    if !segment.contains('{') {
      return Self::Literal(segment.to_string());
    }

    if let Some(name) = segment.strip_prefix('{').and_then(|s| s.strip_suffix('}'))
      && let Some(field) = params.get(name)
    {
      return Self::Param((*field).clone());
    }

    let mut format_str = String::new();
    let mut field_params = Vec::new();
    let mut rest = segment;

    while let Some(start) = rest.find('{') {
      format_str.push_str(&rest[..start]);

      let end = rest[start..].find('}').map_or(rest.len(), |i| start + i);
      let name = &rest[start + 1..end];

      if let Some(field) = params.get(name) {
        format_str.push_str("{}");
        field_params.push((*field).clone());
      } else {
        format_str.push_str(&rest[start..=end]);
      }

      rest = &rest[end + 1..];
    }
    format_str.push_str(rest);

    Self::Mixed {
      format: format_str,
      params: field_params,
    }
  }
}

impl quote::ToTokens for PathSegment {
  fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
    use quote::quote;
    let segment_tokens = match self {
      PathSegment::Literal(lit) => quote! { .push(#lit) },
      PathSegment::Param(field) => quote! { .push(&request.path.#field.to_string()) },
      PathSegment::Mixed { format, params } => {
        let args = params.iter().map(|f| quote! { request.path.#f });
        quote! { .push(&format!(#format, #(#args),*)) }
      }
    };
    quote::ToTokens::to_tokens(&segment_tokens, tokens);
  }
}

#[derive(Debug, Clone, Default)]
pub struct ParsedPath(pub Vec<PathSegment>);

impl ParsedPath {
  pub fn new(path: &str, parameters: &[OperationParameter]) -> Self {
    let param_map: std::collections::HashMap<&str, &FieldNameToken> = parameters
      .iter()
      .filter(|p| matches!(p.location, ParameterLocation::Path))
      .map(|p| (p.original_name.as_str(), &p.rust_field))
      .collect();

    let segments = path
      .trim_start_matches('/')
      .split('/')
      .filter(|s| !s.is_empty())
      .map(|segment| PathSegment::parse(segment, &param_map))
      .collect();

    Self(segments)
  }
}

/// Semantic kind of a struct to determine code generation behavior
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StructKind {
  /// Regular OpenAPI schema struct from components.schemas (includes inline request/response bodies)
  #[default]
  Schema,
  /// Operation request struct combining parameters and body (has `render_path` method)
  OperationRequest,
  /// Nested struct for path parameters (no serde, just storage)
  PathParams,
  /// Nested struct for query parameters (implements Serialize for reqwest's .query())
  QueryParams,
  /// Nested struct for header parameters (no serde, just storage)
  HeaderParams,
}

/// Rust struct definition
#[derive(Debug, Clone, Default)]
pub struct StructDef {
  pub name: StructToken,
  pub docs: Vec<String>,
  pub fields: Vec<FieldDef>,
  pub serde_attrs: Vec<SerdeAttribute>,
  pub outer_attrs: Vec<OuterAttr>,
  pub methods: Vec<StructMethod>,
  pub kind: StructKind,
  pub serde_mode: SerdeMode,
}

impl StructDef {
  #[must_use]
  pub fn has_default(&self) -> bool {
    self.derives().contains(&DeriveTrait::Default)
  }

  #[must_use]
  pub fn has_validation_attrs(&self) -> bool {
    self.fields.iter().any(|f| !f.validation_attrs.is_empty())
  }

  pub fn required_fields(&self) -> impl Iterator<Item = &FieldDef> {
    self.fields.iter().filter(|f| f.is_required())
  }
}

/// Associated method definition for a struct
#[derive(Debug, Clone)]
pub struct StructMethod {
  pub name: MethodNameToken,
  pub docs: Vec<String>,
  pub kind: StructMethodKind,
}

#[derive(Debug, Clone)]
pub enum StructMethodKind {
  ParseResponse {
    response_enum: EnumToken,
    variants: Vec<ResponseVariant>,
  },
}

/// Associated method definition for an enum
#[derive(Debug, Clone)]
pub struct EnumMethod {
  pub name: MethodNameToken,
  pub docs: Vec<String>,
  pub kind: EnumMethodKind,
}

impl EnumMethod {
  pub fn new(name: impl Into<MethodNameToken>, kind: EnumMethodKind, docs: Vec<String>) -> Self {
    Self {
      name: name.into(),
      docs,
      kind,
    }
  }
}

#[derive(Debug, Clone)]
pub enum EnumMethodKind {
  SimpleConstructor {
    variant_name: EnumVariantToken,
    wrapped_type: TypeRef,
  },
  ParameterizedConstructor {
    variant_name: EnumVariantToken,
    wrapped_type: TypeRef,
    param_name: String,
    param_type: TypeRef,
  },
}

/// Rust struct field definition
#[derive(Debug, Clone, Default, Builder)]
#[builder(default, setter(into))]
pub struct FieldDef {
  pub name: FieldNameToken,
  pub docs: Vec<String>,
  pub rust_type: TypeRef,
  pub serde_attrs: Vec<SerdeAttribute>,
  pub serde_as_attr: Option<SerdeAsFieldAttr>,
  /// Whether to emit `#[doc(hidden)]` for this field (used for discriminator fields)
  pub doc_hidden: bool,
  pub validation_attrs: Vec<ValidationAttribute>,
  pub default_value: Option<serde_json::Value>,
  pub example_value: Option<serde_json::Value>,
  pub parameter_location: Option<ParameterLocation>,
  pub deprecated: bool,
  pub multiple_of: Option<serde_json::Number>,
}

impl FieldDef {
  #[must_use]
  pub fn is_required(&self) -> bool {
    self.default_value.is_none() && !self.rust_type.nullable
  }
}

/// Rust enum definition
#[derive(Debug, Clone, Default)]
pub struct EnumDef {
  pub name: EnumToken,
  pub docs: Vec<String>,
  pub variants: Vec<VariantDef>,
  pub discriminator: Option<String>,
  pub serde_attrs: Vec<SerdeAttribute>,
  pub outer_attrs: Vec<OuterAttr>,
  pub case_insensitive: bool,
  pub methods: Vec<EnumMethod>,
  pub serde_mode: SerdeMode,
}

impl EnumDef {
  #[must_use]
  pub fn fallback_variant(&self) -> Option<&VariantDef> {
    const FALLBACK_NAMES: &[&str] = &["Unknown", "Other"];
    self.variants.iter().find(|v| FALLBACK_NAMES.contains(&v.name.as_str()))
  }
}

/// Rust enum variant definition
#[derive(Debug, Clone, Default)]
pub struct VariantDef {
  pub name: EnumVariantToken,
  pub docs: Vec<String>,
  pub content: VariantContent,
  pub serde_attrs: Vec<SerdeAttribute>,
  pub deprecated: bool,
}

impl VariantDef {
  #[must_use]
  pub fn serde_name(&self) -> String {
    self
      .serde_attrs
      .iter()
      .find_map(|attr| match attr {
        SerdeAttribute::Rename(val) => Some(val.clone()),
        _ => None,
      })
      .unwrap_or_else(|| self.name.to_string())
  }

  #[must_use]
  pub fn single_wrapped_type(&self) -> Option<&TypeRef> {
    self.content.single_type()
  }

  #[must_use]
  pub fn unboxed_type_name(&self) -> Option<String> {
    self.content.single_type().map(TypeRef::unboxed_base_type_name)
  }
}

/// Enum variant content (Unit, Tuple, or Struct)
#[derive(Debug, Clone, Default)]
pub enum VariantContent {
  #[default]
  Unit,
  Tuple(Vec<TypeRef>),
}

impl VariantContent {
  #[must_use]
  pub fn tuple_types(&self) -> Option<&[TypeRef]> {
    match self {
      Self::Unit => None,
      Self::Tuple(types) => Some(types),
    }
  }

  #[must_use]
  pub fn single_type(&self) -> Option<&TypeRef> {
    match self {
      Self::Tuple(types) if types.len() == 1 => Some(&types[0]),
      _ => None,
    }
  }
}

/// Type alias definition
#[derive(Debug, Clone, Default)]
pub struct TypeAliasDef {
  pub name: TypeAliasToken,
  pub docs: Vec<String>,
  pub target: TypeRef,
}