openapi_model_generator/
models.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub enum ModelType {
5    Struct(Model),
6    Union(UnionModel),             // oneOf/anyOf -> enum
7    Composition(CompositionModel), // allOf
8    Enum(EnumModel),               // enum values -> enum
9    TypeAlias(TypeAliasModel),     // x-rust-type -> type alias
10}
11
12impl ModelType {
13    pub fn name(&self) -> &str {
14        match self {
15            ModelType::Struct(m) => &m.name,
16            ModelType::Enum(e) => &e.name,
17            ModelType::Union(u) => &u.name,
18            ModelType::Composition(c) => &c.name,
19            ModelType::TypeAlias(t) => &t.name,
20        }
21    }
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct Model {
26    pub name: String,
27    pub fields: Vec<Field>,
28    pub custom_attrs: Option<Vec<String>>,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct Field {
33    pub name: String,
34    pub field_type: String,
35    pub format: String,
36    pub is_required: bool,
37    pub is_nullable: bool,
38}
39
40impl Field {
41    /// Returns true if this field should be flattened (for additionalProperties)
42    pub fn should_flatten(&self) -> bool {
43        self.name == "additional_properties"
44    }
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct UnionModel {
49    pub name: String,
50    pub variants: Vec<UnionVariant>,
51    pub union_type: UnionType,
52    pub custom_attrs: Option<Vec<String>>,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub enum UnionType {
57    OneOf,
58    AnyOf,
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct UnionVariant {
63    pub name: String,
64    pub fields: Vec<Field>,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct CompositionModel {
69    pub name: String,
70    pub all_fields: Vec<Field>,
71    pub custom_attrs: Option<Vec<String>>,
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize)]
75pub struct RequestModel {
76    pub name: String,
77    pub content_type: String,
78    pub schema: String,
79    pub is_required: bool,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct ResponseModel {
84    pub name: String,
85    pub status_code: String,
86    pub content_type: String,
87    pub schema: String,
88    pub description: Option<String>,
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct EnumModel {
93    pub name: String,
94    pub variants: Vec<String>,
95    pub description: Option<String>,
96    pub custom_attrs: Option<Vec<String>>,
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct TypeAliasModel {
101    pub name: String,
102    pub target_type: String,
103    pub description: Option<String>,
104    pub custom_attrs: Option<Vec<String>>,
105}