A powerful Rust library for OpenAPI 3.1 specification parsing, validation, and request handling.
Add this to your Cargo.toml:
[dependencies]
openapi-rs = { git = "https://github.com/baerwang/openapi-rs", features = ["axum"] }
axum = "0.7"
use openapi_rs::model::parse::OpenAPI;
use openapi_rs::request::axum::RequestData;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Parse OpenAPI specification from YAML file
// You can use the example file included in the project: examples/api.yaml
let content = std::fs::read_to_string("examples/api.yaml")?;
let openapi = OpenAPI::yaml(&content)?;
// Create request data for validation
let request_data = RequestData {
path: "/users".to_string(),
inner: axum::http::Request::builder()
.method("GET")
.uri("/users?page=1&limit=10")
.body(axum::body::Body::empty())
.unwrap(),
body: None,
};
// Validate the request against OpenAPI specification
openapi.validator(request_data)?;
// For POST requests with body
let body_data = r#"{"name": "John Doe", "email": "john.doe@example.com", "age": 30}"#;
let post_request = RequestData {
path: "/users".to_string(),
inner: axum::http::Request::builder()
.method("POST")
.uri("/users")
.header("content-type", "application/json")
.body(axum::body::Body::from(body_data))
.unwrap(),
body: Some(axum::body::Bytes::from(body_data)),
};
openapi.validator(post_request)?;
Ok(())
}
Example OpenAPI Specification File (examples/api.yaml):
This library includes a complete example OpenAPI specification file that demonstrates a User Management API definition, featuring:
email)uuid)date)time)date-time)ipv4)ipv6)base64)binary)minLength, maxLength)minimum, maximum)minItems, maxItems)required)enum)pattern)This library provides built-in observability features to help monitor and debug validation operations in production environments.
The observability system generates structured logs with the following information:
Successful Validation:
INFO openapi_validation method="GET" path="/example/{uuid}" success=true duration_ms=2 timestamp=1642752000000
Failed Validation:
WARN openapi_validation method="GET" path="/example/{uuid}" success=false duration_ms=1 error="Invalid UUID format" timestamp=1642752000001
You can run the included observability example to see the logging in action:
RUST_LOG=debug cargo run --example observability_test
For detailed implementation, see: observability_test.rs
Run tests:
cargo test
Contributions are welcome! Please follow these steps:
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
63 commits
4 commits
Rust
100.0%
A powerful Rust library for OpenAPI 3.1 specification parsing, validation, and request handling.
Add this to your Cargo.toml:
[dependencies]
openapi-rs = { git = "https://github.com/baerwang/openapi-rs", features = ["axum"] }
axum = "0.7"
use openapi_rs::model::parse::OpenAPI;
use openapi_rs::request::axum::RequestData;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Parse OpenAPI specification from YAML file
// You can use the example file included in the project: examples/api.yaml
let content = std::fs::read_to_string("examples/api.yaml")?;
let openapi = OpenAPI::yaml(&content)?;
// Create request data for validation
let request_data = RequestData {
path: "/users".to_string(),
inner: axum::http::Request::builder()
.method("GET")
.uri("/users?page=1&limit=10")
.body(axum::body::Body::empty())
.unwrap(),
body: None,
};
// Validate the request against OpenAPI specification
openapi.validator(request_data)?;
// For POST requests with body
let body_data = r#"{"name": "John Doe", "email": "john.doe@example.com", "age": 30}"#;
let post_request = RequestData {
path: "/users".to_string(),
inner: axum::http::Request::builder()
.method("POST")
.uri("/users")
.header("content-type", "application/json")
.body(axum::body::Body::from(body_data))
.unwrap(),
body: Some(axum::body::Bytes::from(body_data)),
};
openapi.validator(post_request)?;
Ok(())
}
Example OpenAPI Specification File (examples/api.yaml):
This library includes a complete example OpenAPI specification file that demonstrates a User Management API definition, featuring:
email)uuid)date)time)date-time)ipv4)ipv6)base64)binary)minLength, maxLength)minimum, maximum)minItems, maxItems)required)enum)pattern)This library provides built-in observability features to help monitor and debug validation operations in production environments.
The observability system generates structured logs with the following information:
Successful Validation:
INFO openapi_validation method="GET" path="/example/{uuid}" success=true duration_ms=2 timestamp=1642752000000
Failed Validation:
WARN openapi_validation method="GET" path="/example/{uuid}" success=false duration_ms=1 error="Invalid UUID format" timestamp=1642752000001
You can run the included observability example to see the logging in action:
RUST_LOG=debug cargo run --example observability_test
For detailed implementation, see: observability_test.rs
Run tests:
cargo test
Contributions are welcome! Please follow these steps:
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
63 commits
4 commits
Rust
100.0%