OpenAPI Model Generator
A Rust library and CLI tool for generating Rust models from OpenAPI specifications. This utility automatically creates Rust structures based on schemas from OpenAPI (Swagger) documentation.
Features
- OpenAPI 3.0 specification support with full schema parsing
- YAML and JSON format support for input specifications
- Automatic generation of Rust structures with Serde attributes
- Schema Composition Support: Complete implementation of OpenAPI composition patterns:
allOf- Combines multiple schemas into a single structoneOf/anyOf- Generates tagged union enums with proper serde configuration
- Advanced Type Support:
- Enum Support - Automatically generates Rust enums from OpenAPI string schemas with enumeration constraints.
- UUID fields (
format: uuid→Uuidtype) - DateTime fields (
format: date-time→DateTime<Utc>type) - Nested types and arrays with proper generic handling
- Custom Type Support:
x-rust-typeextension - Replace generated models with custom Rust types (type aliases)x-rust-attrsextension - Add custom Rust attributes to generated types- Works with any schema type (object, enum, oneOf, etc.)
- Request Bodies Support: Full parsing and model generation from
components.requestBodies - Smart Code Generation:
- Required vs optional field detection (
Option<T>for nullable fields) - PascalCase naming for generated request/response models
- Reference resolution across schema definitions
- Required vs optional field detection (
- Clean Code Output: Properly formatted Rust code with comprehensive serde annotations
Installation
As a CLI tool
As a library dependency
Add to your Cargo.toml:
[]
= "0.4.1"
Usage
Command Line Interface
Parameters
-i, --input- Path to the OpenAPI specification file (YAML or JSON)-o, --output- Path to the output directory (default: ./generated)
Library Usage
use ;
use fs;
// Parse OpenAPI specification
let openapi_spec = read_to_string?;
let openapi: OpenAPI = from_str?;
// Generate models
let = parse_openapi?;
let generated_code = generate_models?;
// Write to file
write?;
Example
Source OpenAPI schema:
components:
schemas:
User:
type: object
properties:
id:
type: string
format: uuid
name:
type: string
email:
type: string
age:
type: integer
is_active:
type: boolean
required:
- id
- name
- email
Generated Rust code:
use ;
use Uuid;
use ;
Using Custom Types with x-rust-type
You can use the x-rust-type extension to replace generated models with your own custom types:
components:
schemas:
User:
type: object
x-rust-type: crate::domain::User
description: "Custom domain user type"
properties:
id:
type: string
format: uuid
name:
type: string
Status:
type: string
enum:
x-rust-type: common::enums::Status
Generated Rust code:
/// Custom domain user type
pub type User = crateUser;
/// Status
pub type Status = Status;
This allows you to:
- Reuse existing domain models instead of generating duplicates
- Integrate with types from other crates
- Maintain a clean separation between API models and domain models
Using Custom Attributes with x-rust-attrs
You can use the x-rust-attrs extension to add arbitrary Rust attributes to generated types:
components:
schemas:
User:
type: object
x-rust-attrs:
- "#[serde(rename_all = \"camelCase\")]"
properties:
user_id:
type: string
format: uuid
first_name:
type: string
is_active:
type: boolean
required:
- user_id
- first_name
Status:
type: string
enum:
x-rust-attrs:
- "#[serde(rename_all = \"UPPERCASE\")]"
Product:
type: object
x-rust-attrs:
- "#[derive(Serialize, Deserialize)]"
- "#[serde(deny_unknown_fields)]"
properties:
id:
type: string
name:
type: string
Generated Rust code:
/// User
/// Status
/// Product
This allows you to:
- Add custom serde rules (rename_all, deny_unknown_fields, tag, etc.)
- Enable additional trait derives
- Apply conditional compilation attributes (cfg, cfg_attr)
- Use custom validation macros
- Works together with
x-rust-typeextension
Development
Dependencies
- Rust 1.70 or higher
- Cargo
Building
Running Tests
Examples
The generator supports complex OpenAPI patterns including schema composition:
- UUID and DateTime handling - Automatic type conversion for formatted strings
- Schema composition with allOf - Inheritance and field merging
- Tagged unions with oneOf/anyOf - Automatic enum generation with proper serde tags
Changelog
See CHANGELOG.md for detailed information about releases and changes.
License
MIT