utoipa

repository·master·Indexed 26 days ago

https://github.com/juhaku/utoipa

A Rust library for compile-time generated OpenAPI documentation. It provides procedural macros to define schemas and API documentation, supporting various web frameworks including actix-web, axum, rocket, tide, and warp. Version 5.5.0.

Tokens
36.5K
Snippets
139
Records
224
Agent score
86%

What's inside utoipa

  1. Overview of utoipa

    master

    utoipa is a Rust crate designed to provide auto-generated OpenAPI documentation for REST APIs. It follows a code-first approach, using procedural macros to annotate your code, which minimizes the need for manual YAML or JSON documentation maintenance. It is framework-agnostic, meaning it can be used standalone or integrated with various web frameworks.

    Key features include:

    • Support for OpenAPI 3.1.
    • Automatic schema collection from request bodies (via handler arguments or request_body attribute) and response bodies (via body or content attributes).
    • Support for generic types (with limitations on tuples, arrays, and slices).
    • Support for various OpenAPI visualization tools.
    • Pluggable setup for easy integration with web frameworks.
  2. Use utoipa with custom web frameworks

    master
    While specific examples are provided for popular frameworks, utoipa is compatible with any web framework that supports decorating functions with macros (similar to the patterns used in the warp and tide examples).
  3. Automatically collect OpenAPI paths and schemas from Actix Web

    master

    Use utoipa-actix-web to recursively collect paths and schemas from Actix Web App, Scope, and ServiceConfig. This reduces duplication by eliminating the need to manually declare paths and schemas in the #[openapi(...)] attribute of your OpenApi derive macro.

    Note: Automatic collection currently only works with service(...) calls. Manual routes defined via route(...) or Route::new().to(...) are not supported.

    use actix_web::{get, App};
    use utoipa_actix_web::{scope, AppExt};
    
    #[derive(utoipa::ToSchema)]
    struct User {
        id: i32,
    }
    
    #[utoipa::path(responses((status = OK, body = User)))]
    #[get("/user")]
    async fn get_user() -> Json<User> {
        Json(User { id: 1 })
    }
    
    let (_, mut api) = App::new()
        .into_utoipa_app()
        .service(scope::scope("/api/v1").service(get_user))
        .split_for_parts();
  4. Access OpenAPI documentation UIs in todo-rocket

    master

    Once the todo-rocket application is running, you can access different OpenAPI documentation interfaces at the following endpoints:

    • Swagger UI: http://localhost:8000/swagger-ui/
    • Redoc: http://localhost:8000/redoc
    • RapiDoc: http://localhost:8000/redoc (Note: The README indicates RapiDoc is also available at this path)
    • Scalar: http://localhost:8000/scalar
  5. Access specific API definitions in Swagger UI

    master

    The demo application provides multiple API definitions. You can switch between them using the "Select a definition" drop-down menu in the Swagger UI. Alternatively, you can load a specific definition directly via URL parameters using urls.primaryName:

    • API 1: http://localhost:8080/swagger-ui/?urls.primaryName=%2Fapi-doc1.json
    • API 2: http://localhost:8080/swagger-ui/?urls.primaryName=%2Fapi-doc2.json
  6. Access OpenAPI documentation UIs in todo-actix

    master

    Once the todo-actix application is running, you can access the generated OpenAPI documentation through several different UI interfaces at the following endpoints:

    • Swagger UI: http://localhost:8080/swagger-ui/
    • Redoc: http://localhost:8080/redoc
    • RapiDoc: http://localhost:8080/rapidoc
    • Scalar: http://localhost:8080/scalar
  7. Use vendored Swagger UI with utoipa-swagger-ui

    master

    The utoipa-swagger-ui-vendored crate contains the Swagger UI (version 5.32.6) assets re-packaged as a Rust crate. It is designed to be used as a build dependency for the utoipa-swagger-ui crate.

    By enabling the vendored feature on the utoipa-swagger-ui crate, you can serve Swagger UI in sandboxed environments where network access is restricted or where external assets cannot be provided.

  8. Run the todo-axum demo application

    master

    The todo-axum example is a demo axum application using in-memory storage to manage Todo items. It demonstrates how to integrate utoipa with various OpenAPI UI tools like utoipa-swagger-ui, utoipa-redoc, utoipa-rapidoc, and scalar.

    To run the application, use the following command:

    cargo run
  9. Run the todo-actix demo application

    master

    The todo-actix example is a demo actix-web application using in-memory storage. It demonstrates how to use utoipa alongside various OpenAPI UI tools like utoipa-swagger-ui, utoipa-redoc, utoipa-rapidoc, and scalar.

    To run the application, use:

    cargo run

    To enable debug logging, prepend the command with RUST_LOG=debug:

    RUST_LOG=debug cargo run
    cargo run
  10. Enable auto-discovery for OpenAPI schemas and paths

    master
    Utoipa does not currently have a built-in solution for automatic discovery of OpenAPI types and paths. To achieve auto-discovery, use the third-party crate utoipauto.