#axum #swagger #openapi

macro okapi-operation-macro

Macro implementation for okapi-operation

8 releases

0.3.0 Jun 15, 2025
0.3.0-rc4 Mar 13, 2025
0.2.0 Aug 7, 2024
0.1.4 Jul 18, 2024
0.1.0 Jul 10, 2022

#85 in #swagger

Download history 1398/week @ 2025-08-11 1571/week @ 2025-08-18 2185/week @ 2025-08-25 2578/week @ 2025-09-01 2482/week @ 2025-09-08 1502/week @ 2025-09-15 1560/week @ 2025-09-22 1878/week @ 2025-09-29 1464/week @ 2025-10-06 1577/week @ 2025-10-13 1799/week @ 2025-10-20 1783/week @ 2025-10-27 1396/week @ 2025-11-03 1888/week @ 2025-11-10 2086/week @ 2025-11-17 1890/week @ 2025-11-24

7,293 downloads per month
Used in okapi-operation

MIT license

47KB
1K SLoC

okapi-operation

Crates.io docs.rs CI

Library which allow to generate OpenAPI's operation definitions (using types from okapi crate) with procedural macro #[openapi].

Example (with axum-integration feature).

use axum::{Json, extract::Query};
use okapi_operation::{axum_integration::*, *};
use serde::Deserialize;

#[derive(Deserialize, JsonSchema)]
struct Request {
    /// Echo data
    data: String,
}

#[openapi(
    summary = "Echo using GET request",
    operation_id = "echo_get",
    tags = "echo",
    parameters(
        query(name = "echo-data", required = true, schema = "std::string::String",),
        header(name = "x-request-id", schema = "std::string::String",)
    )
)]
async fn echo_get(query: Query<Request>) -> Json<String> {
    Json(query.0.data)
}

#[openapi(
    summary = "Echo using POST request",
    operation_id = "echo_post",
    tags = "echo"
)]
async fn echo_post(
    #[body(description = "Echo data", required = true)] body: Json<Request>,
) -> Json<String> {
    Json(body.0.data)
}

fn main() {
    let app = Router::new()
        .route("/echo/get", get(openapi_handler!(echo_get)))
        .route("/echo/post", post(openapi_handler!(echo_post)))
        .finish_openapi("/openapi", "Demo", "1.0.0")
        .expect("no problem");
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app.into_make_service())
        .await
        .unwrap()
}

Features

  • macro: enables re-import of #[openapi] macro (enabled by default);
  • axum-integration: enables integration with axum(https://github.com/tokio-rs/axum) crate (implement traits for certain axum types):
    • Compatibility with axum: since integration heavely rely on axum types, this crate will be compatible only with few (maybe even one) last versions of axum;
    • Currently supported axum versions: 0.7.x.
  • yaml: enables ability to serve the spec in yaml format in case of present Accept header with yaml value. Otherwise, in case of values json|*/* or empty, json's being served (currently affects only axum-integration).

TODO

  • support examples on MediaType or Parameter (examples supported on types via JsonSchema macro)
  • support inferring schemas of parameters from function definitions
  • support for renaming or changing paths to okapi/schemars/okapi-operations in macro
  • more examples
  • introduce MSRV policy
  • ...

Dependencies

~0.6–1MB
~22K SLoC