pub struct OpenApiBuilder { /* private fields */ }Expand description
OpenAPI document builder.
Implementations§
Source§impl OpenApiBuilder
impl OpenApiBuilder
Sourcepub fn with_registry(
title: impl Into<String>,
version: impl Into<String>,
registry: SchemaRegistry,
) -> Self
pub fn with_registry( title: impl Into<String>, version: impl Into<String>, registry: SchemaRegistry, ) -> Self
Create a new builder with an existing schema registry.
Use this when you want to share schemas across multiple OpenAPI documents or when you’ve pre-registered schemas.
Sourcepub fn registry(&self) -> &SchemaRegistry
pub fn registry(&self) -> &SchemaRegistry
Get a reference to the schema registry.
Use this to register schemas that should be in #/components/schemas/
and get $ref references to them.
Sourcepub fn description(self, description: impl Into<String>) -> Self
pub fn description(self, description: impl Into<String>) -> Self
Add a description.
Sourcepub fn security_scheme(
self,
name: impl Into<String>,
scheme: SecurityScheme,
) -> Self
pub fn security_scheme( self, name: impl Into<String>, scheme: SecurityScheme, ) -> Self
Add a security scheme.
Security schemes define authentication methods used by the API.
§Example
use fastapi_openapi::{OpenApiBuilder, SecurityScheme, ApiKeyLocation};
let doc = OpenApiBuilder::new("My API", "1.0.0")
.security_scheme("api_key", SecurityScheme::ApiKey {
name: "X-API-Key".to_string(),
location: ApiKeyLocation::Header,
description: Some("API key for authentication".to_string()),
})
.security_scheme("bearer", SecurityScheme::Http {
scheme: "bearer".to_string(),
bearer_format: Some("JWT".to_string()),
description: None,
})
.build();Sourcepub fn security_requirement(
self,
scheme: impl Into<String>,
scopes: Vec<String>,
) -> Self
pub fn security_requirement( self, scheme: impl Into<String>, scopes: Vec<String>, ) -> Self
Add a global security requirement.
Global security requirements apply to all operations unless overridden at the operation level.
§Example
use fastapi_openapi::OpenApiBuilder;
let doc = OpenApiBuilder::new("My API", "1.0.0")
.security_scheme("api_key", /* ... */)
.security_requirement("api_key", vec![]) // No scopes needed for API key
.build();Sourcepub fn add_route(&mut self, route: &Route)
pub fn add_route(&mut self, route: &Route)
Add a route to the OpenAPI document.
Converts a route’s metadata into an OpenAPI Operation and adds it to the appropriate path. Multiple routes on the same path with different methods are merged into a single PathItem.
§Example
use fastapi_openapi::OpenApiBuilder;
use fastapi_router::Router;
let router = Router::new();
// ... add routes to router ...
let mut builder = OpenApiBuilder::new("My API", "1.0.0");
for route in router.routes() {
builder.add_route(route);
}
let doc = builder.build();Sourcepub fn add_routes(&mut self, routes: &[Route])
pub fn add_routes(&mut self, routes: &[Route])
Add multiple routes to the OpenAPI document.
Convenience method that calls add_route for each route.