### Import from filename Source: https://github.com/probablyclem/utoipauto/blob/main/README.md This example shows how to import methods from specified controller files and manually add a method and a schema to the OpenAPI documentation. ```APIDOC ## Import from filename This example shows how to import methods from specified controller files and manually add a method and a schema to the OpenAPI documentation. ```rust 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; ``` ``` -------------------------------- ### Manual Utoipa Path and Schema Declaration Source: https://github.com/probablyclem/utoipauto/blob/main/README.md This example shows the traditional way of manually listing all paths and components for Utoipa. It becomes verbose with many endpoints and DTOs. ```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; ``` -------------------------------- ### Utoipauto Scan Sub-folder Module with Crate Reference Source: https://github.com/probablyclem/utoipauto/blob/main/README.md This example shows how to specify a path to a sub-folder module, `./src/lib/rest`, and explicitly link it to the crate's module path `crate::rest` using the `from` keyword. This is useful for complex workspace structures. ```rust 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; ``` -------------------------------- ### Utoipauto Scan Specific Module Directory Source: https://github.com/probablyclem/utoipauto/blob/main/README.md This example demonstrates how to configure `#[utoipauto]` to scan a specific directory, `./src/rest`, for Utoipa annotations. This helps in organizing and targeting documentation generation for particular API modules. ```rust use utoipauto::utoipauto; #[utoipauto( paths = "./src/rest" )] #[derive(OpenApi)] #[openapi( tags( (name = "todo", description = "Todo management endpoints.") ), modifiers(&SecurityAddon) )] pub struct ApiDoc; ``` -------------------------------- ### Import and Apply Utoipauto Macro (Default Scan) Source: https://github.com/probablyclem/utoipauto/blob/main/README.md Import the `utoipauto` macro and apply it before `#[derive(OpenApi)]`. When no paths are specified, it scans the entire `src` folder for Utoipa-annotated methods and schemas. ```rust use utoipauto::utoipauto; ... #[utoipauto] #[derive(OpenApi)] #[openapi( tags( (name = "todo", description = "Todo management endpoints.") ), modifiers(&SecurityAddon) )] pub struct ApiDoc; ... ``` -------------------------------- ### Auto-discover API Paths from Default './src' Folder Source: https://context7.com/probablyclem/utoipauto/llms.txt Use `#[utoipauto]` before `#[derive(OpenApi)]` and `#[openapi(...)]` to scan the entire `./src` directory recursively for `#[utoipa::path]`-annotated functions and `ToSchema`/`ToResponse` derived structs. ```rust use utoipa::OpenApi; use utoipauto::utoipauto; #[utoipauto] #[derive(OpenApi)] #[openapi( tags( (name = "pets", description = "Pet management endpoints.") ), info(title = "Pet API", version = "1.0.0") )] pub struct ApiDoc; fn main() { // Prints the complete OpenAPI JSON including all discovered paths and schemas println!("{}", ApiDoc::openapi().to_pretty_json().unwrap()); } ``` -------------------------------- ### Add Utoipauto Crate to Project Source: https://github.com/probablyclem/utoipauto/blob/main/README.md To use Utoipauto, add it as a dependency to your Cargo.toml file using the cargo add command. ```bash cargo add utoipauto ``` -------------------------------- ### Import Methods from Filename with Utoipauto Source: https://github.com/probablyclem/utoipauto/blob/main/README.md Use the `#[utoipauto]` macro to automatically discover and include methods from specified Rust files in your OpenAPI documentation. You can combine automatic discovery with manual additions of paths and schemas. ```rust 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; ``` -------------------------------- ### Legacy Arrow-Syntax Path Specification in Utoipauto Source: https://context7.com/probablyclem/utoipauto/llms.txt Demonstrates the legacy `(MODULE_TREE_PATH => MODULE_SRC_PATH)` syntax for mapping module paths to file paths. This syntax is supported for backward compatibility. ```rust use utoipa::OpenApi; use utoipauto::utoipauto; // Legacy syntax: semicolon-separated parenthesised pairs #[utoipauto( paths = "(crate::api::users => ./src/api/users.rs) ; (crate::api::orders => ./src/api/orders.rs)" )] #[derive(OpenApi)] #[openapi(info(title = "Legacy Syntax API", version = "1.0.0"))] pub struct ApiDoc; // Equivalent modern (preferred) syntax #[utoipauto( paths = "./src/api/users.rs, ./src/api/orders.rs" )] #[derive(OpenApi)] #[openapi(info(title = "Modern Syntax API", version = "1.0.0"))] pub struct ModernApiDoc; ``` -------------------------------- ### Configure Utoipauto with Specific Paths Source: https://github.com/probablyclem/utoipauto/blob/main/README.md Use the `paths` argument in the `#[utoipauto]` macro to specify a comma-separated list of file paths or directories to scan for Utoipa annotations. This is useful for targeting specific modules or sub-folders. ```rust #[utoipauto(paths = "MODULE_SRC_FILE_PATH, MODULE_SRC_FILE_PATH, ...")] ``` -------------------------------- ### Manual `ToSchema` / `ToResponse` Implementation Detection Source: https://context7.com/probablyclem/utoipauto/llms.txt Utoipauto auto-discovers types that manually implement `ToSchema` or `ToResponse` by inspecting `impl` blocks, in addition to detecting derive macros. This allows for more flexible schema definition. ```rust use std::borrow::Cow; use utoipa::{openapi::{ObjectBuilder, RefOr, Schema, ResponseBuilder}, PartialSchema, ToSchema, ToResponse, OpenApi}; use utoipauto::utoipauto; pub struct RawPayload; impl PartialSchema for RawPayload { fn schema() -> RefOr { ObjectBuilder::new().into() } } impl ToSchema for RawPayload { fn name() -> Cow<'static, str> { Cow::Borrowed("RawPayload") } } pub struct AckResponse; impl<'s> ToResponse<'s> for AckResponse { fn response() -> (&'s str, RefOr) { ("AckResponse", ResponseBuilder::new().description("Acknowledged").into()) } } #[utoipauto(paths = "./src/models")] #[derive(OpenApi)] #[openapi(info(title = "Manual Impl API", version = "1.0.0"))] pub struct ApiDoc; // Both RawPayload (schema) and AckResponse (response) are auto-registered ``` -------------------------------- ### Specify API Path Discovery with `paths` Argument Source: https://context7.com/probablyclem/utoipauto/llms.txt Restrict API path discovery to specific directories or `.rs` files using the `paths` argument in `#[utoipauto(paths = "...")]`. Multiple paths can be provided, separated by commas. ```rust use utoipa::OpenApi; use utoipauto::utoipauto; // Scan only the ./src/rest directory #[utoipauto(paths = "./src/rest")] #[derive(OpenApi)] #[openapi( tags((name = "users", description = "User endpoints.")), info(title = "User API", version = "2.0.0") )] pub struct ApiDoc; // Scan two specific controller files #[utoipauto( paths = "./src/rest/users_controller.rs, ./src/rest/orders_controller.rs" )] #[derive(OpenApi)] #[openapi( paths( // Still possible to add paths manually alongside auto-discovered ones crate::rest::health_controller::health_check, ), info(title = "Partial API", version = "1.0.0") )] pub struct PartialApiDoc; ``` -------------------------------- ### Utoipauto Usage with Workspace Crate Paths Source: https://github.com/probablyclem/utoipauto/blob/main/README.md When using Utoipauto within a workspace, specify the crate name in the `paths` argument, even if applying the macro to the same crate. The `from` keyword can be used to explicitly link the path to a crate. ```rust #[utoipauto(paths = "./utoipauto/src from utoipauto")] ``` -------------------------------- ### Custom path detection Source: https://github.com/probablyclem/utoipauto/blob/main/README.md Explains how to specify a custom attribute for path detection in `utoipauto` when the default `#[utoipa::path(...)]` is not used. ```APIDOC ## 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. ```rust #[handler] pub fn custom_router() { // ... } #[utoipauto(function_attribute_name = "handler")] //Custom attribute #[derive(OpenApi)] #[openapi(tags()))] pub struct ApiDoc; ``` ``` -------------------------------- ### Specify Crate Name in Cargo Workspaces with `from` Source: https://context7.com/probablyclem/utoipauto/llms.txt When using Utoipauto in a Cargo workspace, specify the crate name using the `from` keyword in the `paths` argument to map paths correctly. The path is relative to the workspace root. ```rust // In your API crate's src/lib.rs (workspace member: "my-api") use utoipa::OpenApi; use utoipauto::utoipauto; // Path is relative to the workspace root; "from my-api" maps it to crate::... #[utoipauto(paths = "./my-api/src from my-api")] #[derive(OpenApi)] #[openapi(info(title = "Workspace API", version = "1.0.0"))] pub struct ApiDoc; // Scan paths from another workspace crate #[utoipauto(paths = "./shared-models/src from shared_models")] #[derive(OpenApi)] #[openapi(info(title = "Cross-Crate API", version = "1.0.0"))] pub struct CrossCrateApiDoc; ``` -------------------------------- ### Custom Model and Response Detection with Utoipauto Source: https://github.com/probablyclem/utoipauto/blob/main/README.md Specify custom attributes for model and response detection using `schema_attribute_name` and `response_attribute_name` in the `#[utoipauto]` macro. ```rust #[derive(Schema, Response)] pub struct CustomModel { // ... } #[utoipauto(schema_attribute_name = "Schema", response_attribute_name = "Response")] //Custom derive #[derive(OpenApi)] #[openapi(tags())] pub struct ApiDoc; ``` -------------------------------- ### Custom Derive Detection with `schema_attribute_name` and `response_attribute_name` Source: https://context7.com/probablyclem/utoipauto/llms.txt Customize the derive macro names utoipauto detects for schema and response registration. Use `schema_attribute_name` for `ToSchema` and `response_attribute_name` for `ToResponse`. ```rust use utoipa::OpenApi; use utoipauto::utoipauto; // Custom derives that internally implement ToSchema / ToResponse #[derive(MySchema)] pub struct OrderDto { pub id: u64 } #[derive(MyResponse)] pub struct OrderCreatedResponse { pub order_id: u64 } #[utoipauto( paths = "./src/dto", schema_attribute_name = "MySchema", response_attribute_name = "MyResponse" )] #[derive(OpenApi)] #[openapi(info(title = "Orders API", version = "1.0.0"))] pub struct ApiDoc; // OrderDto is registered in components.schemas // OrderCreatedResponse is registered in components.responses ``` -------------------------------- ### Custom model and response detection Source: https://github.com/probablyclem/utoipauto/blob/main/README.md Shows how to specify custom derive attributes for model and response detection when they differ from the default `Schema` and `Response`. ```APIDOC You can also specify custom attributes for the model and response detection. ```rust #[derive(Schema, Response)] pub struct CustomModel { // ... } #[utoipauto(schema_attribute_name = "Schema", response_attribute_name = "Response")] //Custom derive #[derive(OpenApi)] #[openapi(tags())] pub struct ApiDoc; ``` ``` -------------------------------- ### Custom Path Detection with Utoipauto Source: https://github.com/probablyclem/utoipauto/blob/main/README.md Customize the attribute utoipauto looks for when detecting functions for path generation by specifying `function_attribute_name` in the macro. ```rust #[handler] pub fn custom_router() { // ... } #[utoipauto(function_attribute_name = "handler")] //Custom attribute #[derive(OpenApi)] #[openapi(tags())] pub struct ApiDoc; ``` -------------------------------- ### Automatic Generic Type Registration Source: https://context7.com/probablyclem/utoipauto/llms.txt Utoipauto automatically handles and registers generic structs with type and lifetime parameters when they derive `ToSchema` or `ToResponse`. This ensures generic types are discovered and included in the OpenAPI schema. ```rust use utoipa::{ToSchema, ToResponse, OpenApi}; use utoipauto::utoipauto; #[derive(ToSchema, ToResponse)] pub struct ApiResponse { pub status: u16, pub data: T, } #[derive(ToSchema, ToResponse)] pub struct BorrowedPayload<'a> { pub content: &'a str, } #[derive(ToSchema, ToResponse)] pub struct NestedResponse<'a, T: ToSchema> { pub status: u16, pub data: &'a T, } #[utoipauto(paths = "./src")] #[derive(OpenApi)] #[openapi(info(title = "Generic API", version = "1.0.0"))] pub struct ApiDoc; fn main() { // All three generic structs are registered in components.schemas and components.responses println!("{}", ApiDoc::openapi().to_pretty_json().unwrap()); } ``` -------------------------------- ### Custom Function Attribute Detection with `function_attribute_name` Source: https://context7.com/probablyclem/utoipauto/llms.txt Configure utoipauto to detect custom function attribute names instead of the default `#[utoipa::path(...)]`. This is necessary when using wrapper macros that expand to `#[utoipa::path(...)]`. ```rust use utoipa::OpenApi; use utoipauto::utoipauto; // Suppose "my_route" is a proc-macro that internally applies #[utoipa::path(get, ...)] #[my_route] pub fn get_items() {} #[my_route] pub fn create_item() {} // Tell utoipauto to scan for #[my_route] instead of #[utoipa::path] #[utoipauto( paths = "./src/handlers", function_attribute_name = "my_route" )] #[derive(OpenApi)] #[openapi(info(title = "Custom Handler API", version = "1.0.0"))] pub struct ApiDoc; // Both get_items and create_item are registered automatically ``` -------------------------------- ### Ignore Structs with #[utoipa_ignore] Source: https://context7.com/probablyclem/utoipauto/llms.txt Use the `#[utoipa_ignore]` macro on structs to prevent them from being automatically registered in the OpenAPI schema components. This is useful for internal or non-public data structures. ```rust use utoipa::{ToSchema, ToResponse}; use utoipauto::utoipa_ignore; #[derive(ToSchema)] pub struct PublicDto { pub id: u64, pub name: String, } /// This internal struct is kept out of the OpenAPI schema components #[utoipa_ignore] #[derive(ToSchema)] pub struct InternalAuditRecord { pub timestamp: u64, pub actor: String, } #[derive(ToResponse)] pub struct SuccessResponse { pub message: String, } #[utoipa_ignore] #[derive(ToResponse)] pub struct InternalErrorDetail { pub trace_id: String, } // Only PublicDto and SuccessResponse appear in the generated OpenAPI components ``` -------------------------------- ### Exclude a method from automatic scanning Source: https://github.com/probablyclem/utoipauto/blob/main/README.md Demonstrates how to exclude a specific function from automatic scanning for OpenAPI documentation by using the `#[utoipa_ignore]` macro. ```APIDOC ## Exclude a method from automatic scanning This example shows how to exclude a specific function from automatic scanning for OpenAPI documentation by using the `#[utoipa_ignore]` macro. ```rust /// 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) -> impl Responder { // your CODE } ``` ``` -------------------------------- ### Exclude Functions from Auto-Discovery with `#[utoipa_ignore]` Source: https://context7.com/probablyclem/utoipauto/llms.txt Apply `#[utoipa_ignore]` to a handler function to prevent Utoipauto from registering it in the OpenAPI spec, even if it has `#[utoipa::path(...)]`. The function remains functional. ```rust use utoipa::OpenApi; use utoipauto::{utoipauto, utoipa_ignore}; /// This route IS included in the generated OpenAPI spec #[utoipa::path(get, path = "/pets", responses((status = 200, description = "List all pets", body = Vec)) )] pub async fn list_pets() -> Vec { vec![] } /// This internal route is EXCLUDED from the spec #[utoipa_ignore] #[utoipa::path(get, path = "/internal/debug", responses((status = 200, description = "Debug endpoint")) )] pub async fn debug_endpoint() -> &'static str { "ok" } #[utoipauto(paths = "./src/routes")] #[derive(OpenApi)] #[openapi(info(title = "Pet API", version = "1.0.0"))] pub struct ApiDoc; // Result: ApiDoc only contains /pets, not /internal/debug ``` -------------------------------- ### Exclude Method from Automatic Scanning with Utoipa Ignore Source: https://github.com/probablyclem/utoipauto/blob/main/README.md To exclude a specific function from being automatically scanned and included in the documentation paths, apply the `#[utoipa_ignore]` macro directly above the function definition. ```rust /// 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) -> impl Responder { // your CODE } ``` -------------------------------- ### Exclude a struct from automatic scanning Source: https://github.com/probablyclem/utoipauto/blob/main/README.md Illustrates how to exclude a struct from being included in the models and responses list of the OpenAPI documentation using the `#[utoipa_ignore]` macro. ```APIDOC ## Exclude a struct from automatic scanning This example shows how to exclude a struct from being included in the models and responses list of the OpenAPI documentation using the `#[utoipa_ignore]` macro. ```rust #[utoipa_ignore] //<============== this Macro #[derive(ToSchema)] struct ModelToIgnore { // your CODE } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.