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>,
}
Expand description

A route definition used for routing and (optionally) metadata.

Routes are created with a path pattern and HTTP method. The router crate is intentionally pure routing/metadata and does not store framework handler objects (to avoid dependency cycles with fastapi-core).

§Example

use fastapi_router::Route;
use fastapi_types::Method;

let route = Route::new(Method::Get, "/users/{id}");

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(method: Method, path: impl Into<String>) -> Self

Create a new route.

§Arguments
  • method - HTTP method for this route
  • path - Path pattern (e.g., “/users/{id}”)
§Example
use fastapi_router::Route;
use fastapi_types::Method;

let route = Route::new(Method::Get, "/users/{id}");
Source

pub fn with_placeholder_handler(method: Method, path: impl Into<String>) -> Self

Create a route intended for generated metadata (OpenAPI/docs).

This is an alias for Route::new. The name is kept for compatibility with older macro expansions and tests; a Route never contains an actual handler function.

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_types::Method;

// Route requires bearer token authentication
let route = Route::new(Method::Get, "/protected")
    .security("bearer", vec![]);

// Route requires OAuth2 with specific scopes
let route = Route::new(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_types::Method;

let route = Route::new(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_types::Method;

let route = Route::new(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 Clone for Route

Source§

fn clone(&self) -> Route

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.