Skip to main content

routes

Macro routes 

Source
macro_rules! routes {
    ( $handler:path $(, $tail:path)* $(,)? ) => { ... };
    ( $schemas:tt: $router:ident: $paths:ident: $handler:path $(, $tail:tt)* ) => { ... };
    ( @resolve_types $handler:path : $schemas:tt ) => { ... };
    ( @path $op:tt of $part:ident $( :: $tt:tt )* ) => { ... };
    ( $op:tt : [ $first:tt $( $rest:tt )* ] $( $rev:tt )* ) => { ... };
    ( $op:tt : [] $first:tt $( $rest:tt )* ) => { ... };
    ( @inverse $op:tt : $tt:tt $( $rest:tt )* ) => { ... };
    ( @rev $op:tt : $tt:tt [ $first:tt $( $rest:tt)* ] $( $reversed:tt )* ) => { ... };
    ( @rev [$op:ident $( $args:tt )* ] : $handler:tt [] $($tt:tt)* ) => { ... };
    ( ) => { ... };
}
Expand description

Collect axum handlers annotated with utoipa::path to router::UtoipaMethodRouter.

routes macro will return router::UtoipaMethodRouter which contains an axum::routing::MethodRouter and currently registered paths. The output of this macro is meant to be used together with router::OpenApiRouter which combines the paths and axum routers to a single entity.

Only handlers collected with routes macro will get registered to the OpenApi.

§Panics

Routes registered via routes macro or via axum::routing::* operations are bound to same rules where only one one HTTP method can can be registered once per call. This means that the following will produce runtime panic from axum code.

 #[utoipa::path(get, path = "/search")]
 async fn search_user() {}

 #[utoipa::path(get, path = "")]
 async fn get_user() {}

 let _: UtoipaMethodRouter = routes!(get_user, search_user);

Since the axum does not support method filter for CONNECT requests, using this macro with handler having request method type CONNECT #[utoipa::path(connect, path = "")] will panic at runtime.

§Examples

Create new OpenApiRouter with get_user and post_user paths.

 #[utoipa::path(get, path = "")]
 async fn get_user() {}

 #[utoipa::path(post, path = "")]
 async fn post_user() {}

 let _: OpenApiRouter = OpenApiRouter::new().routes(routes!(get_user, post_user));