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
use okapi::openapi3::{Components, OpenApi, SchemaObject};
use okapi::schemars::schema::{InstanceType, Schema, SingleOrVec};
use serde_json::Value;

fn recurse_fix(mut v: &mut Value) {
    match &mut v {
        Value::Array(array) => {
            for item in array {
                recurse_fix(item);
            }
        }
        Value::Object(obj) => {
            for item in obj.iter_mut() {
                recurse_fix(item.1);
            }

            if !obj.contains_key("examples") {
                if let Some(ex) = obj.get("example").cloned() {
                    obj.insert("examples".to_string(), Value::Array(vec![ex]));
                }
            }
        }

        _ => {}
    }
}

/// Parse OpenAPI 3 schema, treating "example" field as "examples"
///
/// Otherwise, examples would be lost
pub fn parse_schema_fix_example_issue(file_content: &str) -> OpenApi {
    let mut root = serde_yaml::from_str::<Value>(file_content)
        .expect("Failed to parse OpenAPI document as YAML document!");

    if let Value::Object(root_obj) = &mut root {
        if let Some(components) = root_obj.get_mut("components") {
            recurse_fix(components);
        }
    }

    parse_schema(&serde_yaml::to_string(&root).unwrap())
}

/// Parse OpenAPI 3 schema
pub fn parse_schema(file_content: &str) -> OpenApi {
    let schema = serde_yaml::from_str::<OpenApi>(file_content).expect("Failed to parse document");

    if schema.components.is_none() {
        log::error!("components is missing!");
        panic!()
    }

    schema
}

fn expect_single<E>(e: &SingleOrVec<E>) -> &E {
    match e {
        SingleOrVec::Single(e) => e,
        SingleOrVec::Vec(v) => &v[0],
    }
}

fn expect_schema_object(s: &Schema) -> &SchemaObject {
    match s {
        Schema::Bool(_) => {
            panic!("Got unexpected bool!");
        }
        Schema::Object(o) => o,
    }
}

/// Object child information
#[derive(Debug, Clone, serde::Serialize)]
pub struct ObjectChild {
    /// The name of the field in the object
    pub name: String,
    /// The structure of the child
    pub node: TreeNode,
}

/// The type of the schema
#[derive(Debug, Clone, serde::Serialize)]
#[serde(tag = "type")]
pub enum NodeType {
    /// NULL value
    Null,
    /// BOOLEAN value
    Boolean,
    /// Array value
    Array {
        /// Schema information about the children of the array
        item: Box<TreeNode>,
    },
    /// Object value
    Object {
        /// Required filed for the object
        required: Option<Vec<String>>,
        /// The children of the object
        children: Vec<ObjectChild>,
    },
    /// String value
    String,
    /// Number value
    Number,
    /// Integer value
    Integer,
}

impl NodeType {
    /// Specify if the type of the node allow children
    pub fn can_have_children(&self) -> bool {
        matches!(self, NodeType::Object { .. } | NodeType::Array { .. })
    }

    /// Get a short symbol representing the type of value
    pub fn symbol(&self) -> &'static str {
        match self {
            NodeType::Null => "NULL",
            NodeType::Boolean => "Bool",
            NodeType::Array { .. } => "[]",
            NodeType::Object { .. } => "{}",
            NodeType::String => "str",
            NodeType::Number => "num",
            NodeType::Integer => "int",
        }
    }
}

/// Parser schema structure node
#[derive(Debug, Clone, serde::Serialize)]
pub struct TreeNode {
    /// The name of the schema
    pub name: String,
    /// The type of the schema
    #[serde(flatten)]
    pub r#type: NodeType,
    /// The description provided for the schema, if available
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Examples values for the schema, if available
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub examples: Vec<String>,
    /// Discrete values of the schema, if specified
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#enum: Option<Vec<String>>,
}

