aide 0.16.0-alpha.4

A code-first API documentation library
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
//! Traits and utilities for schema generation for operations (handlers).

use indexmap::IndexMap;
use schemars::Schema;

use crate::generate::GenContext;
use crate::openapi::{
    self, Operation, Parameter, ParameterData, QueryStyle, ReferenceOr, RequestBody, Response,
    StatusCode,
};
use crate::Error;

#[cfg(feature = "macros")]
pub use aide_macros::OperationIo;

/// A trait for operation input schema generation.
///
/// This must be implemented for all extractors
/// that appear in documented handlers.
///
/// All method implementations are optional.
///
/// # Examples
///
/// In order to allow an extractor to appear in a handler,
/// the following is enough:
///
/// ```
/// use aide::OperationInput;
///
/// struct MyExtractor;
///
/// impl OperationInput for MyExtractor {}
/// ```
///
/// This will enable usage of the extractor in handlers,
/// but will not add anything to the documentation.
/// To extend the generated documentation refer to some of the provided
/// implementations in this crate.
///
/// For simpler cases or wrappers the [`OperationIo`] derive macro
/// can be used to implement this trait.
#[allow(unused_variables)]
pub trait OperationInput {
    /// Modify the operation.
    ///
    /// This method gets mutable access to the
    /// entire operation, it's the implementer's responsibility
    /// to detect errors and only modify the operation as much as needed.
    ///
    /// There are reusable helpers in [`aide::operation`](crate::operation)
    /// to help with both boilerplate and error detection.
    fn operation_input(ctx: &mut GenContext, operation: &mut Operation) {}

    /// Inferred early responses are used to document early returns for
    /// extractors, guards inside handlers. For example these could represent
    /// JSON parsing errors, authentication failures.
    ///
    /// The function is supposed to return `(status code, response)` pairs,
    /// if the status code is not specified, the response is assumed to be
    /// a default response.
    ///
    /// It's important for the implementation to be idempotent.
    ///
    /// See [`OperationOutput::inferred_responses`] for more details.
    fn inferred_early_responses(
        ctx: &mut GenContext,
        operation: &mut Operation,
    ) -> Vec<(Option<StatusCode>, Response)> {
        Vec::new()
    }
}

impl OperationInput for () {}

macro_rules! impl_operation_input {
    ( $($ty:ident),* $(,)? ) => {
        #[allow(non_snake_case)]
        impl<$($ty,)*> OperationInput for ($($ty,)*)
        where
            $( $ty: OperationInput, )*
        {
            fn operation_input(ctx: &mut GenContext, operation: &mut Operation) {
                $(
                    $ty::operation_input(ctx, operation);
                )*
            }

            fn inferred_early_responses(
                ctx: &mut GenContext,
                operation: &mut Operation,
            ) -> Vec<(Option<StatusCode>, Response)> {
                let mut responses = Vec::new();
                $(
                    responses.extend($ty::inferred_early_responses(ctx, operation));
                )*
                responses
            }
        }
    };
}

all_the_tuples!(impl_operation_input);

#[doc(hidden)]
pub trait OperationHandler<I: OperationInput, O: OperationOutput> {}

macro_rules! impl_operation_handler {
    ( $($ty:ident),* $(,)? ) => {
        #[allow(non_snake_case)]
        impl<Ret, F, $($ty,)*> OperationHandler<($($ty,)*), Ret::Output> for F
        where
            F: FnOnce($($ty,)*) -> Ret,
            Ret: std::future::Future,
            Ret::Output: OperationOutput,
            $( $ty: OperationInput, )*
        {}
    };
}

impl<Ret, F> OperationHandler<(), Ret::Output> for F
where
    F: FnOnce() -> Ret,
    Ret: std::future::Future,
    Ret::Output: OperationOutput,
{
}

all_the_tuples!(impl_operation_handler);

