aide
repository·main·Indexed 20 days ago
https://github.com/tamasfe/aideA code-first API documentation library for Rust designed to automate the generation of OpenAPI 3.1.x specifications from source code. It leverages schemars for schema generation and provides traits like OperationInput and OperationOutput to document HTTP parameters and responses. Aide includes integration for Axum and provides tools for customizing documentation via the WithApi wrapper and ApiOverride trait.
What's inside aide
- Aide is a code-first API documentation and utility library for Rust. It is designed to help developers generate documentation (such as OpenAPI specifications) directly from their Rust code, reducing the need for manual documentation maintenance.
Explore the Aide ecosystem
mainAide has a growing ecosystem of community-maintained projects, including:
- aide-axum-typed-multipart-2: A wrapper around
axum_typed_multipartthat enables documentation generation for multipart requests. - rovo: A tool for doc-comment-driven OpenAPI documentation generation specifically for Axum web applications, built on top of Aide.
- aide-axum-typed-multipart-2: A wrapper around
Run the Aide Axum example application
mainTo run the minimal to-do application built with
axumand documented usingaide, use the following cargo command. Once running, the documentation is served locally athttp://localhost:3000.cargo run --bin example-axumRun the Aide Axum Cloudflare Worker example
mainTo run the minimal to-do Axum Cloudflare Worker example, use the following command. Once running, the documentation is served locally at
http://localhost:3000.npm run devConfigure documentation generation settings via thread-local context
mainAide uses a thread-local
GenContextto manage settings for API documentation generation. Most configuration is performed using top-level functions that modify this context for the current thread. This allows you to set global behaviors (like error handling or schema extraction) without passing a context object through every function call in your application.// Example of configuring the context // These settings apply to the current thread aide::extract_schemas(true); aide::inferred_empty_response_status(204); aide::strip_query_null_types(true);How Aide generates OpenAPI documentation
mainaideis a code-first OpenAPI 3.1.x documentation generator. It uses a combination of type-based generation and declarative transformations to minimize manual documentation effort.Type-based Generation
aideleverages theschemarscrate for schema generation. For JSON-based APIs, you simply need to implementschemars::JsonSchemaalongsideserde'sSerializeandDeserializetraits on your types.Automatic Parameter and Response Documentation
aideuses theOperationInputandOperationOutputtraits to automatically document HTTP parameters and responses. For example, if you use anaxum::Json<T>extractor,aidewill automatically generate anapplication/jsonrequest body documentation using the schema ofT, providedTimplementsJsonSchema.Declarative Documentation
Manual documentation is handled through composable
transformfunctions and a builder-pattern API, allowing you to augment the automatically generated schema with specific details.Customize API documentation using `WithApi` and `ApiOverride`
mainAide allows you to customize how types are documented in your OpenAPI specification using the
WithApi<T>wrapper. This is useful for two scenarios:- Overriding documentation for existing types: If a type already implements
OperationInputorOperationOutput, you can useWithApiwith a customApiOverrideimplementation to provide different documentation metadata. - Adding documentation to types that don't support it: You can wrap types that do not natively implement
OperationInputorOperationOutputto make them compatible with Aide's documentation generation.
Implementation Approaches
1. Simple Type Override
Use this when you want to override the documentation for a specific concrete type.
use aide::{ApiOverride, OperationInput, WithApi}; #[derive(Eq, PartialEq, Debug)] struct SomeType; #[derive(Debug)] struct MyApiOverride; impl ApiOverride for MyApiOverride { type Target = SomeType; } // Implement OperationInput (or OperationOutput) on the override type to define custom docs impl OperationInput for MyApiOverride { // override stuff here } // Use WithApi in your handler async fn my_handler(WithApi(ty, ..): WithApi<MyApiOverride>) -> bool { assert_eq!(ty, SomeType); true }2. Generic Type Override
Use this when you want to apply documentation logic to a generic wrapper (e.g., a custom XML or JSON wrapper) across multiple target types.
use std::marker::PhantomData; use aide::{ApiOverride, OperationInput, WithApi}; #[derive(Eq, PartialEq, Debug)] struct SomeType; #[derive(Eq, PartialEq, Debug)] struct CustomXML<T>(T); #[derive(Debug)] struct MyCustomXML<T>(PhantomData<T>); impl<T> ApiOverride for MyCustomXML<T> { type Target = CustomXML<T>; } impl<T> OperationInput for MyCustomXML<T> { // override stuff with access to T } async fn my_handler(WithApi(ty, ..): WithApi<MyCustomXML<SomeType>>) -> bool { assert_eq!(ty, CustomXML(SomeType)); true }use aide::{ApiOverride, OperationInput, WithApi}; #[derive(Eq, PartialEq, Debug)] struct SomeType; #[derive(Debug)] struct MyApiOverride; impl ApiOverride for MyApiOverride { type Target = SomeType; } impl OperationInput for MyApiOverride { // override stuff } async fn my_handler(WithApi(ty, ..): WithApi<MyApiOverride>) -> bool { assert_eq!(ty, SomeType); true }- Overriding documentation for existing types: If a type already implements
Handle documentation generation errors
mainDuring the OpenAPI generation process, some errors may occur. By default,
aidetakes no action on these errors (they are swallowed), which might indicate bugs in the documentation logic.To handle these errors, you can register an error handler in the thread-local context using
aide::generate::on_error.Warning: It is not advised to simply panic on all errors, especially in production environments, as
aidemay produce false positives when contextual information is insufficient.Configure Aide feature flags
mainaidehas no features enabled by default. You must enable specific features depending on your requirements.Core Features
macros: Enables additional helper macros, including theOperationIoderive macro.
Axum Integration
If using
axum, you can enable specific feature gates for various extractors and types:axum(base integration)axum-formaxum-jsonaxum-matched-pathaxum-multipartaxum-original-uriaxum-queryaxum-tokio(forConnectInfo)axum-ws(WebSockets)
Axum-extra Integration
axum-extraaxum-extra-cookieaxum-extra-cookie-privateaxum-extra-formaxum-extra-headersaxum-extra-queryaxum-extra-json-deserializer
UI Documentation Providers
swaggerredocscalar
Use `OperationIo` derive macro for boilerplate reduction
mainThe
OperationIoderive macro (provided byaide_macros) simplifies implementingOperationInputandOperationOutput. You can use attributes to customize which parts of the trait are implemented:#[aide(input)]: ImplementsOperationInput.#[aide(output)]: ImplementsOperationOutput.#[aide(input, output)]: Implements both.#[aide(input_with = "Type", output_with = "Type")]: Uses specific types for the input/output logic.#[aide(json_schema)]: AddsJsonSchemabounds to the implementation.
#[derive(OperationIo)] #[aide(input, output)] struct MyDataWrapper(String);Strip null types from query parameters with strip_query_null_types
mainWhen enabled, null types are automatically removed from query parameter schemas during finalization. This is useful because query strings cannot express null values (a parameter is either present or absent). This is enabled by default.
// Disable automatic stripping of null types aide::strip_query_null_types(false);Toggle error response visibility with all_error_responses
mainControls whether all theoretically possible error responses (including framework-specific ones) are output. This is disabled by default.
// Enable outputting all possible error responses aide::all_error_responses(true);