OpenAPI & Swagger
Ferrite generates an OpenAPI 3.1 specification from your route metadata — no annotation sprawl required. Every route, request body, and guard is already recorded by the macros; ferrite-swagger turns that into a browsable API.
Enable it
use ferrite_framework::{module, Ferrite};
#[module(imports = [SwaggerModule])]
pub struct AppModule;
#[bootstrap]
async fn main() {
let app = Ferrite::create::<AppModule>().await;
app.listen("0.0.0.0:3000").await?;
}What you get
| Route | Content |
|---|---|
GET /openapi.json | The full OpenAPI 3.1 document |
GET /docs & GET /docs/ | Swagger UI over the CDN |
What the generator includes
build_openapi(&SwaggerConfig) enumerates the registered ControllerRoutes and:
- adds a bearer HTTP security scheme (named
bearer) - per-route
200/400/404responses - a
requestBodywhenever a route recorded body DTO types (request_body_types) - a
401response +securityblock whenever a route recorded guards (guard_types)
That last bit is why #[use_guards(AuthGuard)] on a route makes the spec grow a security requirement automatically.
Configuration
use ferrite_swagger::SwaggerConfig;
let config = SwaggerConfig {
title: "Users API".into(),
version: "1.2.3".into(),
description: "Our public API".into(),
server_url: Some("https://api.example.com".into()),
};Or set it from the environment:
| Env | Default |
|---|---|
SWAGGER_TITLE | Ferrite API |
SWAGGER_VERSION | 0.1.0 |
SWAGGER_DESCRIPTION | Generated by Ferrite |
SWAGGER_SERVER_URL | unset — servers omitted |
SwaggerService exposes config(), document(), and openapi() if you want to serve the spec elsewhere.
Extending with SwaggerFragment
Register DTO schemas, tags, and per-operation metadata at compile time with the fragment API:
use ferrite_swagger::submit_fragment;
submit_fragment! { |api: &mut utoipa::openapi::OpenApi| {
// Register DTO schemas from a ToSchema impl:
api.components_mut().and_then(|c| c.schemas).map(|schemas| {
schemas.insert("UserResponse".into(), ToSchema::schemas()["UserResponse"].clone());
});
// tag operations by path prefix:
let tag = utoipa::openapi::tag::TagBuilder::new().name("users").build();
api.tags.get_or_insert_with(Vec::new).push(tag);
}}Fragment closures get &mut utoipa::openapi::OpenApi, so anything utoipa supports is on the table.
Service access
Inject SwaggerService and read the document programmatically:
#[injectable]
pub struct ApiReporter {
swagger: SwaggerService,
}
impl ApiReporter {
#[inject]
pub fn new(swagger: SwaggerService) -> Self {
Self { swagger }
}
pub fn summary(&self) -> serde_json::Value {
self.swagger.document()
}
}CLI note
fr openapi export currently writes a static (empty) spec file. For a real spec, hit GET /openapi.json at runtime, or build it in a test with build_openapi.
Next steps
- Health checks.
- Controllers — route metadata source.