oxidite-openapi 2.3.3

OpenAPI/Swagger documentation generation for Oxidite
Documentation
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
540
541
542
543
544
545
546
547
548
549
550
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// OpenAPI 3.0 Specification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenApiSpec {
    pub openapi: String,
    pub info: Info,
    pub paths: HashMap<String, PathItem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub components: Option<Components>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub servers: Option<Vec<Server>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Info {
    pub title: String,
    pub version: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Server {
    pub url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PathItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub get: Option<Operation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub post: Option<Operation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub put: Option<Operation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delete: Option<Operation>,
}

impl PathItem {
    pub fn with_get(mut self, operation: Operation) -> Self {
        self.get = Some(operation);
        self
    }

    pub fn with_post(mut self, operation: Operation) -> Self {
        self.post = Some(operation);
        self
    }

    pub fn with_put(mut self, operation: Operation) -> Self {
        self.put = Some(operation);
        self
    }

    pub fn with_delete(mut self, operation: Operation) -> Self {
        self.delete = Some(operation);
        self
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Operation {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parameters: Option<Vec<Parameter>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_body: Option<RequestBody>,
    pub responses: HashMap<String, Response>,
}

impl Operation {
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    pub fn add_tag(mut self, tag: impl Into<String>) -> Self {
        let tags = self.tags.get_or_insert_with(Vec::new);
        tags.push(tag.into());
        self
    }

    pub fn add_parameter(mut self, parameter: Parameter) -> Self {
        let parameters = self.parameters.get_or_insert_with(Vec::new);
        parameters.push(parameter);
        self
    }

    pub fn with_request_body(mut self, request_body: RequestBody) -> Self {
        self.request_body = Some(request_body);
        self
    }

    pub fn add_response(mut self, status_code: impl Into<String>, response: Response) -> Self {
        self.responses.insert(status_code.into(), response);
        self
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ParameterLocation {
    Query,
    Path,
    Header,
    Cookie,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Parameter {
    pub name: String,
    #[serde(rename = "in")]
    pub location: String, // "query", "path", "header", "cookie"
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<bool>,
    pub schema: Schema,
}

impl Parameter {
    pub fn new(
        name: impl Into<String>,
        location: ParameterLocation,
        schema: Schema,
    ) -> Self {
        let location = match location {
            ParameterLocation::Query => "query",
            ParameterLocation::Path => "path",
            ParameterLocation::Header => "header",
            ParameterLocation::Cookie => "cookie",
        }
        .to_string();

        Self {
            name: name.into(),
            location,
            description: None,
            required: None,
            schema,
        }
    }

    pub fn query(name: impl Into<String>, schema: Schema) -> Self {
        Self::new(name, ParameterLocation::Query, schema)
    }

    pub fn path(name: impl Into<String>, schema: Schema) -> Self {
        let mut p = Self::new(name, ParameterLocation::Path, schema);
        p.required = Some(true);
        p
    }

    pub fn header(name: impl Into<String>, schema: Schema) -> Self {
        Self::new(name, ParameterLocation::Header, schema)
    }

    pub fn cookie(name: impl Into<String>, schema: Schema) -> Self {
        Self::new(name, ParameterLocation::Cookie, schema)
    }

    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    pub fn required(mut self, required: bool) -> Self {
        self.required = Some(required);
        self
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestBody {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    pub required: bool,
    pub content: HashMap<String, MediaType>,
}

impl RequestBody {
    pub fn json(schema: Schema) -> Self {
        let mut content = HashMap::new();
        content.insert("application/json".to_string(), MediaType { schema });
        Self {
            description: None,
            required: true,
            content,
        }
    }

    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    pub fn required(mut self, required: bool) -> Self {
        self.required = required;
        self
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Response {
    pub description: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<HashMap<String, MediaType>>,
}

impl Response {
    pub fn new(description: impl Into<String>) -> Self {
        Self {
            description: description.into(),
            content: None,
        }
    }

    pub fn json(description: impl Into<String>, schema: Schema) -> Self {
        let mut content = HashMap::new();
        content.insert("application/json".to_string(), MediaType { schema });
        Self {
            description: description.into(),
            content: Some(content),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MediaType {
    pub schema: Schema,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Schema {
    Simple {
        #[serde(rename = "type")]
        type_name: String,
    },
    Object {
        #[serde(rename = "type")]
        type_name: String,
        properties: HashMap<String, Box<Schema>>,
    },
    Array {
        #[serde(rename = "type")]
        type_name: String,
        items: Box<Schema>,
    },
}

impl Schema {
    pub fn string() -> Self {
        Self::Simple {
            type_name: "string".to_string(),
        }
    }

    pub fn integer() -> Self {
        Self::Simple {
            type_name: "integer".to_string(),
        }
    }

    pub fn number() -> Self {
        Self::Simple {
            type_name: "number".to_string(),
        }
    }

    pub fn boolean() -> Self {
        Self::Simple {
            type_name: "boolean".to_string(),
        }
    }

    pub fn object(properties: HashMap<String, Schema>) -> Self {
        Self::Object {
            type_name: "object".to_string(),
            properties: properties
                .into_iter()
                .map(|(k, v)| (k, Box::new(v)))
                .collect(),
        }
    }

    pub fn array(items: Schema) -> Self {
        Self::Array {
            type_name: "array".to_string(),
            items: Box::new(items),
        }
    }
}

/// Lightweight schema inference trait for common Rust types.
pub trait ToSchema {
    fn schema() -> Schema;
}

impl ToSchema for String {
    fn schema() -> Schema {
        Schema::string()
    }
}
impl ToSchema for bool {
    fn schema() -> Schema {
        Schema::boolean()
    }
}
impl ToSchema for i32 {
    fn schema() -> Schema {
        Schema::integer()
    }
}
impl ToSchema for i64 {
    fn schema() -> Schema {
        Schema::integer()
    }
}
impl ToSchema for u32 {
    fn schema() -> Schema {
        Schema::integer()
    }
}
impl ToSchema for u64 {
    fn schema() -> Schema {
        Schema::integer()
    }
}
impl ToSchema for f32 {
    fn schema() -> Schema {
        Schema::number()
    }
}
impl ToSchema for f64 {
    fn schema() -> Schema {
        Schema::number()
    }
}
impl<T: ToSchema> ToSchema for Vec<T> {
    fn schema() -> Schema {
        Schema::array(T::schema())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Components {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schemas: Option<HashMap<String, Schema>>,
}

/// OpenAPI Documentation Builder
pub struct OpenApiBuilder {
    spec: OpenApiSpec,
}

impl OpenApiBuilder {
    pub fn new(title: impl Into<String>, version: impl Into<String>) -> Self {
        Self {
            spec: OpenApiSpec {
                openapi: "3.0.0".to_string(),
                info: Info {
                    title: title.into(),
                    version: version.into(),
                    description: None,
                },
                paths: HashMap::new(),
                components: None,
                servers: None,
            },
        }
    }

    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.spec.info.description = Some(desc.into());
        self
    }

    pub fn server(mut self, url: impl Into<String>, description: Option<String>) -> Self {
        let servers = self.spec.servers.get_or_insert_with(Vec::new);
        servers.push(Server {
            url: url.into(),
            description,
        });
        self
    }

    pub fn path(mut self, path: impl Into<String>, item: PathItem) -> Self {
        self.spec.paths.insert(path.into(), item);
        self
    }

    pub fn build(self) -> OpenApiSpec {
        self.spec
    }

    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(&self.spec)
    }
}

/// Helper to create a GET operation
pub fn get_operation(summary: impl Into<String>) -> Operation {
    Operation {
        summary: Some(summary.into()),
        description: None,
        tags: None,
        parameters: None,
        request_body: None,
        responses: HashMap::new(),
    }
}

/// Helper to create a POST operation
pub fn post_operation(summary: impl Into<String>) -> Operation {
    Operation {
        summary: Some(summary.into()),
        description: None,
        tags: None,
        parameters: None,
        request_body: None,
        responses: HashMap::new(),
    }
}

/// Auto-documentation trait for routers
pub trait AutoDocs {
    /// Register the /api/docs endpoint with OpenAPI documentation
    fn with_auto_docs(self, spec: OpenApiSpec) -> Self;
}

impl AutoDocs for oxidite_core::Router {
    fn with_auto_docs(mut self, spec: OpenApiSpec) -> Self {
        let spec_arc = std::sync::Arc::new(spec);

        let spec_json = spec_arc.clone();
        self.get("/openapi.json", move || {
            let spec_json = spec_json.clone();
            async move { Ok(oxidite_core::OxiditeResponse::json((*spec_json).clone())) }
        });

        let spec_docs = spec_arc.clone();
        self.get("/api/docs", move || {
            let spec_docs = spec_docs.clone();
            async move {
                Ok(oxidite_core::OxiditeResponse::html(generate_docs_html(
                    &spec_docs,
                )))
            }
        });

        self
    }
}

/// Generate HTML documentation page
pub fn generate_docs_html(spec: &OpenApiSpec) -> String {
    let spec_json = serde_json::to_string_pretty(spec).unwrap_or_else(|_| "{}".to_string());
    let safe_title = html_escape(&spec.info.title);
    let safe_spec_json = spec_json.replace("</script>", "<\\/script>");
    
    format!(r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{} - API Documentation</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css">
</head>
<body>
    <div id="swagger-ui"></div>
    <script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
    <script>
        const spec = {};
        SwaggerUIBundle({{
            spec: spec,
            dom_id: '#swagger-ui',
            deepLinking: true,
            presets: [
                SwaggerUIBundle.presets.apis,
                SwaggerUIBundle.SwaggerUIStandalonePreset
            ],
        }});
    </script>
</body>
</html>"#, safe_title, safe_spec_json)
}

fn html_escape(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&#x27;")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_openapi_builder() {
        let spec = OpenApiBuilder::new("Test API", "1.0.0")
            .description("A test API")
            .server("http://localhost:8080", Some("Local server".to_string()))
            .build();

        assert_eq!(spec.info.title, "Test API");
        assert_eq!(spec.info.version, "1.0.0");
    }

    #[test]
    fn test_operation_builder_helpers() {
        let operation = get_operation("Get users")
            .with_description("Return users")
            .add_tag("users")
            .add_parameter(Parameter::query("page", Schema::integer()).required(false))
            .add_response("200", Response::json("ok", Schema::array(Schema::string())));

        assert_eq!(operation.summary.as_deref(), Some("Get users"));
        assert_eq!(operation.tags.as_ref().map(Vec::len), Some(1));
        assert!(operation.responses.contains_key("200"));
    }

    #[test]
    fn test_generate_docs_html_escapes_title() {
        let spec = OpenApiBuilder::new("<script>x</script>", "1.0.0").build();
        let html = generate_docs_html(&spec);
        assert!(html.contains("&lt;script&gt;x&lt;/script&gt;"));
        assert!(!html.contains("<title><script>x</script>"));
    }

    #[test]
    fn to_schema_infers_basic_types() {
        let string_schema = <String as ToSchema>::schema();
        let vec_schema = <Vec<i32> as ToSchema>::schema();
        assert!(matches!(string_schema, Schema::Simple { .. }));
        assert!(matches!(vec_schema, Schema::Array { .. }));
    }
}