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 and client attribute macros, which read an OpenAPI specification file at compile time and generate inside the annotated mod.

§Examples

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

use petstore::PetstoreApi as _;

#[derive(Clone)]
struct MyServer;

#[derive(Clone)]
struct AppState;

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

    async fn get_pet_by_id(
        &self,
        req: petstore::GetPetByIdRequest,
        _state: axum::extract::State<AppState>,
        _headers: axum::http::HeaderMap,
    ) -> Result<petstore::GetPetByIdResponse, Self::Error> {
        Ok(petstore::GetPetByIdResponse::Status200(petstore::Pet {
            id: Some(req.pet_id),
            name: "doggie".into(),
            photo_urls: vec![],
            category: None,
            tags: None,
            status: None,
        }))
    }
}

let app: axum::Router = MyServer.router().with_state(AppState);

The generated trait names come from the annotated module name, so mod petstore {} produces petstore::PetstoreApi and petstore::PetstoreClient.

The reqwest-client feature is enabled by default. It adds ReqwestClient, ReqwestClientCore, and the reqwest re-export used by the generated blanket client implementation.

Re-exports§

pub use percent_encoding;
pub use reqwest;

Traits§

ReqwestClientCore
Shared accessors used by generated reqwest client implementations.

Attribute Macros§

axum
Generates typed Rust code from an OpenAPI specification file.
client
Generates transport-agnostic Rust client traits from an OpenAPI specification file.

Derive Macros§

ReqwestClient
Derive support for user-owned reqwest client carrier structs.