Rust OpenAPI 3.1 Type Generator
oas3-gen is a command-line interface (CLI) for generating idiomatic Rust type definitions from an OpenAPI v3.1.x specification. The tool produces clean, production-ready code designed for seamless integration into any Rust project. Its primary function is to provide a robust and reliable method for type generation, ensuring the resulting code is correct, efficient, and well-documented.
Quick Start
1. Installation
Install the tool directly from crates.io using cargo.
2. Generation
Provide a path to an OpenAPI specification and specify an output file for the generated Rust code.
Example
Consider the following OpenAPI schema definition in schemas/pet.json:
Executing oas3-gen produces the corresponding Rust types.
// src/generated_types.rs
use ;
/// Represents a pet in the store.
Key Features
- Comprehensive OpenAPI 3.1 Support: Parses schemas, parameters, request bodies, and responses from the latest OpenAPI specification.
- Idiomatic Code Generation: Creates Rust structs and enums that follow common language conventions.
- Serde Integration: Automatically derives
serde::Serializeandserde::Deserializefor immediate use with JSON and other data formats. - Documentation Generation: Converts OpenAPI schema descriptions directly into Rust documentation comments.
- Complex Schema Resolution: Correctly handles
allOf,oneOf, andanyOfcompositions to generate accurate and complex type definitions. - Cycle Detection: Intelligently detects and manages cyclical dependencies between schemas, preventing infinite recursion in type definitions.
- Convention-Aware Naming: Detects
camelCaseandsnake_casein the source schema and applies the appropriate#[serde(rename_all = "...")]attribute. - Operation Scaffolding: Generates types for API operation parameters, request bodies, and responses.
- Validation Support: Translates OpenAPI constraints (e.g.,
minLength,maxLength,pattern,minimum,maximum) into validation attributes. - Enhanced CLI Experience: Provides colored, timestamped output with automatic theme detection for improved readability in various terminal environments.
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
--visibility <VISIBILITY> Visibility level for generated types [default: public]
[possible values: public, crate, file]
-v, --verbose Enable verbose output with detailed progress information
-q, --quiet Suppress non-essential output (errors only)
--color <COLOR> Control color output [default: auto]
[possible values: always, auto, never]
--theme <THEME> Terminal theme (dark or light background) [default: auto]
[possible values: dark, light, auto]
-h, --help Print help
-V, --version Print version
Examples
# Basic usage with automatic color and theme detection
# Verbose output showing detailed statistics
# Force dark theme with always-on colors
# Generate with crate-level visibility
# Quiet mode (errors only)
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