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 Pet OpenAPI schema definition in fixtures/petstore.json:
Executing oas3-gen produces the corresponding Rust types.
// src/types.rs
use Deserialize;
Key Features
| Feature | Description |
|---|---|
| Cycle Detection | Prevents infinite type recursion |
| Doc Comments | Schema descriptions become rustdoc |
| Enum Helpers | Ergonomic is_/as_ methods |
| Enum Modes | Merge, preserve, or relaxed |
| OData Support | Optional @odata.* field handling |
| OpenAPI 3.1 | Full spec parsing support |
| Operation Filtering | Include/exclude specific operations |
| Operation Types | Request/response type generation |
| Schema Composition | Handles allOf/oneOf/anyOf correctly |
| Serde Integration | Automatic derive for serialization |
| Smart Naming | Auto-detects camelCase/snake_case conventions |
| Validation | Constraint attributes from spec |
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]
--odata-support Enable OData-specific field optionality rules (makes @odata.* fields optional)
--enum-mode <MODE> How to handle enum case sensitivity and duplicates [default: merge] [possible values: merge, preserve, relaxed]
--no-helpers Disable generation of ergonomic helper methods for enum variants
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)
# Enable OData support for Microsoft Graph
# Enable relaxed (case-insensitive) enum deserialization
# 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