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 = petstore::NotImplemented;
async fn get_pet_by_id(
&self,
req: petstore::GetPetByIdRequest,
_auth: petstore::ApiKey,
_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.
§Validation
The non-default validation feature makes every generated model type derive
serde_valid::Validate and gain
#[validate(...)] field attributes reflecting the schema’s constraints
(minLength, minimum, pattern, minItems, uniqueItems, …). Bring the
Validate trait into scope (serde_valid is re-exported by this crate under
the feature) and call .validate():
openapi-trait = { version = "0.1", features = ["validation"] }use openapi_trait::serde_valid::Validate as _;
widget.validate()?; // Err if any declared constraint is violatedValidation is opt-in and non-enforcing — nothing calls .validate() for you,
and with the feature off the generated code is unchanged.
Re-exports§
pub use percent_encoding;pub use reqwest;pub use base64;pub use chrono;pub use form_urlencoded;pub use uuid;
Structs§
- Request
Options - Per-request transport options applied on top of the operation’s own parameters.
Traits§
- Reqwest
Client Auth - Sibling of
ReqwestClientCorefor clients that carry credentials. - Reqwest
Client Core - Shared accessors used by generated reqwest client implementations.
Attribute Macros§
- axum
- Generates typed Rust code from an
OpenAPIspecification file. - client
- Generates transport-agnostic Rust client traits from an
OpenAPIspecification file.
Derive Macros§
- Reqwest
Client - Derive support for user-owned reqwest client carrier structs.