Skip to main content

Crate openapi_to_rust

Crate openapi_to_rust 

Source
Expand description

Generate typed Rust models, async HTTP/SSE clients, and opt-in Axum server scaffolding from OpenAPI 3.0 and 3.1 documents (with experimental 3.2 parsing).

Most users should install the openapi-to-rust CLI and start with openapi-to-rust generate <SOURCE>. The library API is useful when a build tool or application needs to analyze and render a document in memory.

§Library example

use openapi_to_rust::{CodeGenerator, GeneratorConfig, SchemaAnalyzer};
use serde_json::json;

let spec = json!({
    "openapi": "3.1.0",
    "info": { "title": "Example", "version": "1.0.0" },
    "paths": {},
    "components": {
        "schemas": {
            "Greeting": {
                "type": "object",
                "required": ["message"],
                "properties": { "message": { "type": "string" } }
            }
        }
    }
});

let mut analyzer = SchemaAnalyzer::new(spec)?;
let mut analysis = analyzer.analyze()?;
let generator = CodeGenerator::new(GeneratorConfig {
    enable_async_client: false,
    ..GeneratorConfig::default()
});
let source = generator.generate(&mut analysis)?;
assert!(source.contains("pub struct Greeting"));

Configuration is documented in config. Generated dependency fragments are represented by DepRequirement, and generated files can be written with CodeGenerator::write_files.

Re-exports§

pub use analysis::SchemaAnalysis;
pub use analysis::SchemaAnalyzer;
pub use analysis::merge_schema_extensions;
pub use config::ConfigFile;
pub use error::GeneratorError;
pub use generator::CodeGenerator;
pub use generator::GeneratedFile;
pub use generator::GenerationResult;
pub use generator::GeneratorConfig;
pub use http_config::AuthConfig;
pub use http_config::HttpClientConfig;
pub use http_config::RetryConfig;
pub use http_error::ApiError;
pub use http_error::ApiOpError;
pub use http_error::HttpError;
pub use http_error::HttpResult;
pub use openapi::OpenApiSpec;
pub use openapi::Schema;
pub use openapi::SchemaType;
pub use type_mapping::ByteStrategy;
pub use type_mapping::DepRequirement;
pub use type_mapping::MappedType;
pub use type_mapping::TypeFeature;
pub use type_mapping::TypeMapper;
pub use type_mapping::TypeMappingConfig;
pub use type_mapping::UsedFeatures;

Modules§

analysis
cli
client_generator
HTTP client generation for OpenAPI specifications.
config
TOML configuration file support for OpenAPI code generation.
error
extensions
Specification Extensions support — x-* fields per OAS §“Specification Extensions”.
generator
http_config
Runtime HTTP client configuration types
http_error
HTTP client error types with comprehensive retry detection.
openapi
patterns
registry_generator
Operation registry generation for OpenAPI specifications.
server
Server codegen — opt-in Axum scaffolding for user-selected operations.
spec_source
Spec-source policy and document parsing shared by the CLI, library consumers, and the WASM playground build.
streaming
type_mapping
Centralized OpenAPI type → Rust type mapping.

Constants§

VERSION
Crate version, exposed so embedders (e.g. the WASM playground) can report which generator produced their output.

Type Aliases§

Result