pub struct Route {Show 13 fields
pub path: String,
pub method: Method,
pub operation_id: String,
pub summary: Option<String>,
pub description: Option<String>,
pub tags: Vec<String>,
pub deprecated: bool,
pub path_params: Vec<ParamInfo>,
pub request_body_schema: Option<String>,
pub request_body_content_type: Option<String>,
pub request_body_required: bool,
pub security: Vec<RouteSecurityRequirement>,
pub responses: Vec<RouteResponse>,
/* private fields */
}Expand description
A route definition with handler for request processing.
Routes are created with a path pattern, HTTP method, and a handler function.
The handler is stored as a type-erased Arc<dyn Handler> for dynamic dispatch.
§Example
use fastapi_router::Route;
use fastapi_core::{Method, Handler, RequestContext, Request, Response};
let route = Route::new(Method::Get, "/users/{id}", my_handler);Fields§
§path: StringRoute path pattern (e.g., “/users/{id}”).
method: MethodHTTP method for this route.
operation_id: StringOperation ID for OpenAPI documentation.
summary: Option<String>OpenAPI summary (short description).
description: Option<String>OpenAPI description (detailed explanation).
Tags for grouping routes in OpenAPI documentation.
deprecated: boolWhether this route is deprecated.
path_params: Vec<ParamInfo>Path parameters extracted from the route pattern for OpenAPI documentation.
request_body_schema: Option<String>Request body schema type name for OpenAPI documentation (e.g., “CreateUser”).
request_body_content_type: Option<String>Request body content type for OpenAPI documentation (e.g., “application/json”).
request_body_required: boolWhether the request body is required.
security: Vec<RouteSecurityRequirement>Security requirements for this route.
Each requirement specifies a security scheme name and optional scopes. Multiple requirements means any one of them can be used (OR logic).
responses: Vec<RouteResponse>Declared responses for OpenAPI documentation.
Each response specifies a status code, schema type, and description.
Implementations§
Source§impl Route
impl Route
Sourcepub fn new<H>(method: Method, path: impl Into<String>, handler: H) -> Selfwhere
H: Handler + 'static,
pub fn new<H>(method: Method, path: impl Into<String>, handler: H) -> Selfwhere
H: Handler + 'static,
Create a new route with a handler.
§Arguments
method- HTTP method for this routepath- Path pattern (e.g., “/users/{id}”)handler- Handler that processes matching requests
§Example
use fastapi_router::Route;
use fastapi_core::Method;
let route = Route::new(Method::Get, "/users/{id}", my_handler);Sourcepub fn with_arc_handler(
method: Method,
path: impl Into<String>,
handler: Arc<dyn Handler>,
) -> Self
pub fn with_arc_handler( method: Method, path: impl Into<String>, handler: Arc<dyn Handler>, ) -> Self
Create a new route with a pre-wrapped Arc handler.
Useful when the handler is already wrapped in an Arc for sharing.
Sourcepub fn with_placeholder_handler(method: Method, path: impl Into<String>) -> Self
pub fn with_placeholder_handler(method: Method, path: impl Into<String>) -> Self
Create a route with a placeholder handler.
This is used by the route registration macros during compile-time route discovery. The placeholder handler returns 501 Not Implemented.
Note: Routes created with this method should have their handlers replaced before being used to handle actual requests.
Sourcepub fn summary(self, summary: impl Into<String>) -> Self
pub fn summary(self, summary: impl Into<String>) -> Self
Set the summary for OpenAPI documentation.
Sourcepub fn description(self, description: impl Into<String>) -> Self
pub fn description(self, description: impl Into<String>) -> Self
Set the description for OpenAPI documentation.
Sourcepub fn operation_id(self, operation_id: impl Into<String>) -> Self
pub fn operation_id(self, operation_id: impl Into<String>) -> Self
Set the operation ID for OpenAPI documentation.
Sourcepub fn tag(self, tag: impl Into<String>) -> Self
pub fn tag(self, tag: impl Into<String>) -> Self
Add a tag for grouping in OpenAPI documentation.
Set multiple tags for grouping in OpenAPI documentation.
Sourcepub fn deprecated(self) -> Self
pub fn deprecated(self) -> Self
Mark this route as deprecated in OpenAPI documentation.
Sourcepub fn request_body(
self,
schema: impl Into<String>,
content_type: impl Into<String>,
required: bool,
) -> Self
pub fn request_body( self, schema: impl Into<String>, content_type: impl Into<String>, required: bool, ) -> Self
Set the request body schema for OpenAPI documentation.
The schema name will be used to generate a $ref to the schema
in the components section.
Sourcepub fn security(
self,
scheme: impl Into<String>,
scopes: impl IntoIterator<Item = impl Into<String>>,
) -> Self
pub fn security( self, scheme: impl Into<String>, scopes: impl IntoIterator<Item = impl Into<String>>, ) -> Self
Add a security requirement for this route.
Each call adds an alternative security requirement (OR logic). The scheme name must match a security scheme defined in the OpenAPI components section.
§Example
use fastapi_router::Route;
use fastapi_core::Method;
// Route requires bearer token authentication
let route = Route::with_placeholder_handler(Method::Get, "/protected")
.security("bearer", vec![]);
// Route requires OAuth2 with specific scopes
let route = Route::with_placeholder_handler(Method::Post, "/users")
.security("oauth2", vec!["write:users"]);Sourcepub fn security_scheme(self, scheme: impl Into<String>) -> Self
pub fn security_scheme(self, scheme: impl Into<String>) -> Self
Sourcepub fn response(
self,
status: u16,
schema_name: impl Into<String>,
description: impl Into<String>,
) -> Self
pub fn response( self, status: u16, schema_name: impl Into<String>, description: impl Into<String>, ) -> Self
Add a response declaration for OpenAPI documentation.
The response type is verified at compile time to ensure it implements
JsonSchema, enabling OpenAPI schema generation.
§Arguments
status- HTTP status code (e.g., 200, 201, 404)schema_name- Type name for the response body (e.g., “User”)description- Description of when this response is returned
§Example
use fastapi_router::Route;
use fastapi_core::Method;
let route = Route::with_placeholder_handler(Method::Get, "/users/{id}")
.response(200, "User", "User found")
.response(404, "ErrorResponse", "User not found");Sourcepub fn has_responses(&self) -> bool
pub fn has_responses(&self) -> bool
Check if this route has response declarations.
Sourcepub fn has_request_body(&self) -> bool
pub fn has_request_body(&self) -> bool
Check if this route has a request body defined.
Sourcepub fn has_path_params(&self) -> bool
pub fn has_path_params(&self) -> bool
Check if this route has path parameters.
Sourcepub fn has_security(&self) -> bool
pub fn has_security(&self) -> bool
Check if this route has security requirements.