Skip to main content

Route

Struct Route 

Source
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: String

Route path pattern (e.g., “/users/{id}”).

§method: Method

HTTP method for this route.

§operation_id: String

Operation ID for OpenAPI documentation.

§summary: Option<String>

OpenAPI summary (short description).

§description: Option<String>

OpenAPI description (detailed explanation).

§tags: Vec<String>

Tags for grouping routes in OpenAPI documentation.

§deprecated: bool

Whether 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: bool

Whether 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

Source

pub fn new<H>(method: Method, path: impl Into<String>, handler: H) -> Self
where H: Handler + 'static,

Create a new route with a handler.

§Arguments
  • method - HTTP method for this route
  • path - 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);
Source

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.

Source

pub fn handler(&self) -> &Arc<dyn Handler>

Get a reference to the handler.

Source

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.

Source

pub fn summary(self, summary: impl Into<String>) -> Self

Set the summary for OpenAPI documentation.

Source

pub fn description(self, description: impl Into<String>) -> Self

Set the description for OpenAPI documentation.

Source

pub fn operation_id(self, operation_id: impl Into<String>) -> Self

Set the operation ID for OpenAPI documentation.

Source

pub fn tag(self, tag: impl Into<String>) -> Self

Add a tag for grouping in OpenAPI documentation.

Source

pub fn tags(self, tags: impl IntoIterator<Item = impl Into<String>>) -> Self

Set multiple tags for grouping in OpenAPI documentation.

Source

pub fn deprecated(self) -> Self

Mark this route as deprecated in OpenAPI documentation.

Source

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.

Source

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"]);
Source

pub fn security_scheme(self, scheme: impl Into<String>) -> Self

Add a security requirement without scopes.

Convenience method for schemes that don’t use scopes (e.g., API key, bearer token).

§Example
use fastapi_router::Route;
use fastapi_core::Method;

let route = Route::with_placeholder_handler(Method::Get, "/protected")
    .security_scheme("api_key");
Source

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");
Source

pub fn has_responses(&self) -> bool

Check if this route has response declarations.

Source

pub fn has_request_body(&self) -> bool

Check if this route has a request body defined.

Source

pub fn has_path_params(&self) -> bool

Check if this route has path parameters.

Source

pub fn has_security(&self) -> bool

Check if this route has security requirements.

Trait Implementations§

Source§

impl Debug for Route

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Route

§

impl !RefUnwindSafe for Route

§

impl Send for Route

§

impl Sync for Route

§

impl Unpin for Route

§

impl !UnwindSafe for Route

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ResponseProduces<T> for T