/// A trait for operation output schema generation.
///
/// This can be implemented for types that can
/// describe their own output schema.
///
/// All method implementations are optional.
///
/// For simpler cases or wrappers the [`OperationIo`] derive macro
/// can be used to implement this trait.
#[allow(unused_variables)]
pub trait OperationOutput {
    /// The type that is used in examples.
    ///
    /// # Examples
    ///
    /// In case of `Json<T>`, this should be `T`,
    /// whereas for `String` it should be `Self`.
    type Inner;

    /// Return a response documentation for this type,
    /// alternatively modify the operation if required.
    ///
    /// This method gets mutable access to the
    /// entire operation, it's the implementer's responsibility
    /// to detect errors and only modify the operation as much as needed.
    ///
    /// Note that this function **can be called multiple
    /// times for the same operation** and should be idempotent.
    ///
    /// There are reusable helpers in [`aide::operation`](crate::operation)
    /// to help with both boilerplate and error detection.
    fn operation_response(ctx: &mut GenContext, operation: &mut Operation) -> Option<Response> {
        None
    }

    /// Inferred responses are used when the type is
    /// used as a request handler return type.
    ///
    /// The function is supposed to return `(status code, response)` pairs,
    /// if the status code is not specified, the response is assumed to be
    /// a default response.
    ///
    /// As an example `Result<T, E>` could
    /// return `(Some(200), T::operation_response(..))` and
    /// `(None, E::operation_response(..))` to indicate
    /// a successful response and a default error.
    ///
    /// This function can be called after or before `operation_response`,
    /// it's important for the implementation to be idempotent.
    fn inferred_responses(
        ctx: &mut GenContext,
        operation: &mut Operation,
    ) -> Vec<(Option<StatusCode>, Response)> {
        Vec::new()
    }
}

/// Location of an operation parameter.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParamLocation {
    Query,
    Path,
    Header,
    Cookie,
}

/// Generate operation parameters from a JSON schema
/// where the schema is an object, and each
/// property is a parameter.
#[tracing::instrument(skip_all)]
pub fn parameters_from_schema(
    ctx: &mut GenContext,
    schema: Schema,
    location: ParamLocation,
) -> Vec<Parameter> {
    let schema = ctx.resolve_schema(&schema);

    let mut params = Vec::new();

    if let Some(obj) = schema.as_object() {
        for (name, schema) in obj
            .get("properties")
            .and_then(|p| p.as_object())
            .into_iter()
            .flatten()
        {
            let json_schema: Schema = schema
                .clone()
                .try_into()
                .unwrap_or_else(|err| panic!("Failed to convert schema {schema}: {err:?}"));

            match location {
                ParamLocation::Query => {
                    params.push(Parameter::Query {
                        parameter_data: ParameterData {
                            name: name.clone(),
                            description: json_schema
                                .get("description")
                                .and_then(|d| d.as_str())
                                .map(String::from),
                            required: obj
                                .get("required")
                                .and_then(|r| r.as_array())
                                .is_some_and(|r| r.contains(&name.as_str().into())),
                            format: crate::openapi::ParameterSchemaOrContent::Schema(
                                openapi::SchemaObject {
                                    json_schema,
                                    example: None,
                                    external_docs: None,
                                },
                            ),
                            extensions: Default::default(),
                            deprecated: None,
                            example: None,
                            examples: IndexMap::default(),
                            explode: None,
                        },
                        allow_reserved: false,
                        style: QueryStyle::Form,
                        allow_empty_value: None,
                    });
                }
                ParamLocation::Path => {
                    params.push(Parameter::Path {
                        parameter_data: ParameterData {
                            name: name.clone(),
                            description: json_schema
                                .get("description")
                                .and_then(|d| d.as_str())
                                .map(String::from),
                            required: obj
                                .get("required")
                                .and_then(|r| r.as_array())
                                .is_some_and(|r| r.contains(&name.as_str().into())),
                            format: crate::openapi::ParameterSchemaOrContent::Schema(
                                openapi::SchemaObject {
                                    json_schema,
                                    example: None,
                                    external_docs: None,
                                },
                            ),
                            extensions: Default::default(),
                            deprecated: None,
                            example: None,
                            examples: IndexMap::default(),
                            explode: None,
                        },
                        style: openapi::PathStyle::Simple,
                    });
                }
                ParamLocation::Header => {
                    params.push(Parameter::Header {
                        parameter_data: ParameterData {
                            name: name.clone(),
                            description: json_schema
                                .get("description")
                                .and_then(|d| d.as_str())
                                .map(String::from),
                            required: obj
                                .get("required")
                                .and_then(|r| r.as_array())
                                .is_some_and(|r| r.contains(&name.as_str().into())),
                            format: crate::openapi::ParameterSchemaOrContent::Schema(
                                openapi::SchemaObject {
                                    json_schema,
                                    example: None,
                                    external_docs: None,
                                },
                            ),
                            extensions: Default::default(),
                            deprecated: None,
                            example: None,
                            examples: IndexMap::default(),
                            explode: None,
                        },
                        style: openapi::HeaderStyle::Simple,
                    });
                }
                ParamLocation::Cookie => {
                    params.push(Parameter::Cookie {
                        parameter_data: ParameterData {
                            name: name.clone(),
                            description: json_schema
                                .get("description")
                                .and_then(|d| d.as_str())
                                .map(String::from),
                            required: obj
                                .get("required")
                                .and_then(|r| r.as_array())
                                .is_some_and(|r| r.contains(&name.as_str().into())),
                            format: crate::openapi::ParameterSchemaOrContent::Schema(
                                openapi::SchemaObject {
                                    json_schema,
                                    example: None,
                                    external_docs: None,
                                },
                            ),
                            extensions: Default::default(),
                            deprecated: None,
                            example: None,
                            examples: IndexMap::default(),
                            explode: None,
                        },
                        style: openapi::CookieStyle::Form,
                    });
                }
            }
        }
    }

    params
}

