openapi-trait
A Rust proc-macro attribute that reads an OpenAPI specification file at compile time and generates a typed Rust trait from it, so you can implement your API server with full type safety and no boilerplate.
;
// Wire up an axum router.
let app = MyServer.router.with_state;
What gets generated
For every OpenAPI spec the macro emits inside the target module:
| Generated item | Source |
|---|---|
Structs with serde derives |
components/schemas |
{OperationId}Request structs |
Path, query, header params + request body per operation |
Per-operation {OperationId}Response enums |
HTTP status codes per operation |
impl axum::response::IntoResponse |
For every response enum |
{Title}Api trait |
One async fn per operationId (default impl returns 500) |
router method on the trait |
Wires all operations to an axum::Router |
Crates
| Crate | Purpose |
|---|---|
openapi-trait |
Main entry point — add this to your Cargo.toml |
openapi-trait-axum |
Axum proc-macro — not for direct use |
openapi-trait-shared |
Framework-agnostic codegen helpers — not for direct use |
Usage
Add to Cargo.toml:
[]
= "0.1"
Then apply the macro to a mod block:
The path is resolved relative to the crate root (CARGO_MANIFEST_DIR). The
file is tracked by Cargo — the crate recompiles automatically when the spec
changes.
OpenAPI support
| Feature | Status |
|---|---|
components/schemas → structs |
✅ |
| Path parameters | ✅ |
| Query parameters (including string enums) | ✅ |
| Header parameters | ✅ |
| Request bodies (JSON) | ✅ |
| Response enums per operation | ✅ |
allOf / anyOf / oneOf |
Partial — falls back to serde_json::Value |
| Security schemes | Not planned for v0.1 |