rustapi-openapi 0.1.450

Native OpenAPI 3.1 specification generator for RustAPI. Integrates Swagger UI.
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
//! OpenAPI 3.1 specification types

use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashSet};

use crate::schema::JsonSchema2020;
pub use crate::schema::SchemaRef;

/// OpenAPI 3.1.0 specification
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OpenApiSpec {
    /// OpenAPI version (always "3.1.0")
    pub openapi: String,

    /// API information
    pub info: ApiInfo,

    /// JSON Schema dialect (optional, defaults to JSON Schema 2020-12)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub json_schema_dialect: Option<String>,

    /// Server list
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub servers: Vec<Server>,

    /// API paths
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub paths: BTreeMap<String, PathItem>,

    /// Webhooks
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub webhooks: BTreeMap<String, PathItem>,

    /// Components
    #[serde(skip_serializing_if = "Option::is_none")]
    pub components: Option<Components>,

    /// Security requirements
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub security: Vec<BTreeMap<String, Vec<String>>>,

    /// Tags
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<Tag>,

    /// External documentation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub external_docs: Option<ExternalDocs>,
}

impl OpenApiSpec {
    pub fn new(title: impl Into<String>, version: impl Into<String>) -> Self {
        Self {
            openapi: "3.1.0".to_string(),
            info: ApiInfo {
                title: title.into(),
                version: version.into(),
                ..Default::default()
            },
            json_schema_dialect: Some("https://spec.openapis.org/oas/3.1/dialect/base".to_string()),
            servers: Vec::new(),
            paths: BTreeMap::new(),
            webhooks: BTreeMap::new(),
            components: None,
            security: Vec::new(),
            tags: Vec::new(),
            external_docs: None,
        }
    }

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

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

    pub fn path(mut self, path: &str, method: &str, operation: Operation) -> Self {
        let item = self.paths.entry(path.to_string()).or_default();
        match method.to_uppercase().as_str() {
            "GET" => item.get = Some(operation),
            "POST" => item.post = Some(operation),
            "PUT" => item.put = Some(operation),
            "PATCH" => item.patch = Some(operation),
            "DELETE" => item.delete = Some(operation),
            "HEAD" => item.head = Some(operation),
            "OPTIONS" => item.options = Some(operation),
            "TRACE" => item.trace = Some(operation),
            _ => {}
        }
        self
    }

    /// Register a type that implements RustApiSchema
    pub fn register<T: crate::schema::RustApiSchema>(mut self) -> Self {
        self.register_in_place::<T>();
        self
    }

    /// Register a type into this spec in-place.
    pub fn register_in_place<T: crate::schema::RustApiSchema>(&mut self) {
        let mut ctx = crate::schema::SchemaCtx::new();

        // Pre-load existing schemas to avoid re-generating or to handle deduplication correctly
        if let Some(c) = &self.components {
            ctx.components = c.schemas.clone();
        }

        // Generate schema for T (and dependencies)
        let _ = T::schema(&mut ctx);

        // Merge back into components
        let components = self.components.get_or_insert_with(Components::default);
        for (name, schema) in ctx.components {
            if let Some(existing) = components.schemas.get(&name) {
                if existing != &schema {
                    panic!("Schema collision detected for component '{}'. Existing schema differs from new schema. This usually means two different types are mapped to the same component name. Please implement `RustApiSchema::name()` or alias the type.", name);
                }
            } else {
                components.schemas.insert(name, schema);
            }
        }
    }

    pub fn server(mut self, server: Server) -> Self {
        self.servers.push(server);
        self
    }

    pub fn security_scheme(mut self, name: impl Into<String>, scheme: SecurityScheme) -> Self {
        let components = self.components.get_or_insert_with(Components::default);
        components
            .security_schemes
            .entry(name.into())
            .or_insert(scheme);
        self
    }

    pub fn to_json(&self) -> serde_json::Value {
        serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
    }

    /// Validate that all $ref references point to existing components.
    /// Returns Ok(()) if valid, or a list of missing references.
    pub fn validate_integrity(&self) -> Result<(), Vec<String>> {
        let mut defined_schemas = HashSet::new();
        if let Some(components) = &self.components {
            for key in components.schemas.keys() {
                defined_schemas.insert(format!("#/components/schemas/{}", key));
            }
        }

        let mut missing_refs = Vec::new();

        // Helper to check a single ref
        let mut check_ref = |r: &str| {
            if r.starts_with("#/components/schemas/") && !defined_schemas.contains(r) {
                missing_refs.push(r.to_string());
            }
            // Ignore other refs for now (e.g. external or non-schema refs)
        };

        // 1. Visit Paths
        for path_item in self.paths.values() {
            visit_path_item(path_item, &mut |s| visit_schema_ref(s, &mut check_ref));
        }

        // 2. Visit Webhooks
        for path_item in self.webhooks.values() {
            visit_path_item(path_item, &mut |s| visit_schema_ref(s, &mut check_ref));
        }

        // 3. Visit Components
        if let Some(components) = &self.components {
            for schema in components.schemas.values() {
                visit_json_schema(schema, &mut check_ref);
            }
            for resp in components.responses.values() {
                visit_response(resp, &mut |s| visit_schema_ref(s, &mut check_ref));
            }
            for param in components.parameters.values() {
                visit_parameter(param, &mut |s| visit_schema_ref(s, &mut check_ref));
            }
            for body in components.request_bodies.values() {
                visit_request_body(body, &mut |s| visit_schema_ref(s, &mut check_ref));
            }
            for header in components.headers.values() {
                visit_header(header, &mut |s| visit_schema_ref(s, &mut check_ref));
            }
            for callback_map in components.callbacks.values() {
                for item in callback_map.values() {
                    visit_path_item(item, &mut |s| visit_schema_ref(s, &mut check_ref));
                }
            }
        }

        if missing_refs.is_empty() {
            Ok(())
        } else {
            // Deduplicate
            missing_refs.sort();
            missing_refs.dedup();
            Err(missing_refs)
        }
    }
}

