|
| 1 | +//! Typed routing for Axum. |
| 2 | +
|
| 3 | +use axum::extract::rejection::PathRejection; |
| 4 | +use axum_extra::routing::SecondElementIs; |
| 5 | +use http::request::Parts; |
| 6 | +use schemars::JsonSchema; |
| 7 | +use serde::de::DeserializeOwned; |
| 8 | + |
| 9 | +use super::*; |
| 10 | +use crate::operation::{add_parameters, parameters_from_schema, OperationHandler, ParamLocation}; |
| 11 | + |
| 12 | +impl<S> crate::axum::ApiRouter<S> |
| 13 | +where |
| 14 | + S: Clone + Send + Sync + 'static, |
| 15 | +{ |
| 16 | + /// Add a typed `GET` route to the router. |
| 17 | + /// |
| 18 | + /// The path will be inferred from the first argument to the handler function which must |
| 19 | + /// implement [`TypedPath`]. |
| 20 | + pub fn typed_get<H, I, O, T, P>(self, handler: H) -> Self |
| 21 | + where |
| 22 | + H: axum::handler::Handler<T, S> + OperationHandler<I, O>, |
| 23 | + T: SecondElementIs<P> + 'static, |
| 24 | + I: OperationInput, |
| 25 | + O: OperationOutput, |
| 26 | + P: axum_extra::routing::TypedPath + schemars::JsonSchema + OperationInput, |
| 27 | + { |
| 28 | + self.api_route(P::PATH, crate::axum::routing::get(handler)) |
| 29 | + } |
| 30 | + |
| 31 | + /// Add a typed `DELETE` route to the router. |
| 32 | + /// |
| 33 | + /// The path will be inferred from the first argument to the handler function which must |
| 34 | + /// implement [`TypedPath`]. |
| 35 | + pub fn typed_delete<H, I, O, T, P>(self, handler: H) -> Self |
| 36 | + where |
| 37 | + H: axum::handler::Handler<T, S> + OperationHandler<I, O>, |
| 38 | + T: SecondElementIs<P> + 'static, |
| 39 | + I: OperationInput, |
| 40 | + O: OperationOutput, |
| 41 | + P: axum_extra::routing::TypedPath + schemars::JsonSchema + OperationInput, |
| 42 | + { |
| 43 | + self.api_route(P::PATH, crate::axum::routing::delete(handler)) |
| 44 | + } |
| 45 | + |
| 46 | + /// Add a typed `HEAD` route to the router. |
| 47 | + /// |
| 48 | + /// The path will be inferred from the first argument to the handler function which must |
| 49 | + /// implement [`TypedPath`]. |
| 50 | + pub fn typed_head<H, I, O, T, P>(self, handler: H) -> Self |
| 51 | + where |
| 52 | + H: axum::handler::Handler<T, S> + OperationHandler<I, O>, |
| 53 | + T: SecondElementIs<P> + 'static, |
| 54 | + I: OperationInput, |
| 55 | + O: OperationOutput, |
| 56 | + P: axum_extra::routing::TypedPath + schemars::JsonSchema + OperationInput, |
| 57 | + { |
| 58 | + self.api_route(P::PATH, crate::axum::routing::head(handler)) |
| 59 | + } |
| 60 | + |
| 61 | + /// Add a typed `OPTIONS` route to the router. |
| 62 | + /// |
| 63 | + /// The path will be inferred from the first argument to the handler function which must |
| 64 | + /// implement [`TypedPath`]. |
| 65 | + pub fn typed_options<H, I, O, T, P>(self, handler: H) -> Self |
| 66 | + where |
| 67 | + H: axum::handler::Handler<T, S> + OperationHandler<I, O>, |
| 68 | + T: SecondElementIs<P> + 'static, |
| 69 | + I: OperationInput, |
| 70 | + O: OperationOutput, |
| 71 | + P: axum_extra::routing::TypedPath + schemars::JsonSchema + OperationInput, |
| 72 | + { |
| 73 | + self.api_route(P::PATH, crate::axum::routing::options(handler)) |
| 74 | + } |
| 75 | + |
| 76 | + /// Add a typed `PATCH` route to the router. |
| 77 | + /// |
| 78 | + /// The path will be inferred from the first argument to the handler function which must |
| 79 | + /// implement [`TypedPath`]. |
| 80 | + pub fn typed_patch<H, I, O, T, P>(self, handler: H) -> Self |
| 81 | + where |
| 82 | + H: axum::handler::Handler<T, S> + OperationHandler<I, O>, |
| 83 | + T: SecondElementIs<P> + 'static, |
| 84 | + I: OperationInput, |
| 85 | + O: OperationOutput, |
| 86 | + P: axum_extra::routing::TypedPath + schemars::JsonSchema + OperationInput, |
| 87 | + { |
| 88 | + self.api_route(P::PATH, crate::axum::routing::patch(handler)) |
| 89 | + } |
| 90 | + |
| 91 | + /// Add a typed `POST` route to the router. |
| 92 | + /// |
| 93 | + /// The path will be inferred from the first argument to the handler function which must |
| 94 | + /// implement [`TypedPath`]. |
| 95 | + pub fn typed_post<H, I, O, T, P>(self, handler: H) -> Self |
| 96 | + where |
| 97 | + H: axum::handler::Handler<T, S> + OperationHandler<I, O>, |
| 98 | + T: SecondElementIs<P> + 'static, |
| 99 | + I: OperationInput, |
| 100 | + O: OperationOutput, |
| 101 | + P: axum_extra::routing::TypedPath + schemars::JsonSchema + OperationInput, |
| 102 | + { |
| 103 | + self.api_route(P::PATH, crate::axum::routing::post(handler)) |
| 104 | + } |
| 105 | + |
| 106 | + /// Add a typed `PUT` route to the router. |
| 107 | + /// |
| 108 | + /// The path will be inferred from the first argument to the handler function which must |
| 109 | + /// implement [`TypedPath`]. |
| 110 | + pub fn typed_put<H, I, O, T, P>(self, handler: H) -> Self |
| 111 | + where |
| 112 | + H: axum::handler::Handler<T, S> + OperationHandler<I, O>, |
| 113 | + T: SecondElementIs<P> + 'static, |
| 114 | + I: OperationInput, |
| 115 | + O: OperationOutput, |
| 116 | + P: axum_extra::routing::TypedPath + schemars::JsonSchema + OperationInput, |
| 117 | + { |
| 118 | + self.api_route(P::PATH, crate::axum::routing::put(handler)) |
| 119 | + } |
| 120 | + |
| 121 | + /// Add a typed `TRACE` route to the router. |
| 122 | + /// |
| 123 | + /// The path will be inferred from the first argument to the handler function which must |
| 124 | + /// implement [`TypedPath`]. |
| 125 | + pub fn typed_trace<H, I, O, T, P>(self, handler: H) -> Self |
| 126 | + where |
| 127 | + H: axum::handler::Handler<T, S> + OperationHandler<I, O>, |
| 128 | + T: SecondElementIs<P> + 'static, |
| 129 | + I: OperationInput, |
| 130 | + O: OperationOutput, |
| 131 | + P: axum_extra::routing::TypedPath + schemars::JsonSchema + OperationInput, |
| 132 | + { |
| 133 | + self.api_route(P::PATH, crate::axum::routing::trace(handler)) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +/// A wrapper around `axum_extra::routing::TypedPath` to implement `OperationInput`. |
| 138 | +/// Basically fix for Rust does not support `!Trait` and specialization on stable. |
| 139 | +#[derive(Debug)] |
| 140 | +pub struct TypedPath<T: axum_extra::routing::TypedPath + JsonSchema>(pub T); |
| 141 | + |
| 142 | +impl<T> OperationInput for TypedPath<T> |
| 143 | +where |
| 144 | + T: axum_extra::routing::TypedPath + JsonSchema, |
| 145 | +{ |
| 146 | + fn operation_input(ctx: &mut crate::generate::GenContext, operation: &mut Operation) { |
| 147 | + // `subschema_for` `description` is none, while `root_schema_for` is some |
| 148 | + let schema = ctx.schema.root_schema_for::<T>().schema; |
| 149 | + operation.description = schema.metadata.as_ref().and_then(|x| x.description.clone()); |
| 150 | + let params = parameters_from_schema(ctx, schema, ParamLocation::Path); |
| 151 | + add_parameters(ctx, operation, params); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl<T, S> axum::extract::FromRequestParts<S> for TypedPath<T> |
| 156 | +where |
| 157 | + T: DeserializeOwned + Send + axum_extra::routing::TypedPath + JsonSchema, |
| 158 | + S: Send + Sync, |
| 159 | +{ |
| 160 | + type Rejection = PathRejection; |
| 161 | + |
| 162 | + async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 163 | + let path = axum::extract::Path::<T>::from_request_parts(parts, state).await?; |
| 164 | + Ok(Self(path.0)) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +impl<T, S> axum::extract::OptionalFromRequestParts<S> for TypedPath<T> |
| 169 | +where |
| 170 | + T: DeserializeOwned + Send + 'static + axum_extra::routing::TypedPath + JsonSchema, |
| 171 | + S: Send + Sync, |
| 172 | +{ |
| 173 | + type Rejection = PathRejection; |
| 174 | + |
| 175 | + async fn from_request_parts( |
| 176 | + parts: &mut Parts, |
| 177 | + state: &S, |
| 178 | + ) -> Result<Option<Self>, Self::Rejection> { |
| 179 | + let path = axum::extract::Path::<T>::from_request_parts(parts, state).await?; |
| 180 | + Ok(path.map(|x| Self(x.0))) |
| 181 | + } |
| 182 | +} |
0 commit comments