Crates.io | utoipauto |
lib.rs | utoipauto |
version | 0.2.0 |
source | src |
created_at | 2023-12-01 13:57:09.948769 |
updated_at | 2024-10-20 15:21:53.443181 |
description | Rust Macros to automate the addition of Paths/Schemas to Utoipa crate, simulating Reflection during the compilation phase |
homepage | https://github.com/ProbablyClem/utoipauto |
repository | https://github.com/ProbablyClem/utoipauto |
max_upload_size | |
id | 1055144 |
size | 16,127 |
Rust Macros to automate the addition of Paths/Schemas to Utoipa crate, simulating Reflection during the compilation phase
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 :
#[derive(OpenApi)]
#[openapi(
paths(
// <================================ All functions 1 to N
test_controller::service::func_get_1,
test_controller::service::func_get_2,
test_controller::service::func_get_3,
test_controller::service::func_get_4,
....
....
....
test_controller::service::func_get_N,
),
components(
// <====================== All DTO one by one
schemas(TestDTO_1, TestDTO_2, ........ , TestDTO_N)
),
tags(
(name = "todo", description = "Todo management endpoints.")
),
modifiers(&SecurityAddon)
)]
pub struct ApiDoc;
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.
Simply add the crate utoipauto
to the project
cargo add utoipauto
Import macro
use utoipauto::utoipauto;
Then add the #[utoipauto]
macro just before the #[derive(OpenApi)] and #[openapi]
macros.
Put #[utoipauto]
before #[derive(OpenApi)]
and #[openapi]
macros.
#[utoipauto(paths = "MODULE_SRC_FILE_PATH, MODULE_SRC_FILE_PATH, ...")]
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 ","
.
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.
#[utoipauto(paths = "./utoipauto/src")]
You can specify that the specified paths are from another crate by using the from key work.
#[utoipauto(paths = "./utoipauto/src from utoipauto")]
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::utoipauto;
...
#[utoipauto]
#[derive(OpenApi)]
#[openapi(
tags(
(name = "todo", description = "Todo management endpoints.")
),
modifiers(&SecurityAddon)
)]
pub struct ApiDoc;
...
Here's an example of how to add all the methods and structs contained in the rest module.
use utoipauto::utoipauto;
#[utoipauto(
paths = "./src/rest"
)]
#[derive(OpenApi)]
#[openapi(
tags(
(name = "todo", description = "Todo management endpoints.")
),
modifiers(&SecurityAddon)
)]
pub struct ApiDoc;
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::utoipauto;
#[utoipauto(
paths = "(./src/lib/rest from crate::rest)"
)]
#[derive(OpenApi)]
#[openapi(
tags(
(name = "todo", description = "Todo management endpoints.")
),
modifiers(&SecurityAddon)
)]
pub struct ApiDoc;
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::utoipauto;
#[utoipauto(
paths = "./src/rest/test_controller.rs,./src/rest/test2_controller.rs "
)]
#[derive(OpenApi)]
#[openapi(
paths(
crate::rest::other_controller::get_users,
),
components(
schemas(TestDTO)
),
tags(
(name = "todo", description = "Todo management endpoints.")
),
modifiers(&SecurityAddon)
)]
pub struct ApiDoc;
you can exclude a function from the Doc Path list by adding the following macro #[utoipa_ignore]
.
ex:
/// Get all pets from database
///
#[utoipa_ignore] //<============== this Macro
#[utoipa::path(
responses(
(status = 200, description = "List all Pets", body = [ListPetsDTO])
)
)]
#[get("/pets")]
async fn get_all_pets(req: HttpRequest, store: web::Data<AppState>) -> impl Responder {
// your CODE
}
you can also exclude a struct from the models and reponses list by adding the following macro #[utoipa_ignore]
.
ex:
#[utoipa_ignore] //<============== this Macro
#[derive(ToSchema)]
struct ModelToIgnore {
// your CODE
}
By default, this macro will look for function with the #[utoipa::path(...)]
attribute, but you can also specify a
custom attribute to look for.
#[handler]
pub fn custom_router() {
// ...
}
#[utoipauto(function_attribute_name = "handler")] //Custom attribute
#[derive(OpenApi)]
#[openapi(tags()))]
pub struct ApiDoc;
You can also specify custom attributes for the model and response detection.
#[derive(Schema, Response)]
pub struct CustomModel {
// ...
}
#[utoipauto(schema_attribute_name = "Schema", response_attribute_name = "Response")] //Custom derive
#[derive(OpenApi)]
#[openapi(tags())]
pub struct ApiDoc;
Sub-modules within a module containing methods tagged with utoipa::path are also automatically detected.
Contributions are welcomed, feel free to submit a PR or an issue.
Inspired by utoipa_auto_discovery