OpenAPI Specification (OAS) 3.0 for Rust
A comprehensive Rust implementation of the OpenAPI Specification 3.0 with full serialization/deserialization support and convenient builder patterns for programmatic API specification creation.
Features
- ✅ Complete OAS 3.0 Support - All OpenAPI 3.0 specification features
- 🔄 Serde Integration - Full JSON/YAML serialization and deserialization
- 🛠️ Builder Patterns - Fluent APIs for easy specification construction
- 🔒 Type Safety - Leverages Rust's type system to prevent invalid specifications
- 📚 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.2"
= "1.0" # For JSON serialization
Basic Example
use ;
Core Concepts
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
The crate includes comprehensive tests using real OpenAPI specification files:
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.