Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTO discovery #5

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4815e1d
spliting utils in other files
ProbablyClem Nov 27, 2023
8ea13da
refacto: splitting into smaller functions and move them to new files
ProbablyClem Nov 27, 2023
1d69930
fix: skip non openapi lines
ProbablyClem Nov 27, 2023
d1a0611
add quick tests
ProbablyClem Nov 27, 2023
780e67b
add import from a module tests
ProbablyClem Nov 27, 2023
97cd293
error if file cannot be parsed
ProbablyClem Nov 27, 2023
3fbb470
Merge branch 'error-on-parse' into discover-module
ProbablyClem Nov 27, 2023
2cc9089
tests
ProbablyClem Nov 28, 2023
674b5bf
refacto discover
ProbablyClem Nov 28, 2023
13ff3e6
spliting into two crates for debugging
ProbablyClem Nov 28, 2023
7c1ccb4
splitting into crates
ProbablyClem Nov 28, 2023
4978bce
working discovering from folder
ProbablyClem Nov 28, 2023
a1ab093
to level crate
ProbablyClem Nov 29, 2023
3e19128
workiing dir import
ProbablyClem Nov 29, 2023
5da430e
working parse crate root
ProbablyClem Nov 29, 2023
107cb1f
new syntax support
ProbablyClem Nov 29, 2023
3920b1a
doc
ProbablyClem Nov 29, 2023
eafe856
parse only rs
ProbablyClem Nov 29, 2023
b5e1045
use src if nothing specified
ProbablyClem Nov 29, 2023
e04738e
doc
ProbablyClem Nov 29, 2023
80edb0b
uncomment tests
ProbablyClem Nov 29, 2023
9383e8c
remove print
ProbablyClem Nov 29, 2023
2a38915
Merge branch 'discover-module' into default
ProbablyClem Nov 29, 2023
6c1e1c8
Update README.md
ProbablyClem Nov 29, 2023
58102a6
manual path test
ProbablyClem Nov 30, 2023
aff88a2
test build new attributes
ProbablyClem Nov 30, 2023
43960c1
add unit tests for schema and responses replacement
ProbablyClem Nov 30, 2023
02a837e
Update attribute_utils.rs
ProbablyClem Nov 30, 2023
78441dc
update attributes with paths and responses
ProbablyClem Nov 30, 2023
ee9d9c3
refacto to allows for trait parsing
ProbablyClem Nov 30, 2023
9c1e387
working models and response imports
ProbablyClem Dec 1, 2023
0287cbb
lint
ProbablyClem Dec 1, 2023
c5fb909
Update controller1.rs
ProbablyClem Dec 1, 2023
47f7ee8
Update controller2.rs
ProbablyClem Dec 1, 2023
3dc7cf6
Update README.md
ProbablyClem Dec 1, 2023
5c135c0
itoipa_ignore on struct
ProbablyClem Dec 1, 2023
9e564da
add comments
ProbablyClem Dec 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/target
*target
/Cargo.lock
git_cmd.md
git_cmd.md
27 changes: 14 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
[package]
name = "utoipa_auto_discovery"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
authors = ["RxDiscovery"]
rust-version = "1.69"
keywords = ["utoipa","openapi","swagger", "path", "auto"]
authors = ["RxDiscovery", "ProbablyClem"]
rust-version = "1.69"
keywords = ["utoipa", "openapi", "swagger", "path", "auto"]
description = "Rust Macros to automate the addition of Paths/Schemas to Utoipa crate, simulating Reflection during the compilation phase"
categories = ["parsing","development-tools::procedural-macro-helpers","web-programming"]
categories = [
"parsing",
"development-tools::procedural-macro-helpers",
"web-programming",
]
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/rxdiscovery/utoipa_auto_discovery"
homepage = "https://github.com/rxdiscovery/utoipa_auto_discovery"


[lib]
proc-macro = true

[dependencies]
quote = "1.0.28"
syn = { version ="2.0.18", features = [ "full" ]}
proc-macro2 = "1.0.59"

[dependencies]
utoipa-auto-macro = { version = "0.4.0", path = "./utoipa-auto-macro" }


[build-dependencies]
[dev-dependencies]
utoipa = { version = "4.1.0", features = ["preserve_path_order"] }
syn = { version = "2.0.39", features = ["extra-traits", "full"] }
123 changes: 94 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ Utoipa is a great crate for generating documentation (openapi/swagger) via sourc

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.
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.
But, if you have hundreds or even thousands of endpoints, the code becomes very verbose and difficult to maintain.

