Rust OpenAPI 3.1 Type Generator
oas3-gen is a command-line tool that generates idiomatic Rust type definitions from an OpenAPI v3.1.x specification. It is designed to create production-ready code that is easy to use and integrate into any Rust project.
The primary goal is to provide a robust and reliable way to generate Rust types from an OpenAPI specification, ensuring that the generated code is correct, efficient, and well-documented.
Key Features
- OpenAPI 3.1 Support: Full support for the latest OpenAPI specification.
- Idiomatic Rust Code: Generates clean, readable, and idiomatic Rust structs and enums.
- Serde Integration: Automatically derives
serde::Serializeandserde::Deserializefor seamless JSON and other format integration. - Documentation Generation: Converts schema descriptions into Rust doc comments.
- Complex Schema Support: Handles
allOf,oneOf, andanyOffor complex type compositions. - Cycle Detection: Intelligently detects and handles cyclical dependencies between schemas.
- Naming Conventions: Automatically detects
camelCaseandsnake_casenaming conventions and applies#[serde(rename_all = "...")]. - Operation Generation: Generates types for API operation parameters, request bodies, and responses.
Installation
You can install oas3-gen directly from crates.io using cargo:
Usage
To generate Rust types, provide the path to your OpenAPI specification file and the desired output file.
Example
Given the following OpenAPI schemas/pet.json:
Running oas3-gen will produce the following Rust code in src/generated_types.rs:
// src/generated_types.rs
use ;
/// Represents a pet in the store.
Command-Line Options
A rust type generator for OpenAPI v3.1.x specification.
Usage: oas3-gen [OPTIONS] --input <FILE> --output <FILE>
Options:
-i, --input <FILE> Path to the OpenAPI JSON specification file
-o, --output <FILE> Path where the generated Rust code will be written
-v, --verbose Enable verbose output with detailed progress information
-q, --quiet Suppress non-essential output (errors only)
-h, --help Print help
-V, --version Print version
License
This project is licensed under the MIT License. See the LICENSE.md file for details.
Contribution
Contributions are welcome! Please see CONTRIBUTING.md for more details on how to get started.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be licensed as MIT, without any additional terms or conditions.
See [OpenAPI v3.1.x]: https://spec.openapis.org/oas/v3.1.1