Attribute Macro api_route

Source
#[api_route]
Expand description

Same as route, but with support for OpenApi using aide. See route for more information and examples.

§Syntax

#[api_route(<METHOD> "<PATH>" [with <STATE>] [{
    summary: "<SUMMARY>",
    description: "<DESCRIPTION>",
    id: "<ID>",
    tags: ["<TAG>", ..],
    hidden: <bool>,
    security: { <SCHEME>: ["<SCOPE>", ..], .. },
    responses: { <CODE>: <TYPE>, .. },
    transform: |op| { .. },
}])]
  • summary is the OpenApi summary. If not specified, the first line of the function’s doc-comments
  • description is the OpenApi description. If not specified, the rest of the function’s doc-comments
  • id is the OpenApi operationId. If not specified, the function’s name is used.
  • tags are the OpenApi tags.
  • hidden sets whether docs should be hidden for this route.
  • security is the OpenApi security requirements.
  • responses are the OpenApi responses.
  • transform is a closure that takes an TransformOperation and returns an TransformOperation. This may override the other options. (see the crate aide for more information).

§Example

use axum::extract::{State, Json};
use axum_typed_routing_macros::api_route;

#[api_route(GET "/item/:id?amount&offset" with String {
    summary: "Get an item",
    description: "Get an item by id",
    id: "get-item",
    tags: ["items"],
    hidden: false,
    security: { "bearer": ["read:items"] },
    responses: { 200: String },
    transform: |op| op.tag("private"),
})]
async fn item_handler(
    id: u32,
    amount: Option<u32>,
    offset: Option<u32>,
    State(state): State<String>,
) -> String {
    todo!("handle request")
}