poem_openapi/
base.rs

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
use std::{
    collections::HashMap,
    fmt::{self, Debug, Display},
    future::Future,
    ops::Deref,
};

use futures_util::FutureExt;
use poem::{endpoint::BoxEndpoint, http::Method, Error, FromRequest, Request, RequestBody, Result};

use crate::{
    payload::Payload,
    registry::{
        MetaApi, MetaMediaType, MetaOAuthScope, MetaParamIn, MetaRequest, MetaResponse,
        MetaResponses, MetaSchemaRef, MetaWebhook, Registry,
    },
};

/// API extractor types.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ApiExtractorType {
    /// A request object.
    RequestObject,

    /// A request parameter.
    Parameter,

    /// A security scheme.
    SecurityScheme,

    /// A poem extractor.
    PoemExtractor,
}

#[doc(hidden)]
#[derive(Clone)]
pub struct UrlQuery(pub Vec<(String, String)>);

impl Deref for UrlQuery {
    type Target = Vec<(String, String)>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl UrlQuery {
    /// Returns all values with the specified name.
    pub fn get_all<'a, 'b: 'a>(&'b self, name: &'a str) -> impl Iterator<Item = &'b String> + 'a {
        self.0
            .iter()
            .filter(move |(n, _)| n == name)
            .map(|(_, value)| value)
    }

    /// Returns the first value with the specified name.
    pub fn get(&self, name: &str) -> Option<&String> {
        self.get_all(name).next()
    }
}

/// Options for the parameter extractor.
#[derive(Clone)]
pub struct ExtractParamOptions<T> {
    /// The name of this parameter.
    pub name: &'static str,

    /// The default value of this parameter.
    pub default_value: Option<fn() -> T>,

    /// The example value of this parameter.
    pub example_value: Option<fn() -> T>,

    /// When this is `true`, parameter values of type array or object generate
    /// separate parameters for each value of the array or key-value pair of the
    /// map.
    pub explode: bool,
}

impl<T> Default for ExtractParamOptions<T> {
    fn default() -> Self {
        Self {
            name: "",
            default_value: None,
            example_value: None,
            explode: true,
        }
    }
}

/// Represents an OpenAPI extractor.
///
/// # Provided Implementations
///
/// - **Path&lt;T: Type>**
///
///    Extract the parameters in the request path into
///    [`Path`](crate::param::Path).
///
/// - **Query&lt;T: Type>**
///
///    Extract the parameters in the query string into
///    [`Query`](crate::param::Query).
///
/// - **Header&lt;T: Type>**
///
///    Extract the parameters in the request header into
///    [`Header`](crate::param::Header).
///
/// - **Cookie&lt;T: Type>**
///
///    Extract the parameters in the cookie into
///    [`Cookie`](crate::param::Cookie).
///
/// - **CookiePrivate&lt;T: Type>**
///
///    Extract the parameters in the private cookie into
///    [`CookiePrivate`](crate::param::CookiePrivate).
///
/// - **CookieSigned&lt;T: Type>**
///
///    Extract the parameters in the signed cookie into
///    [`CookieSigned`](crate::param::CookieSigned).
///
/// - **Binary&lt;T>**
///
///    Extract the request body as binary into
///    [`Binary`](crate::payload::Binary).
///
/// - **Json&lt;T>**
///
///    Parse the request body in `JSON` format into
///    [`Json`](crate::payload::Json).
///
/// - **PlainText&lt;T>**
///
///    Extract the request body as utf8 string into
///    [`PlainText`](crate::payload::PlainText).
///
/// - **Any type derived from the [`ApiRequest`](crate::ApiRequest) macro**
///
///    Extract the complex request body derived from the `ApiRequest` macro.
///
/// - **Any type derived from the [`Multipart`](crate::Multipart) macro**
///
///    Extract the multipart object derived from the `Multipart` macro.
///
/// - **Any type derived from the [`SecurityScheme`](crate::SecurityScheme)
///   macro**
///
///    Extract the authentication value derived from the `SecurityScheme`
///    macro.
///
/// - **T: poem::FromRequest**
///
///    Use Poem's extractor.
#[allow(unused_variables)]
pub trait ApiExtractor<'a>: Sized {
    /// The type of API extractor.
    const TYPES: &'static [ApiExtractorType];

    /// If it is `true`, it means that this parameter is required.
    const PARAM_IS_REQUIRED: bool = false;

    /// The parameter type.
    type ParamType;

    /// The raw parameter type for validators.
    type ParamRawType;

    /// Register related types to registry.
    fn register(registry: &mut Registry) {}

    /// Returns names of security scheme if this extractor is security scheme.
    fn security_schemes() -> Vec<&'static str> {
        vec![]
    }

    /// Returns the location of the parameter if this extractor is parameter.
    fn param_in() -> Option<MetaParamIn> {
        None
    }

    /// Returns the schema of the parameter if this extractor is parameter.
    fn param_schema_ref() -> Option<MetaSchemaRef> {
        None
    }

    /// Returns `MetaRequest` if this extractor is request object.
    fn request_meta() -> Option<MetaRequest> {
        None
    }

    /// Returns a reference to the raw type of this parameter.
    fn param_raw_type(&self) -> Option<&Self::ParamRawType> {
        None
    }

    /// Parse from the HTTP request.
    fn from_request(
        request: &'a Request,
        body: &mut RequestBody,
        param_opts: ExtractParamOptions<Self::ParamType>,
    ) -> impl Future<Output = Result<Self>> + Send;
}

