Stonehm - Automatic OpenAPI 3.0 Generation for Axum
Stonehm is a Rust crate that automatically generates OpenAPI 3.0 specifications for Axum web applications by analyzing handler functions and their documentation.
Features
- Automatic OpenAPI generation: Extract documentation from function comments
- Schema generation: Automatically generate JSON schemas for request/response types
- Zero runtime overhead: All processing happens at compile time
- Axum integration: Drop-in replacement for
axum::Router - Documentation-driven: Uses standard Rust doc comments
Quick Start
Add stonehm to your Cargo.toml:
[]
= "0.1"
= "0.7"
= { = "1.0", = ["derive"] }
= { = "1", = ["macros", "rt-multi-thread"] }
Basic Usage
use Json;
use ;
use ;
/// Returns a simple hello world message
///
/// This endpoint doesn't require any parameters and always returns
/// the same friendly greeting message.
///
/// # Responses
/// - 200: Successfully returned hello message
async
/// Creates a personalized greeting
///
/// Takes a name in the request body and returns a personalized
/// greeting message.
///
/// # Request Body
/// Content-Type: application/json
/// The request body should contain a JSON object with a name field.
///
/// # Responses
/// - 200: Successfully created personalized greeting
/// - 400: Invalid request body
async
// Note: This example shows the API structure but doesn't compile
// due to missing tokio dependency in doctests
Documentation Format
Stonehm extracts documentation from standard Rust doc comments using these sections:
Parameters
Document path, query, and header parameters:
use ;
use Json;
use Serialize;
/// Get user by ID
///
/// # Parameters
/// - id (path): The user's unique identifier
/// - include_posts (query): Whether to include user's posts
/// - authorization (header): Bearer token for authentication
async
Request Body
Document the expected request body:
use ;
use Json;
use ;
/// Create a new user
///
/// # Request Body
/// Content-Type: application/json
/// User information including name, email, and optional preferences.
async
Responses
Document possible response status codes:
use ;
use Json;
use Serialize;
/// Update user information
///
/// # Responses
/// - 200: User successfully updated
/// - 400: Invalid user data provided
/// - 404: User not found
/// - 500: Internal server error
async
Schema Generation
Use the StoneSchema derive macro on your request/response types:
use Serialize;
use StoneSchema;
This automatically generates JSON Schema definitions that are included in the OpenAPI specification.