| Crates.io | oas |
| lib.rs | oas |
| version | 0.2.1 |
| created_at | 2023-02-01 05:48:32.299229+00 |
| updated_at | 2025-08-20 15:54:24.4761+00 |
| description | OpenAPi Specification |
| homepage | |
| repository | |
| max_upload_size | |
| id | 773278 |
| size | 804,361 |
A comprehensive Rust implementation of the OpenAPI Specification 3.0 with full serialization/deserialization support and convenient builder patterns for programmatic API specification creation.
$ref referencesAdd this to your Cargo.toml:
[dependencies]
oas = "0.2"
serde_json = "1.0" # For JSON serialization
use oas::{builders, Referenceable, PathItem, Tag, Server};
fn main() {
let api = builders::api("Pet Store API", "1.0.0")
.with_description("A sample Pet Store API")
.add_server(Server::new("https://api.petstore.com"))
.add_tag(Tag::with_description("pets", "Pet operations"))
.add_path("/pets", PathItem::new()
.with_get(builders::get("List all pets")
.tag("pets")
.parameter(Referenceable::query_param("limit")
.with_schema(Referenceable::integer_schema()))
.build())
.with_post(builders::post("Create a pet")
.tag("pets")
.request_body(Referenceable::json_body(
Referenceable::schema_ref("Pet")))
.build()));
println!("{}", api.to_string());
}
The Referenceable<T> type is central to OpenAPI specifications, allowing you to use either inline data or references to reusable components:
use oas::{Referenceable, Schema};
// Inline schema
let inline = Referenceable::data(Schema::string());
// Reference to a component
let reference = Referenceable::schema_ref("User");
// Component reference with custom path
let custom_ref = Referenceable::component_ref("schemas", "CustomType");
Use the builder pattern for complex operations:
use oas::{builders, Referenceable};
let operation = builders::get("Get user by ID")
.tag("users")
.operation_id("getUserById")
.parameter(Referenceable::path_param("userId")
.with_schema(Referenceable::string_schema())
.with_description("The ID of the user to retrieve"))
.response("200", Referenceable::ok("User retrieved successfully"))
.response("404", Referenceable::error("User not found"))
.build();
Create schemas with type-specific constructors:
use oas::{Schema, Referenceable};
// Basic types
let string_schema = Schema::string();
let integer_schema = Schema::integer();
let boolean_schema = Schema::boolean();
// Or as Referenceable for use in parameters/responses
let ref_schema = Referenceable::string_schema();
use oas::{builders, Referenceable, PathItem, Tag, Server, Components, Schema};
use std::collections::BTreeMap;
let mut schemas = BTreeMap::new();
schemas.insert("User".to_string(), Referenceable::data(
Schema::object()
.with_description("A user object")
));
let api = builders::api("User Management API", "2.0.0")
.with_description("API for managing users")
.add_server(Server::new("https://api.example.com/v2")
.with_description("Production server"))
.add_server(Server::new("https://staging.api.example.com/v2")
.with_description("Staging server"))
.with_components(Components::new()
.with_schemas(schemas))
.add_tag(Tag::with_description("users", "User management operations"))
.add_path("/users", PathItem::new()
.with_get(builders::get("List users")
.tag("users")
.parameter(Referenceable::query_param("page")
.with_schema(Referenceable::integer_schema())
.with_description("Page number"))
.parameter(Referenceable::query_param("limit")
.with_schema(Referenceable::integer_schema())
.with_description("Items per page"))
.build())
.with_post(builders::post("Create user")
.tag("users")
.request_body(Referenceable::json_body(
Referenceable::schema_ref("User")))
.build()))
.add_path("/users/{id}", PathItem::new()
.with_get(builders::get("Get user")
.tag("users")
.parameter(Referenceable::path_param("id")
.with_schema(Referenceable::string_schema()))
.build())
.with_put(builders::put("Update user")
.tag("users")
.parameter(Referenceable::path_param("id")
.with_schema(Referenceable::string_schema()))
.request_body(Referenceable::json_body(
Referenceable::schema_ref("User")))
.build())
.with_delete(builders::delete("Delete user")
.tag("users")
.parameter(Referenceable::path_param("id")
.with_schema(Referenceable::string_schema()))
.build()));
println!("{}", serde_json::to_string_pretty(&api).unwrap());
use oas::OpenAPIV3;
let json_spec = r#"{
"openapi": "3.0.0",
"info": {
"title": "Sample API",
"version": "1.0.0"
},
"paths": {}
}"#;
let spec: OpenAPIV3 = serde_json::from_str(json_spec).unwrap();
println!("Loaded API: {}", spec.info.title);
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 responsesReferenceable::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 bodySchema::string(), Schema::integer(), Schema::boolean()Schema::array(), Schema::object()Referenceable::string_schema(), Referenceable::integer_schema(), etc.The crate includes comprehensive tests using real OpenAPI specification files:
cargo test
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.
This project is licensed under the MIT License - see the LICENSE file for details.