impl<'a, T: FromRequest<'a>> ApiExtractor<'a> for T {
    const TYPES: &'static [ApiExtractorType] = &[ApiExtractorType::PoemExtractor];

    type ParamType = ();
    type ParamRawType = ();

    async fn from_request(
        request: &'a Request,
        body: &mut RequestBody,
        _param_opts: ExtractParamOptions<Self::ParamType>,
    ) -> Result<Self> {
        // FIXME: remove the unnecessary boxed
        // https://github.com/rust-lang/rust/issues/100013
        T::from_request(request, body).boxed().await
    }
}

/// Represents an OpenAPI response content object.
pub trait ResponseContent {
    /// Returns the media types in this content.
    fn media_types() -> Vec<MetaMediaType>;

    /// Register the schema contained in this content to the registry.
    #[allow(unused_variables)]
    fn register(registry: &mut Registry) {}
}

impl<T: Payload> ResponseContent for T {
    fn media_types() -> Vec<MetaMediaType> {
        vec![MetaMediaType {
            content_type: T::CONTENT_TYPE,
            schema: T::schema_ref(),
        }]
    }

    fn register(registry: &mut Registry) {
        T::register(registry);
    }
}

/// Represents an OpenAPI responses object.
///
/// # Provided Implementations
///
/// - **Binary&lt;T: Type>**
///
///    A binary response with content type `application/octet-stream`.
///
/// - **Json&lt;T: Type>**
///
///    A JSON response with content type `application/json`.
///
/// - **PlainText&lt;T: Type>**
///
///    A utf8 string response with content type `text/plain`.
///
/// - **Attachment&lt;T: Type>**
///
///    A file download response, the content type is
///    `application/octet-stream`.
///
/// - **Response&lt;T: Type>**
///
///    A response type use it to modify the status code and HTTP headers.
///
/// - **()**
///
///     It means that this API does not have any response body.
///
/// - **poem::Result&lt;T: ApiResponse>**
///
///     It means that an error may occur in this API.
///
/// - **Any type derived from the [`ApiResponse`](crate::ApiResponse) macro**
///
///     A complex response  derived from the `ApiResponse` macro.
pub trait ApiResponse: Sized {
    /// If true, it means that the response object has a custom bad request
    /// handler.
    const BAD_REQUEST_HANDLER: bool = false;

    /// Gets metadata of this response.
    fn meta() -> MetaResponses;

    /// Register the schema contained in this response object to the registry.
    fn register(registry: &mut Registry);

    /// Convert [`poem::Error`] to this response object.
    #[allow(unused_variables)]
    fn from_parse_request_error(err: Error) -> Self {
        unreachable!()
    }
}

impl ApiResponse for () {
    fn meta() -> MetaResponses {
        MetaResponses {
            responses: vec![MetaResponse {
                description: "",
                status: Some(200),
                content: vec![],
                headers: vec![],
            }],
        }
    }

    fn register(_registry: &mut Registry) {}
}

impl ApiResponse for Error {
    fn meta() -> MetaResponses {
        MetaResponses {
            responses: Vec::new(),
        }
    }

    fn register(_registry: &mut Registry) {}
}

impl<T, E> ApiResponse for Result<T, E>
where
    T: ApiResponse,
    E: ApiResponse + Into<Error> + Send + Sync + 'static,
{
    const BAD_REQUEST_HANDLER: bool = T::BAD_REQUEST_HANDLER;

    fn meta() -> MetaResponses {
        let mut meta = T::meta();
        meta.responses.extend(E::meta().responses);
        meta
    }

    fn register(registry: &mut Registry) {
        T::register(registry);
        E::register(registry);
    }

    fn from_parse_request_error(err: Error) -> Self {
        Ok(T::from_parse_request_error(err))
    }
}