/// Set the body of an operation while
/// reporting errors.
pub fn set_body(ctx: &mut GenContext, operation: &mut Operation, body: RequestBody) {
    if operation.request_body.is_some() {
        ctx.error(Error::DuplicateRequestBody);
    }
    operation.request_body = Some(ReferenceOr::Item(body));
}

/// Add parameters to an operation while
/// reporting errors.
pub fn add_parameters(
    ctx: &mut GenContext,
    operation: &mut Operation,
    params: impl IntoIterator<Item = Parameter>,
) {
    for param in params {
        if operation.parameters.iter().any(|p| match p {
            ReferenceOr::Reference { .. } => false,
            ReferenceOr::Item(p) => p.parameter_data_ref().name == param.parameter_data_ref().name,
        }) {
            ctx.error(Error::DuplicateParameter(
                param.parameter_data_ref().name.clone(),
            ));
        }
        operation.parameters.push(ReferenceOr::Item(param));
    }
}

#[cfg(test)]
mod tests {
    use crate::generate::GenContext;
    use crate::openapi::{Operation, Response, StatusCode};
    use crate::{generate, OperationInput, OperationOutput};
    use aide_macros::OperationIo;
    use schemars::JsonSchema;

    fn assert_default_input_impl<T: OperationInput>(ctx: &mut GenContext) {
        let mut operation = Operation::default();

        T::operation_input(ctx, &mut operation);
        assert_eq!(operation, Operation::default());

        assert_eq!(T::inferred_early_responses(ctx, &mut operation), Vec::new());
        assert_eq!(operation, Operation::default());
    }

    fn assert_default_output_impl<T: OperationOutput<Inner = T>>(ctx: &mut GenContext) {
        let mut operation = Operation::default();

        assert_eq!(T::operation_response(ctx, &mut operation), None);
        assert_eq!(operation, Operation::default());

        assert_eq!(T::inferred_responses(ctx, &mut operation), Vec::new());
        assert_eq!(operation, Operation::default());
    }