impl TreeNode {
    /// Get the name of the schema, alongside with a symbol
    /// indicating its type
    pub fn print_name(&self) -> String {
        format!("{} {}", self.name, self.r#type.symbol())
    }

    /// Merge two TreeNode
    pub fn merge_with(self, other: Self) -> Self {
        if !matches!(self.r#type, NodeType::String | NodeType::Object { .. }) {
            panic!("Cannot merge!");
        }

        if !matches!(other.r#type, NodeType::String | NodeType::Object { .. }) {
            panic!("Cannot merge other!");
        }

        let r#type = match (self.r#type, other.r#type) {
            (NodeType::String, NodeType::String) => NodeType::String,
            (NodeType::String, NodeType::Object { children, required })
            | (NodeType::Object { children, required }, NodeType::String) => {
                NodeType::Object { children, required }
            }

            (
                NodeType::Object {
                    children: c1,
                    required: r1,
                },
                NodeType::Object {
                    children: mut c2,
                    required: r2,
                },
            ) => {
                let mut children = c1;
                children.append(&mut c2);

                let mut required = r1.unwrap_or_default();
                required.append(&mut r2.unwrap_or_default());

                NodeType::Object {
                    children,
                    required: match required.is_empty() {
                        true => None,
                        false => Some(required),
                    },
                }
            }

            (_, _) => unreachable!(),
        };

        TreeNode {
            name: self.name.to_string(),
            r#type,
            description: other.description.or(self.description),
            examples: match other.examples.is_empty() {
                true => self.examples,
                false => other.examples,
            },
            r#enum: other.r#enum.or(self.r#enum),
        }
    }
}

/// Construct the tree of a given structure name
pub fn build_tree(struct_name: &str, components: &Components) -> TreeNode {
    let schema = components
        .schemas
        .get(struct_name)
        .unwrap_or_else(|| panic!("Missing {struct_name}"));

    build_tree_schema(schema, struct_name, components)
}

/// Build a structure tree using a schema
fn build_tree_schema(
    schema: &SchemaObject,
    struct_name: &str,
    components: &Components,
) -> TreeNode {
    if let Some(name) = &schema.reference {
        return build_tree(
            name.strip_prefix("#/components/schemas/").unwrap(),
            components,
        );
    }

    if let Some(subschemas) = &schema.subschemas {
        if let Some(all_of) = &subschemas.all_of {
            assert!(!all_of.is_empty());
            let mut tree =
                build_tree_schema(expect_schema_object(&all_of[0]), struct_name, components);

            for other in all_of.iter().skip(1) {
                let other = build_tree_schema(expect_schema_object(other), struct_name, components);
                tree = tree.merge_with(other);
            }

            tree.name = struct_name.to_string();
            return tree;
        } else {
            panic!("Unsupported case!");
        }
    }

    let schema_type = schema
        .instance_type
        .as_ref()
        .map(expect_single)
        .unwrap_or(&InstanceType::String);

    let r#type = match schema_type {
        InstanceType::Null => NodeType::Null,
        InstanceType::Boolean => NodeType::Boolean,
        InstanceType::Object => {
            let object = schema.object.as_ref();
            let children = object
                .map(|s| s.properties.clone())
                .unwrap_or_default()
                .iter()
                .map(|e| {
                    let o = expect_schema_object(e.1);
                    ObjectChild {
                        name: e.0.to_string(),
                        node: build_tree_schema(o, e.0, components),
                    }
                })
                .collect::<Vec<_>>();

            let required = object
                .as_ref()
                .map(|o| &o.required)
                .map(|r| r.iter().map(|s| s.to_string()).collect());

            NodeType::Object { children, required }
        }
        InstanceType::Array => {
            let item = expect_schema_object(expect_single(
                schema.array.as_ref().unwrap().items.as_ref().unwrap(),
            ));
            NodeType::Array {
                item: Box::new(build_tree_schema(
                    item,
                    &format!("{struct_name}[]"),
                    components,
                )),
            }
        }
        InstanceType::Number => NodeType::Number,
        InstanceType::String => NodeType::String,
        InstanceType::Integer => NodeType::Integer,
    };

    let metadata = schema.metadata.clone().unwrap_or_default();

    TreeNode {
        name: struct_name.to_string(),
        r#type,
        description: metadata.description,
        examples: metadata
            .examples
            .iter()
            .map(|v| v.to_string())
            .collect::<Vec<_>>(),
        r#enum: schema
            .enum_values
            .as_ref()
            .map(|v| v.iter().map(|v| v.to_string()).collect()),
    }
}