#[cfg(feature = "websocket")]
impl<F, Fut> ApiResponse for poem::web::websocket::WebSocketUpgraded<F>
where
    F: FnOnce(poem::web::websocket::WebSocketStream) -> Fut + Send + Sync + 'static,
    Fut: std::future::Future + Send + 'static,
{
    fn meta() -> MetaResponses {
        MetaResponses {
            responses: vec![MetaResponse {
                description: "A websocket response",
                status: Some(101),
                content: vec![],
                headers: vec![],
            }],
        }
    }

    fn register(_registry: &mut Registry) {}
}

/// Represents an OpenAPI tags.
pub trait Tags {
    /// Register this tag type to registry.
    fn register(&self, registry: &mut Registry);

    /// Gets the tag name.
    fn name(&self) -> &'static str;
}

/// Represents a OAuth scopes.
pub trait OAuthScopes {
    /// Gets metadata of this object.
    fn meta() -> Vec<MetaOAuthScope>;

    /// Get the scope name.
    fn name(&self) -> &'static str;
}

/// A operation id that can be obtained from the response
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct OperationId(pub &'static str);

impl Display for OperationId {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.0)
    }
}

/// Represents an OpenAPI object.
pub trait OpenApi: Sized {
    /// Gets metadata of this API object.
    fn meta() -> Vec<MetaApi>;

    /// Register some types to the registry.
    fn register(registry: &mut Registry);

    /// Adds all API endpoints to the routing object.
    fn add_routes(self, route_table: &mut HashMap<String, HashMap<Method, BoxEndpoint<'static>>>);
}

macro_rules! impl_openapi_for_tuple {
    (($head:ident, $hn:tt), $(($tail:ident, $tn:tt)),*) => {
        impl<$head: OpenApi, $($tail: OpenApi),*> OpenApi for ($head, $($tail),*) {
            fn meta() -> Vec<MetaApi> {
                let mut metadata = $head::meta();
                $(
                metadata.extend($tail::meta());
                )*
                metadata
            }

            fn register(registry: &mut Registry) {
                $head::register(registry);
                $(
                $tail::register(registry);
                )*
            }

            fn add_routes(self, route_table: &mut HashMap<String, HashMap<Method, BoxEndpoint<'static>>>) {
                self.$hn.add_routes(route_table);
                $(
                self.$tn.add_routes(route_table);
                )*
            }
        }
    };

    () => {};
}

#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4), (T6, 5), (T7, 6), (T8, 7), (T9, 8), (T10, 9), (T11, 10), (T12, 11), (T13, 12), (T14, 13), (T15, 14), (T16, 15));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4), (T6, 5), (T7, 6), (T8, 7), (T9, 8), (T10, 9), (T11, 10), (T12, 11), (T13, 12), (T14, 13), (T15, 14));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4), (T6, 5), (T7, 6), (T8, 7), (T9, 8), (T10, 9), (T11, 10), (T12, 11), (T13, 12), (T14, 13));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4), (T6, 5), (T7, 6), (T8, 7), (T9, 8), (T10, 9), (T11, 10), (T12, 11), (T13, 12));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4), (T6, 5), (T7, 6), (T8, 7), (T9, 8), (T10, 9), (T11, 10), (T12, 11));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4), (T6, 5), (T7, 6), (T8, 7), (T9, 8), (T10, 9), (T11, 10));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4), (T6, 5), (T7, 6), (T8, 7), (T9, 8), (T10, 9));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4), (T6, 5), (T7, 6), (T8, 7), (T9, 8));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4), (T6, 5), (T7, 6), (T8, 7));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4), (T6, 5), (T7, 6));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4), (T6, 5));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3), (T5, 4));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2), (T4, 3));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1), (T3, 2));
#[rustfmt::skip]
impl_openapi_for_tuple!((T1, 0), (T2, 1));

impl OpenApi for () {
    fn meta() -> Vec<MetaApi> {
        vec![]
    }

    fn register(_registry: &mut Registry) {}

    fn add_routes(self, _route_table: &mut HashMap<String, HashMap<Method, BoxEndpoint<'static>>>) {
    }
}

/// Represents a webhook object.
pub trait Webhook: Sized {
    /// Gets metadata of this webhooks object.
    fn meta() -> Vec<MetaWebhook>;

    /// Register some types to the registry.
    fn register(registry: &mut Registry);
}

impl Webhook for () {
    fn meta() -> Vec<MetaWebhook> {
        vec![]
    }

    fn register(_: &mut Registry) {}
}