OpenAPI Client Generator
A Rust procedural macro crate that generates type-safe, async HTTP clients from OpenAPI 3.0 specifications.
Features
- 🚀 Zero-runtime dependencies - Pure compile-time code generation
- 🔒 Type-safe - Full Rust type system integration with proper error handling
- 📚 Auto-documented - Generates comprehensive documentation from OpenAPI descriptions
- 🛡️ Keyword-safe - Handles Rust keywords automatically with proper escaping
- ⚡ Async/await - Built on
reqwestwith full async support - 🎯 Easy to use - Simple macro interface with sensible defaults
- 🔧 Flexible - Supports both JSON and YAML OpenAPI specifications
Quick Start
Add this to your Cargo.toml:
[]
= "0.3"
= { = "0.11", = ["json"] }
= { = "1.0", = ["full"] }
= { = "1.0", = ["derive"] }
= "1.0"
= "1.0"
Basic Usage
use openapi_client;
// Generate client from OpenAPI spec with auto-generated name
openapi_client!;
// Or specify a custom client name
openapi_client!;
async
Generated Code
The macro generates:
1. Type-Safe Structs
/// Represents a user in the system with comprehensive profile information.
2. Async Client Methods
3. Comprehensive Documentation
The generated client includes:
- API information from the OpenAPI
infosection - Method documentation from operation summaries and descriptions
- Type documentation from schema descriptions
- Field documentation from property descriptions
OpenAPI Feature Support
| Feature | Support | Notes |
|---|---|---|
| HTTP Methods | ✅ | GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE |
| Path Parameters | ✅ | Automatic URL template substitution |
| Query Parameters | ✅ | Optional and required parameters |
| Request Bodies | ✅ | JSON request bodies |
| Response Types | ✅ | Typed response parsing |
| Schema References | ✅ | $ref resolution for reusable components |
| Enums | ✅ | String enumerations with proper Rust enum generation |
| Arrays | ✅ | Vec<T> generation for array types |
| Objects | ✅ | Struct generation with proper field types |
| Optional Fields | ✅ | Option<T> for non-required fields |
| Nested Objects | ✅ | Complex object hierarchies |
| Type Aliases | ✅ | Simple type aliases |
| Rust Keywords | ✅ | Automatic escaping with r# or _ suffix |
Rust Keyword Handling
The generator automatically handles Rust keywords in:
- Field names:
type→r#type,self→self_ - Parameter names:
const→r#const - Method names: Derived from operation IDs with keyword escaping
Special handling for self and Self (cannot be raw identifiers):
self→self_Self→Self_
Error Handling
The generated client includes a comprehensive error type:
pub type ApiResult<T> = ;
Configuration
Client Customization
// Basic client
let client = new;
// Client with custom HTTP client
let http_client = builder
.timeout
.build?;
let client = with_client;
Examples
Complete Example
use openapi_client;
use json;
// Generate the client
openapi_client!;
async
Requirements
- Rust: 2024 edition or later
- OpenAPI: 3.0.x specifications (JSON or YAML)
Dependencies
Runtime dependencies (required in your project):
reqwest- HTTP client with JSON supportserde- Serialization framework (with derive feature)serde_json- JSON serializationthiserror- Error handlingtokio- Async runtime
Build-time dependencies (used by the macro):
proc-macro2,quote,syn- Procedural macro infrastructureopenapiv3- OpenAPI 3.0 specification parsingserde_yaml- YAML parsing for specsheck- Case conversionstokio- For compile-time URL fetching
Development
Building
Testing
Documentation
The project includes a comprehensive test OpenAPI specification that validates all crate features.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.