Skip to main content

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    pub description: Option<String>,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct Field {
34    pub name: String,
35    pub field_type: String,
36    pub format: String,
37    pub is_required: bool,
38    pub is_nullable: bool,
39    pub is_array_ref: bool,
40    pub description: Option<String>,
41}
42
43impl Field {
44    /// Returns true if this field should be flattened (for additionalProperties)
45    pub fn should_flatten(&self) -> bool {
46        self.name == "additional_properties"
47    }
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct UnionModel {
52    pub name: String,
53    pub variants: Vec<UnionVariant>,
54    pub union_type: UnionType,
55    pub custom_attrs: Option<Vec<String>>,
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub enum UnionType {
60    OneOf,
61    AnyOf,
62}
63
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct UnionVariant {
66    pub name: String,
67    pub fields: Vec<Field>,
68    pub primitive_type: Option<String>,
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct CompositionModel {
73    pub name: String,
74    pub all_fields: Vec<Field>,
75    pub custom_attrs: Option<Vec<String>>,
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize)]
79pub struct RequestModel {
80    pub name: String,
81    pub content_type: String,
82    pub schema: String,
83    pub is_required: bool,
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct ResponseModel {
88    pub name: String,
89    pub status_code: String,
90    pub content_type: String,
91    pub schema: String,
92    pub description: Option<String>,
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct EnumModel {
97    pub name: String,
98    pub variants: Vec<String>,
99    pub description: Option<String>,
100    pub custom_attrs: Option<Vec<String>>,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct TypeAliasModel {
105    pub name: String,
106    pub target_type: String,
107    pub description: Option<String>,
108    pub custom_attrs: Option<Vec<String>>,
109}