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}Requeststruct per operation that bundles all parameters and the request body - A
{OperationId}Responseenum per operation whose variants map to HTTP status codes impl axum::response::IntoResponsefor every response enum- A
{Title}Apitrait with oneasync fnper operation, keyed byoperationId - A
routermethod on the trait that wires all operations to anaxum::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_traitattribute macro, wired to the axum backend.