ex :
Ex :

```rust

...

#[derive(OpenApi)]
#[openapi(
paths(
// <================================ all functions 1 to N
// <================================ All functions 1 to N
test_controller::service::func_get_1,
test_controller::service::func_get_2,
test_controller::service::func_get_3,
Expand All @@ -51,53 +49,110 @@ ex :
)]
pub struct ApiDoc;

...

```

The aim of crate **utoipa_auto_discovery** 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.)
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 `ToSchema` for the `components(schemas)` section, and the `ToResponse` for the `components(responses)` section.

# Features

- [x] Automatic recursive path detection
- [x] Automatic import from module
- [x] Automatic import from src folder
- [x] Automatic model detection
- [x] Automatic response detection

# how to use it
# How to use it

simply add the crate `utoipa_auto_discovery` to the project
Simply add the crate `utoipa_auto_discovery` to the project

```
cargo add utoipa_auto_discovery
```

import macro
Import macro

```rust
use utoipa_auto_discovery::utoipa_auto_discovery;
```

then add the `#[utoipa_auto_discovery]` macro just before the #[derive(OpenApi)] and `#[openapi]` macros.
Then add the `#[utoipa_auto_discovery]` macro just before the #[derive(OpenApi)] and `#[openapi]` macros.

## important !!
## Important !!

Put `#[utoipa_auto_discovery]` before #[derive(OpenApi)] and `#[openapi]` macros.
Put `#[utoipa_auto_discovery]` before `#[derive(OpenApi)] `and `#[openapi]` macros.

```rust
#[utoipa_auto_discovery(paths = "( MODULE_TREE::MODULE_NAME => MODULE_SRC_FILE_PATH ) ; ( MODULE_TREE::MODULE_NAME => MODULE_SRC_FILE_PATH ) ; ... ;")]
#[utoipa_auto_discovery(paths = "MODULE_SRC_FILE_PATH, MODULE_SRC_FILE_PATH, ...")]
```

the paths receives a String which must respect this structure :
The paths receives a String which must respect this structure :

`" ( MODULE_TREE_PATH => MODULE_SRC_FILE_PATH ) ;"`
`"MODULE_SRC_FILE_PATH, MODULE_SRC_FILE_PATH, ..."`

you can add several pairs (Module Path => Src Path ) by separating them with a semicolon ";".
You can add several paths by separating them with a coma `","`.

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".
### 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.

```rust
...

use utoipa_auto_discovery::utoipa_auto_discovery;

...
#[utoipa_auto_discovery]
#[derive(OpenApi)]
#[openapi(
tags(
(name = "todo", description = "Todo management endpoints.")
),
modifiers(&SecurityAddon)
)]

pub struct ApiDoc;

...

```

### Import from module

Here's an example of how to add all the methods and structs contained in the rest module.

```rust

use utoipa_auto_discovery::utoipa_auto_discovery;

#[utoipa_auto_discovery(
paths = "( crate::rest::test_controller => ./src/rest/test_controller.rs ) ; ( crate::rest::test2_controller => ./src/rest/test2_controller.rs )"
paths = "./src/rest"
)]
#[derive(OpenApi)]
#[openapi(
tags(
(name = "todo", description = "Todo management endpoints.")
),
modifiers(&SecurityAddon)
)]

pub struct ApiDoc;

```

### 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".

```rust

use utoipa_auto_discovery::utoipa_auto_discovery;

#[utoipa_auto_discovery(
paths = "./src/rest/test_controller.rs,./src/rest/test2_controller.rs "
)]
#[derive(OpenApi)]
#[openapi(
Expand All @@ -116,11 +171,11 @@ use utoipa_auto_discovery::utoipa_auto_discovery;

pub struct ApiDoc;

...


```

## exclude a method of automatic scanning
## Exclude a method from automatic scanning

you can exclude a function from the Doc Path list by adding the following macro `#[utoipa_ignore]` .

Expand All @@ -142,11 +197,21 @@ ex:

```

## note
## Exclude a struct from automatic scanning

sub-modules within a module containing methods tagged with utoipa::path are also automatically detected.
you can also exclude a struct from the models and reponses list by adding the following macro `#[utoipa_ignore]` .

# Features
ex:

```rust
#[utoipa_ignore] //<============== this Macro
#[derive(ToSchema)]
struct ModelToIgnore {
// your CODE
}

```

## Note

- [x] automatic path detection
- [ ] automatic schema detection (in progress)
Sub-modules within a module containing methods tagged with utoipa::path are also automatically detected.
Loading