Clawspec
A Rust library for generating OpenAPI specifications from your HTTP client test code. Write tests, get documentation.
Overview
Clawspec automatically generates OpenAPI documentation by observing HTTP client interactions in your tests. Instead of maintaining separate API documentation, your tests become the source of truth.
Key Features
- ๐งช Test-Driven Documentation - Generate specs from integration tests
- ๐ Type Safety - Leverage Rust's type system for accurate schemas
- ๐ Zero Runtime Overhead - Documentation generation only runs during tests
- ๐ ๏ธ Framework Agnostic - Works with any async HTTP server
- ๐ OpenAPI 3.1 Compliant - Generate standard-compliant specifications
Quick Start
Add to your Cargo.toml:
[]
= "0.1.0"
[]
= { = "1", = ["full"] }
Basic Example with ApiClient
use ApiClient;
use ;
use ToSchema;
async
Test Server Example with TestClient
For testing complete web applications. See the axum example for a full working implementation:
use ;
use TcpListener;
;
async
Core Concepts
ApiClient
The main HTTP client that captures request/response schemas:
- Builder pattern for configuration
- Automatic schema extraction from Rust types
- Flexible parameter handling (path, query, headers)
- Status code validation with ranges and specific codes
TestClient
A test-focused wrapper providing:
- Automatic server lifecycle management
- Health checking with retries
- Integrated OpenAPI generation
- Framework-agnostic design
Advanced Usage
Parameter Handling
use ;
let mut path = from;
path.add_param;
path.add_param;
let query = new
.add_param
.add_param;
let headers = new
.add_header
.add_header;
let response = client
.get?
.with_query
.with_headers
.exchange
.await?;
Status Code Validation
By default, requests expect status codes in the range 200-499. You can customize this:
use ;
// Accept specific codes
client.post?
.with_expected_status_codes
.exchange
.await?;
// Accept ranges
client.get?
.with_expected_status_codes
.exchange
.await?;
// Complex patterns
client.delete?
.with_expected_status_codes
.exchange
.await?;
Schema Registration
use ;
// Register schemas for better documentation
register_schemas!;
Integration Examples
With Axum
For a complete working example, see the axum example implementation.
use ;
use ;
Configuration
TestServerConfig
Configure test server behavior:
use TestServerConfig;
use Duration;
let config = TestServerConfig ;
Error Handling
The library provides comprehensive error types:
ApiClientError- HTTP client errorsTestAppError- Test server errors
All errors implement standard error traits and provide detailed context for debugging.
Best Practices
- Write focused tests - Each test should document specific endpoints
- Use descriptive types - Well-named structs generate better documentation
- Register schemas - Explicitly register types for complete documentation
- Validate status codes - Be explicit about expected responses
- Organize tests - Group related endpoint tests together
Contributing
We welcome contributions! Please see our Contributing Guide for details.
Note: This project has been developed with assistance from Claude Code. All AI-generated code has been carefully reviewed, tested, and validated to ensure quality, security, and adherence to Rust best practices.
License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Acknowledgments
Built with excellent crates from the Rust ecosystem: