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.
# generate types (structs and enums)
# generate client operations
Example
Consider the following OpenAPI schema definition in schemas/pet.json:
Executing oas3-gen produces the corresponding Rust types.
// src/types.rs
use ;
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.
- Operation Filtering: Selectively generate code for specific operations using
--onlyor exclude operations with--excludefor fine-grained control.
Command-Line Options
OpenAPI to Rust code generator
Usage: oas3-gen [OPTIONS] <COMMAND>
Commands:
list List information from OpenAPI specification
generate Generates idiomatic, type-safe Rust code from an OpenAPI v3.1 (OAS31) specification
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
-V, --version Print version
Terminal Output:
--color <WHEN> Coloring [default: auto] [possible values: always, auto, never]
--theme <THEME> Theme [default: auto] [possible values: dark, light, auto]
Generate Command
Generates idiomatic, type-safe Rust code from an OpenAPI v3.1 (OAS31) specification
Usage: oas3-gen generate [OPTIONS] --input <FILE> --output <FILE> [MODE]
Arguments:
[MODE] Sets the generation mode [default: types] [possible values: types, client]
Required:
-i, --input <FILE> Path to the OpenAPI specification file
-o, --output <FILE> Path for the generated rust output file
Code Generation:
-C, --visibility <PUB> Module visibility for generated items [default: public] [possible values: public, crate, file]
Operation Filtering:
--only <id_1,id_2,...> Include only the specified comma-separated operation IDs
--exclude <id_1,id_2,...> Exclude the specified comma-separated operation IDs
--all-schemas Generate all schemas, even those unreferenced by selected operations
List Command
List information from OpenAPI specification
Usage: oas3-gen list <COMMAND>
Commands:
operations List all operations defined in the OpenAPI specification
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
Examples
# Basic usage to generate types
# Basic usage to generate companion client
# Generate types with crate-level visibility
# Generate all schemas types including unused ones
# Generate code for specific operation types only
# Generate code excluding certain operation types
# Generate all schemas but only specific operation types (includes unreferenced schemas)
# List all operations in the specification
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