fn visit_path_item<F>(item: &PathItem, visit: &mut F)
where
    F: FnMut(&SchemaRef),
{
    if let Some(op) = &item.get {
        visit_operation(op, visit);
    }
    if let Some(op) = &item.put {
        visit_operation(op, visit);
    }
    if let Some(op) = &item.post {
        visit_operation(op, visit);
    }
    if let Some(op) = &item.delete {
        visit_operation(op, visit);
    }
    if let Some(op) = &item.options {
        visit_operation(op, visit);
    }
    if let Some(op) = &item.head {
        visit_operation(op, visit);
    }
    if let Some(op) = &item.patch {
        visit_operation(op, visit);
    }
    if let Some(op) = &item.trace {
        visit_operation(op, visit);
    }

    for param in &item.parameters {
        visit_parameter(param, visit);
    }
}

fn visit_operation<F>(op: &Operation, visit: &mut F)
where
    F: FnMut(&SchemaRef),
{
    for param in &op.parameters {
        visit_parameter(param, visit);
    }
    if let Some(body) = &op.request_body {
        visit_request_body(body, visit);
    }
    for resp in op.responses.values() {
        visit_response(resp, visit);
    }
}

fn visit_parameter<F>(param: &Parameter, visit: &mut F)
where
    F: FnMut(&SchemaRef),
{
    if let Some(s) = &param.schema {
        visit(s);
    }
}

fn visit_response<F>(resp: &ResponseSpec, visit: &mut F)
where
    F: FnMut(&SchemaRef),
{
    for media in resp.content.values() {
        visit_media_type(media, visit);
    }
    for header in resp.headers.values() {
        visit_header(header, visit);
    }
}

fn visit_request_body<F>(body: &RequestBody, visit: &mut F)
where
    F: FnMut(&SchemaRef),
{
    for media in body.content.values() {
        visit_media_type(media, visit);
    }
}

fn visit_header<F>(header: &Header, visit: &mut F)
where
    F: FnMut(&SchemaRef),
{
    if let Some(s) = &header.schema {
        visit(s);
    }
}

fn visit_media_type<F>(media: &MediaType, visit: &mut F)
where
    F: FnMut(&SchemaRef),
{
    if let Some(s) = &media.schema {
        visit(s);
    }
}

fn visit_schema_ref<F>(s: &SchemaRef, check: &mut F)
where
    F: FnMut(&str),
{
    match s {
        SchemaRef::Ref { reference } => check(reference),
        SchemaRef::Schema(boxed) => visit_json_schema(boxed, check),
        SchemaRef::Inline(_) => {} // Inline JSON value, assume safe or valid
    }
}

