| Crates.io | openapi-model-generator |
| lib.rs | openapi-model-generator |
| version | 0.4.1 |
| created_at | 2025-07-14 21:05:50.314213+00 |
| updated_at | 2025-12-05 12:23:55.056146+00 |
| description | CLI tool for generating Rust models from OpenAPI v3 specs |
| homepage | |
| repository | https://github.com/denislituev/openapi-model-generator |
| max_upload_size | |
| id | 1752249 |
| size | 121,180 |
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.
allOf - Combines multiple schemas into a single structoneOf / anyOf - Generates tagged union enums with proper serde configurationformat: uuid → Uuid type)format: date-time → DateTime<Utc> type)x-rust-type extension - Replace generated models with custom Rust types (type aliases)x-rust-attrs extension - Add custom Rust attributes to generated typescomponents.requestBodiesOption<T> for nullable fields)cargo install openapi-model-generator
Add to your Cargo.toml:
[dependencies]
openapi-model-generator = "0.4.1"
omg -i path/to/openapi.yaml -o ./generated
-i, --input - Path to the OpenAPI specification file (YAML or JSON)-o, --output - Path to the output directory (default: ./generated)use openapi_model_generator::{parse_openapi, generate_models};
use std::fs;
// Parse OpenAPI specification
let openapi_spec = fs::read_to_string("openapi.yaml")?;
let openapi: openapiv3::OpenAPI = serde_yaml::from_str(&openapi_spec)?;
// Generate models
let (models, requests, responses) = parse_openapi(&openapi)?;
let generated_code = generate_models(&models, &requests, &responses)?;
// Write to file
fs::write("models.rs", generated_code)?;
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 serde::{Serialize, Deserialize};
use uuid::Uuid;
use chrono::{DateTime, NaiveDate, Utc};
#[derive(Debug, Serialize, Deserialize)]
pub struct User {
#[serde(rename = "id")]
pub id: Uuid,
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "email")]
pub email: String,
#[serde(rename = "age")]
pub age: Option<i64>,
#[serde(rename = "is_active")]
pub is_active: Option<bool>,
}
x-rust-typeYou 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: [active, inactive, pending]
x-rust-type: common::enums::Status
Generated Rust code:
/// Custom domain user type
pub type User = crate::domain::User;
/// Status
pub type Status = common::enums::Status;
This allows you to:
x-rust-attrsYou 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: [ACTIVE, INACTIVE, PENDING]
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
#[serde(rename_all = "camelCase")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
pub user_id: Uuid,
pub first_name: String,
pub is_active: Option<bool>,
}
/// Status
#[serde(rename_all = "UPPERCASE")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Status {
ACTIVE,
INACTIVE,
PENDING
}
/// Product
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Product {
pub id: Option<String>,
pub name: Option<String>,
}
This allows you to:
x-rust-type extensioncargo build
cargo test
The generator supports complex OpenAPI patterns including schema composition:
See CHANGELOG.md for detailed information about releases and changes.
MIT