Skip to main content

Crate openapi_trait

Crate openapi_trait 

Source
Expand description

Generate typed Rust traits from OpenAPI specifications.

This crate exposes the axum attribute macro, which reads an OpenAPI specification file at compile time and generates inside the annotated mod:

  • Rust structs for every schema defined in components/schemas
  • A {OperationId}Request struct per operation that bundles all parameters and the request body
  • A {OperationId}Response enum per operation whose variants map to HTTP status codes
  • impl axum::response::IntoResponse for every response enum
  • A {Title}Api trait with one async fn per operation, keyed by operationId
  • A router method on the trait that wires all operations to an axum::Router

§Quick start

#[openapi_trait::axum("openapi/petstore.yaml")]
pub mod petstore {}

#[derive(Clone)]
struct MyServer;

impl petstore::PetstoreApi for MyServer {
    type Error = std::convert::Infallible;

    async fn list_pets(
        &self,
        req: petstore::ListPetsRequest,
        state: axum::extract::State<()>,
        headers: axum::http::HeaderMap,
    ) -> Result<petstore::ListPetsResponse, Self::Error> {
        Ok(petstore::ListPetsResponse::Status200(vec![]))
    }
}

// Wire up an axum router.
let app = MyServer.router().with_state(());

See the axum macro documentation for the full list of generated items and compile-time error conditions.

Attribute Macros§

axum
The openapi_trait attribute macro, wired to the axum backend.