Crate presentation
Utoipa is a great crate for generating documentation (openapi/swagger) via source code.
But since Rust is a static programming language, we don't have the possibility of automatically discovering paths and dto in runtime and adding them to the documentation,
For APIs with just a few endpoints, it's not that much trouble to add controller functions one by one, and DTOs one by one.
But, if you have hundreds or even thousands of endpoints, the code becomes very verbose and difficult to maintain.
Ex :
;
The goal of this crate is to propose a macro that automates the detection of methods carrying Utoipa
macros (#[utoipa::path(...]), and adds them automatically. (it also detects sub-modules.)
It also detects struct that derive or implement ToSchema for the components(schemas) section, and the ToResponse
for the components(responses) section.
Features
- Automatic recursive path detection
- Automatic import from module
- Automatic import from src folder
- Automatic model detection
- Automatic response detection
- Works with workspaces
- Exclude a method from automatic scanning
- Custom path detection
How to use it
Simply add the crate utoipauto to the project
cargo add utoipauto
Import macro
use utoipauto;
Then add the #[utoipauto] macro just before the #[derive(OpenApi)] and #[openapi] macros.
Important !!
Put #[utoipauto] before #[derive(OpenApi)] and #[openapi] macros.
The paths receives a String which must respect this structure :
"MODULE_SRC_FILE_PATH, MODULE_SRC_FILE_PATH, ..."
You can add several paths by separating them with a coma ",".
Usage with workspaces
If you are using a workspace, you must specify the name of the crate in the path.
This applies even if you are using #[utoipauto] in the same crate.
You can specify that the specified paths are from another crate by using the from key work.
Import from src folder
If no path is specified, the macro will automatically scan the src folder and add all the methods carrying
the #[utoipa::path(...)] macro, and all structs deriving ToSchema and ToResponse.
Here's an example of how to add all the methods contained in the src code.
...
use utoipauto;
...
;
...
Import from module
Here's an example of how to add all the methods and structs contained in the rest module.
use utoipauto;
;
Here's an example of how to add all methods and structs contained in the rest module if it is in a sub-folder:
use utoipauto;
;
Import from filename
Here's an example of how to add all the methods contained in the test_controller and test2_controller modules. you can also combine automatic and manual addition, as here we've added a method manually to the documentation " other_controller::get_users", and a schema "TestDTO".
use utoipauto;
;
Exclude a method from automatic scanning
you can exclude a function from the Doc Path list by adding the following macro #[utoipa_ignore] .
ex:
/// Get all pets from database
///
//<============== this Macro
async
Exclude a struct from automatic scanning
you can also exclude a struct from the models and reponses list by adding the following macro #[utoipa_ignore] .
ex:
//<============== this Macro
Custom path detection
By default, this macro will look for function with the #[utoipa::path(...)] attribute, but you can also specify a
custom attribute to look for.
//Custom attribute
;
You can also specify custom attributes for the model and response detection.
//Custom derive
;
Note
Sub-modules within a module containing methods tagged with utoipa::path are also automatically detected.
Contributing
Contributions are welcomed, feel free to submit a PR or an issue.
Inspiration
Inspired by utoipa_auto_discovery