fn visit_json_schema<F>(s: &JsonSchema2020, check: &mut F)
where
    F: FnMut(&str),
{
    if let Some(r) = &s.reference {
        check(r);
    }
    if let Some(items) = &s.items {
        visit_json_schema(items, check);
    }
    if let Some(props) = &s.properties {
        for p in props.values() {
            visit_json_schema(p, check);
        }
    }
    if let Some(crate::schema::AdditionalProperties::Schema(p)) =
        &s.additional_properties.as_deref()
    {
        visit_json_schema(p, check);
    }
    if let Some(one_of) = &s.one_of {
        for p in one_of {
            visit_json_schema(p, check);
        }
    }
    if let Some(any_of) = &s.any_of {
        for p in any_of {
            visit_json_schema(p, check);
        }
    }
    if let Some(all_of) = &s.all_of {
        for p in all_of {
            visit_json_schema(p, check);
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ApiInfo {
    pub title: String,
    pub version: String,
    #[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 terms_of_service: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contact: Option<Contact>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub license: Option<License>,
}

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

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

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

impl Server {
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            description: None,
            variables: BTreeMap::new(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerVariable {
    #[serde(rename = "enum", skip_serializing_if = "Vec::is_empty")]
    pub enum_values: Vec<String>,
    pub default: 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 summary: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub get: Option<Operation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub put: Option<Operation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub post: Option<Operation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delete: Option<Operation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<Operation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub head: Option<Operation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub patch: Option<Operation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trace: Option<Operation>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub servers: Vec<Server>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<Parameter>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Operation {
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
    #[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 external_docs: Option<ExternalDocs>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub operation_id: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<Parameter>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_body: Option<RequestBody>,
    pub responses: BTreeMap<String, ResponseSpec>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub security: Vec<BTreeMap<String, Vec<String>>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deprecated: Option<bool>,
}

impl Operation {
    pub fn new() -> Self {
        Self {
            responses: BTreeMap::from([("200".to_string(), ResponseSpec::default())]),
            ..Default::default()
        }
    }

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

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

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

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

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ResponseSpec {
    pub description: String,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub content: BTreeMap<String, MediaType>,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub headers: BTreeMap<String, Header>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MediaType {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schema: Option<SchemaRef>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub example: Option<serde_json::Value>,
}

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

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Components {
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub schemas: BTreeMap<String, JsonSchema2020>,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub responses: BTreeMap<String, ResponseSpec>,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub parameters: BTreeMap<String, Parameter>,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub examples: BTreeMap<String, serde_json::Value>,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub request_bodies: BTreeMap<String, RequestBody>,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub headers: BTreeMap<String, Header>,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub security_schemes: BTreeMap<String, SecurityScheme>,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub links: BTreeMap<String, serde_json::Value>,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub callbacks: BTreeMap<String, BTreeMap<String, PathItem>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum SecurityScheme {
    ApiKey {
        name: String,
        #[serde(rename = "in")]
        location: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
    },
    Http {
        scheme: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        bearer_format: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
    },
    Oauth2 {
        flows: Box<OAuthFlows>,
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
    },
    OpenIdConnect {
        open_id_connect_url: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct OAuthFlows {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub implicit: Option<OAuthFlow>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub password: Option<OAuthFlow>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_credentials: Option<OAuthFlow>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authorization_code: Option<OAuthFlow>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OAuthFlow {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authorization_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub refresh_url: Option<String>,
    pub scopes: BTreeMap<String, String>,
}

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

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

// Re-exports/Traits needed for backwards compatibility or easy migration
pub trait OperationModifier {
    fn update_operation(op: &mut Operation);

    fn register_components(_spec: &mut OpenApiSpec) {}
}

pub trait ResponseModifier {
    fn update_response(op: &mut Operation);

    fn register_components(_spec: &mut OpenApiSpec) {}
}

// Helper implementations for OperationModifier/ResponseModifier
impl<T: OperationModifier> OperationModifier for Option<T> {
    fn update_operation(op: &mut Operation) {
        T::update_operation(op);
        if let Some(body) = &mut op.request_body {
            body.required = Some(false);
        }
    }

    fn register_components(spec: &mut OpenApiSpec) {
        T::register_components(spec);
    }
}

impl<T: OperationModifier, E> OperationModifier for Result<T, E> {
    fn update_operation(op: &mut Operation) {
        T::update_operation(op);
    }

    fn register_components(spec: &mut OpenApiSpec) {
        T::register_components(spec);
    }
}

macro_rules! impl_op_modifier_for_primitives {
    ($($ty:ty),*) => {
        $(
            impl OperationModifier for $ty {
                fn update_operation(_op: &mut Operation) {}
            }
        )*
    };
}
impl_op_modifier_for_primitives!(
    i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64, bool, String
);

impl ResponseModifier for () {
    fn update_response(op: &mut Operation) {
        op.responses.insert(
            "200".to_string(),
            ResponseSpec {
                description: "Successful response".into(),
                ..Default::default()
            },
        );
    }
}

impl ResponseModifier for String {
    fn update_response(op: &mut Operation) {
        let mut content = BTreeMap::new();
        content.insert(
            "text/plain".to_string(),
            MediaType {
                schema: Some(SchemaRef::Inline(serde_json::json!({"type": "string"}))),
                example: None,
            },
        );
        op.responses.insert(
            "200".to_string(),
            ResponseSpec {
                description: "Successful response".into(),
                content,
                ..Default::default()
            },
        );
    }
}

impl ResponseModifier for &'static str {
    fn update_response(op: &mut Operation) {
        String::update_response(op);
    }
}

impl<T: ResponseModifier> ResponseModifier for Option<T> {
    fn update_response(op: &mut Operation) {
        T::update_response(op);
    }

    fn register_components(spec: &mut OpenApiSpec) {
        T::register_components(spec);
    }
}

impl<T: ResponseModifier, E: ResponseModifier> ResponseModifier for Result<T, E> {
    fn update_response(op: &mut Operation) {
        T::update_response(op);
        E::update_response(op);
    }

    fn register_components(spec: &mut OpenApiSpec) {
        T::register_components(spec);
        E::register_components(spec);
    }
}

impl<T> ResponseModifier for http::Response<T> {
    fn update_response(op: &mut Operation) {
        op.responses.insert(
            "200".to_string(),
            ResponseSpec {
                description: "Successful response".into(),
                ..Default::default()
            },
        );
    }
}