    #[test]
    fn operation_io() {
        #[derive(OperationIo)]
        struct OperationInputOutput;
        #[derive(OperationIo)]
        #[aide(input, output)]
        struct OperationInputOutput2;
        #[derive(OperationIo)]
        struct OperationInputOutputGeneric<T>(T);
        #[derive(OperationIo)]
        #[aide(input)]
        struct OperationInput;
        #[derive(OperationIo)]
        #[aide(output)]
        struct OperationOutput;

        generate::in_context(|ctx| {
            assert_default_input_impl::<OperationInputOutput>(ctx);
            assert_default_output_impl::<OperationInputOutput>(ctx);

            assert_default_input_impl::<OperationInputOutput2>(ctx);
            assert_default_output_impl::<OperationInputOutput2>(ctx);

            assert_default_input_impl::<OperationInputOutputGeneric<()>>(ctx);
            assert_default_output_impl::<OperationInputOutputGeneric<()>>(ctx);

            assert_default_input_impl::<OperationInput>(ctx);

            assert_default_output_impl::<OperationOutput>(ctx);
        });
    }

    #[test]
    fn operation_io_with() {
        struct ImplsOperationInput;
        impl OperationInput for ImplsOperationInput {
            fn operation_input(_ctx: &mut GenContext, operation: &mut Operation) {
                // Changing a property of the operation so that we know this function was called
                operation.deprecated = true;
            }

            fn inferred_early_responses(
                _ctx: &mut GenContext,
                _operation: &mut Operation,
            ) -> Vec<(Option<StatusCode>, Response)> {
                vec![(Some(StatusCode::Code(400)), Response::default())]
            }
        }

        struct ImplsOperationOutput;
        impl OperationOutput for ImplsOperationOutput {
            type Inner = ();

            fn operation_response(
                _ctx: &mut GenContext,
                _operation: &mut Operation,
            ) -> Option<Response> {
                Some(Response::default())
            }

            fn inferred_responses(
                _ctx: &mut GenContext,
                _operation: &mut Operation,
            ) -> Vec<(Option<StatusCode>, Response)> {
                vec![(None, Response::default())]
            }
        }

        #[derive(OperationIo)]
        #[aide(
            input_with = "ImplsOperationInput",
            output_with = "ImplsOperationOutput"
        )]
        struct OperationIoWith;

        generate::in_context(|ctx| {
            let mut operation = Operation::default();

            OperationIoWith::operation_input(ctx, &mut operation);
            assert!(operation.deprecated);

            assert_eq!(
                OperationIoWith::inferred_early_responses(ctx, &mut operation),
                vec![(Some(StatusCode::Code(400)), Response::default())],
            );

            assert_eq!(
                OperationIoWith::operation_response(ctx, &mut operation),
                Some(Response::default()),
            );
            assert_eq!(
                OperationIoWith::inferred_responses(ctx, &mut operation),
                vec![(None, Response::default())],
            );
            #[allow(clippy::items_after_statements)]
            fn assert_inner_is_unit<T: OperationOutput<Inner = ()>>() {}
            assert_inner_is_unit::<OperationIoWith>();
        });
    }

    #[test]
    fn operation_io_json_schema() {
        // The `input_with`/`output_with` ensures that this test will only compile if
        // the `json_schema` trait bounds are correct.
        #[derive(OperationIo)]
        #[aide(
            input_with = "OperationInputOutputIfJsonSchema<T, U>",
            output_with = "OperationInputOutputIfJsonSchema<T, U>",
            json_schema
        )]
        struct OperationInputOutput<T, U>(T, U);

        struct OperationInputOutputIfJsonSchema<T, U>(T, U);
        impl<T: JsonSchema, U: JsonSchema> OperationInput for OperationInputOutputIfJsonSchema<T, U> {}
        impl<T: JsonSchema, U: JsonSchema> OperationOutput for OperationInputOutputIfJsonSchema<T, U> {
            type Inner = Self;
        }

        fn assert_impls_operation_input_output<T: OperationInput + OperationOutput>() {}
        assert_impls_operation_input_output::<OperationInputOutput<(), i32>>();
    }
}