utoipa_auto_discovery

Rust Macros to automate the addition of Paths/Schemas to Utoipa crate, simulating Reflection during the compilation phase

![MSRV](https://img.shields.io/badge/rustc-1.69+-ab6000.svg)

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.

if you have hundreds or even thousands of endpoints, the code becomes very verbose and difficult to maintain.

ex :

```rust

...

[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 aim of crate utoipaautodiscovery 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.)

how to use it

simply add the crate utoipa_auto_discovery to the project

cargo add utoipa_auto_discovery

then add the #[utoipa_auto_discovery] macro just before the #[openapi] macro.

```rust

[utoipaautodiscovery(paths = "MODULETREE::MODULENAME | MODULESRCFILEPATH; MODULETREE::MODULENAME | MODULESRCFILEPATH; ... ;")]

```

the paths receives a String which must respect this structure :

"{MODULE_TREE_PATH} | {MODULE_SRC_FILE_PATH} ;"

you can add several pairs (Module Path | Src Path ) by separating them with a semicolon ";".

Here's an example of how to add all the methods contained in the testcontroller and test2controller modules. you can also combine automatic and manual addition, as here we've added a method manually to the documentation "othercontroller::getusers".

```rust

...

[derive(OpenApi)]

[utoipaautodiscovery(

paths = "crate::rest::testcontroller | ./src/rest/testcontroller.rs ; crate::rest::test2controller | ./src/rest/test2controller.rs")]

[openapi(

paths(

    crate::rest::other_controller::get_users,
),
components(
    schemas(TestDTO)
),
tags(
    (name = "todo", description = "Todo management endpoints.")
),
modifiers(&SecurityAddon)

)]

pub struct ApiDoc;

...

```

note

sub-modules within a module containing methods tagged with utoipa::path are also automatically detected.

Features