OpenAPI Specification (OAS) for Rust
Rust OpenAPI models with Serde serialization and convenient builders. The 0.3 API adds JSON Schema 2020-12 representations and OpenAPI 3.2 streaming media types while keeping existing constructors on OpenAPI 3.0.0 by default.
OpenAPI 3.1/3.2 coverage is partial. See MIGRATION.md for source compatibility changes, schema migration, and the remaining model differences.
Features
- OpenAPI Models - Operations, parameters, responses, security, callbacks, and links
- Modern Schemas - Type unions, boolean schemas, reference siblings, and string-encoded content
- Streaming Media Types - OpenAPI 3.2
itemSchemafor parsed stream items, including SSE - 🔄 Serde Integration - Full JSON/YAML serialization and deserialization
- 🛠️ Builder Patterns - Fluent APIs for easy specification construction
- Typed Construction - Rust models and builders; document/schema validation is left to callers
- 📚 Reference System - Support for both inline definitions and
$refreferences - 🚀 Convenience Methods - Extensive helper functions to reduce boilerplate
Quick Start
Add this to your Cargo.toml:
[]
= "0.3"
= "1.0" # For JSON serialization
Basic Example
use ;
Core Concepts
OpenAPI 3.2 and SSE
Choose OpenAPIV3::new_v3_2(info) explicitly and use itemSchema for individual
parsed events. JSON in an SSE data field remains a string on the wire:
use ;
let api = new_v3_2;
let events = new
.with_item_schema;
let data = string
.with_content_media_type
.with_content_schema;
let nullable_string = string.with_types;
See examples/sse.rs for a complete document (cargo run --example sse).
SchemaValue represents either a Schema object or a boolean schema; schema
positions accept both. Unknown JSON Schema keywords are retained in Schema.extras.
Referenceable Types
The Referenceable<T> type is central to OpenAPI specifications, allowing you to use either inline data or references to reusable components:
use ;
// Inline schema
let inline = data;
// Reference to a component
let reference = schema_ref;
// Component reference with custom path
let custom_ref = component_ref;
Builder Pattern
Use the builder pattern for complex operations:
use ;
let operation = get
.tag
.operation_id
.parameter
.response
.response
.build;
Schema Creation
Create schemas with type-specific constructors:
use ;
// Basic types
let string_schema = string;
let integer_schema = integer;
let boolean_schema = boolean;
// Or as Referenceable for use in parameters/responses
let ref_schema = string_schema;
Examples
Complete API Specification
use ;
use BTreeMap;
let mut schemas = new;
schemas.insert;
let api = api
.with_description
.add_server
.add_server
.with_components
.add_tag
.add_path
.add_path;
println!;
Loading from JSON
use OpenAPIV3;
let json_spec = r#"{
"openapi": "3.0.0",
"info": {
"title": "Sample API",
"version": "1.0.0"
},
"paths": {}
}"#;
let spec: OpenAPIV3 = from_str.unwrap;
println!;
API Reference
Quick Builders
builders::api(title, version)- Create a new API specificationbuilders::get(summary)- GET operation with 200 responsebuilders::post(summary)- POST operation with 201/400 responsesbuilders::put(summary)- PUT operation with 200/404 responsesbuilders::delete(summary)- DELETE operation with 204/404 responses
Referenceable Helpers
Referenceable::data(item)- Wrap inline dataReferenceable::reference(ref_str)- Create referenceReferenceable::schema_ref(name)- Schema component referenceReferenceable::query_param(name)- Query parameterReferenceable::path_param(name)- Path parameter (automatically required)Referenceable::json_body(schema)- JSON request body
Schema Shortcuts
Schema::string(),Schema::integer(),Schema::boolean()Schema::array(),Schema::object()Referenceable::string_schema(),Referenceable::integer_schema(), etc.
Testing
Tests cover existing 3.0 documents, 3.2 SSE round trips, modern schema forms, and constructor compatibility. Initialize the fixture submodule on a fresh checkout:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License - see the LICENSE file for details.