pub struct OpenApiRouter<S = ()>(/* private fields */);Expand description
A wrapper struct for axum::Router and utoipa::openapi::OpenApi for composing handlers
and services with collecting OpenAPI information from the handlers.
This struct provides pass through implementation for most of the axum::Router methods and
extends capabilities for few to collect the OpenAPI information. Methods that are not
implemented can be easily called after converting this router to axum::Router by
Into::into.
§Examples
Create new OpenApiRouter with default values populated from cargo environment variables.
let _: OpenApiRouter = OpenApiRouter::new();Instantiate a new OpenApiRouter with new empty utoipa::openapi::OpenApi.
let _: OpenApiRouter = OpenApiRouter::default();Implementations§
Source§impl<S> OpenApiRouter<S>
impl<S> OpenApiRouter<S>
Sourcepub fn new() -> OpenApiRouter<S>
pub fn new() -> OpenApiRouter<S>
Instantiate a new OpenApiRouter with default values populated from cargo environment
variables. This creates an OpenApi similar of creating a new OpenApi via
#[derive(OpenApi)]
If you want to create OpenApiRouter with completely empty utoipa::openapi::OpenApi
instance, use OpenApiRouter::default().
Sourcepub fn with_openapi(openapi: OpenApi) -> OpenApiRouter<S>
pub fn with_openapi(openapi: OpenApi) -> OpenApiRouter<S>
Instantiates a new OpenApiRouter with given openapi instance.
This function allows using existing utoipa::openapi::OpenApi as source for this router.
§Examples
Use derived utoipa::openapi::OpenApi as source for OpenApiRouter.
#[derive(utoipa::ToSchema)]
struct Todo {
id: i32,
}
#[derive(utoipa::OpenApi)]
#[openapi(components(schemas(Todo)))]
struct Api;
let mut router: OpenApiRouter = OpenApiRouter::with_openapi(Api::openapi());Sourcepub fn as_service<B>(&mut self) -> RouterAsService<'_, B, S>
pub fn as_service<B>(&mut self) -> RouterAsService<'_, B, S>
Pass through method for axum::Router::as_service.
Sourcepub fn fallback<H, T>(self, handler: H) -> OpenApiRouter<S>where
H: Handler<T, S>,
T: 'static,
pub fn fallback<H, T>(self, handler: H) -> OpenApiRouter<S>where
H: Handler<T, S>,
T: 'static,
Pass through method for axum::Router::fallback.
Sourcepub fn fallback_service<T>(self, service: T) -> OpenApiRouter<S>
pub fn fallback_service<T>(self, service: T) -> OpenApiRouter<S>
Pass through method for axum::Router::fallback_service.
Sourcepub fn layer<L>(self, layer: L) -> OpenApiRouter<S>where
L: Layer<Route> + Clone + Send + Sync + 'static,
<L as Layer<Route>>::Service: Service<Request<Body>> + Clone + Send + Sync + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Response: IntoResponse + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Infallible> + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send + 'static,
pub fn layer<L>(self, layer: L) -> OpenApiRouter<S>where
L: Layer<Route> + Clone + Send + Sync + 'static,
<L as Layer<Route>>::Service: Service<Request<Body>> + Clone + Send + Sync + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Response: IntoResponse + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Infallible> + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send + 'static,
Pass through method for axum::Router::layer.
Sourcepub fn routes(
self,
_: (Vec<(String, RefOr<Schema>)>, Paths, MethodRouter<S>),
) -> OpenApiRouter<S>
pub fn routes( self, _: (Vec<(String, RefOr<Schema>)>, Paths, MethodRouter<S>), ) -> OpenApiRouter<S>
Register UtoipaMethodRouter content created with routes macro to self.
Paths of the UtoipaMethodRouter will be extended to utoipa::openapi::OpenApi and
axum::routing::MethodRouter will be added to the axum::Router.
Sourcepub fn route(
self,
path: &str,
method_router: MethodRouter<S>,
) -> OpenApiRouter<S>
pub fn route( self, path: &str, method_router: MethodRouter<S>, ) -> OpenApiRouter<S>
Pass through method for axum::Router<S>::route.
Sourcepub fn route_layer<L>(self, layer: L) -> OpenApiRouter<S>where
L: Layer<Route> + Clone + Send + Sync + 'static,
<L as Layer<Route>>::Service: Service<Request<Body>> + Clone + Send + Sync + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Response: IntoResponse + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Infallible> + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send + 'static,
pub fn route_layer<L>(self, layer: L) -> OpenApiRouter<S>where
L: Layer<Route> + Clone + Send + Sync + 'static,
<L as Layer<Route>>::Service: Service<Request<Body>> + Clone + Send + Sync + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Response: IntoResponse + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Infallible> + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send + 'static,
Pass through method for axum::Router::route_layer.
Sourcepub fn route_service<T>(self, path: &str, service: T) -> OpenApiRouter<S>
pub fn route_service<T>(self, path: &str, service: T) -> OpenApiRouter<S>
Pass through method for axum::Router<S>::route_service.
Sourcepub fn nest(self, path: &str, router: OpenApiRouter<S>) -> OpenApiRouter<S>
pub fn nest(self, path: &str, router: OpenApiRouter<S>) -> OpenApiRouter<S>
Nest router to self under given path. Router routes will be nested with
axum::Router::nest.
This method expects OpenApiRouter instance in order to nest OpenApi paths and router
routes. If you wish to use axum::Router::nest you need to first convert this instance
to axum::Router (let _: Router = OpenApiRouter::new().into()).
§Examples
Nest two routers.
#[utoipa::path(get, path = "/search")]
async fn search() {}
let search_router = OpenApiRouter::new()
.routes(utoipa_axum::routes!(search));
let router: OpenApiRouter = OpenApiRouter::new()
.nest("/api", search_router);Sourcepub fn nest_service<T>(self, path: &str, service: T) -> OpenApiRouter<S>
pub fn nest_service<T>(self, path: &str, service: T) -> OpenApiRouter<S>
Pass through method for axum::Router::nest_service. This does nothing for OpenApi paths.
Sourcepub fn merge(self, router: OpenApiRouter<S>) -> OpenApiRouter<S>
pub fn merge(self, router: OpenApiRouter<S>) -> OpenApiRouter<S>
Merge utoipa::openapi::path::Paths from router to self and merge Router routes
and fallback with axum::Router::merge.
This method expects OpenApiRouter instance in order to merge OpenApi paths and router
routes. If you wish to use axum::Router::merge you need to first convert this instance
to axum::Router (let _: Router = OpenApiRouter::new().into()).
§Examples
Merge two routers.
#[utoipa::path(get, path = "/search")]
async fn search() {}
let search_router = OpenApiRouter::new()
.routes(utoipa_axum::routes!(search));
let router: OpenApiRouter = OpenApiRouter::new()
.merge(search_router);Sourcepub fn with_state<S2>(self, state: S) -> OpenApiRouter<S2>
pub fn with_state<S2>(self, state: S) -> OpenApiRouter<S2>
Pass through method for axum::Router::with_state.
Sourcepub fn into_openapi(self) -> OpenApi
pub fn into_openapi(self) -> OpenApi
Consume self returning the utoipa::openapi::OpenApi instance of the
OpenApiRouter.
Sourcepub fn to_openapi(&mut self) -> OpenApi
pub fn to_openapi(&mut self) -> OpenApi
Take the utoipa::openapi::OpenApi instance without consuming the OpenApiRouter.
Sourcepub fn get_openapi(&self) -> &OpenApi
pub fn get_openapi(&self) -> &OpenApi
Get reference to the utoipa::openapi::OpenApi instance of the router.
Sourcepub fn get_openapi_mut(&mut self) -> &mut OpenApi
pub fn get_openapi_mut(&mut self) -> &mut OpenApi
Get mutable reference to the utoipa::openapi::OpenApi instance of the router.
Sourcepub fn split_for_parts(self) -> (Router<S>, OpenApi)
pub fn split_for_parts(self) -> (Router<S>, OpenApi)
Split the content of the OpenApiRouter to parts. Method will return a tuple of
inner axum::Router and utoipa::openapi::OpenApi.
Trait Implementations§
Source§impl<S> Clone for OpenApiRouter<S>where
S: Clone,
impl<S> Clone for OpenApiRouter<S>where
S: Clone,
Source§fn clone(&self) -> OpenApiRouter<S>
fn clone(&self) -> OpenApiRouter<S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<S> Default for OpenApiRouter<S>
impl<S> Default for OpenApiRouter<S>
Source§fn default() -> OpenApiRouter<S>
fn default() -> OpenApiRouter<S>
Source§impl<S> From<Router<S>> for OpenApiRouter<S>
impl<S> From<Router<S>> for OpenApiRouter<S>
Source§fn from(value: Router<S>) -> OpenApiRouter<S>
fn from(value: Router<S>) -> OpenApiRouter<S>
Source§impl<S: Clone + Send + Sync + 'static> OpenApiRouterExt<S> for OpenApiRouter<S>
impl<S: Clone + Send + Sync + 'static> OpenApiRouterExt<S> for OpenApiRouter<S>
Source§fn layer_documented<L>(self, layer: L) -> Selfwhere
L: Layer<Route> + DocumentedLayer + Clone + Send + Sync + 'static,
L::Service: Service<Request> + Clone + Send + Sync + 'static,
<L::Service as Service<Request>>::Response: IntoResponse + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
fn layer_documented<L>(self, layer: L) -> Selfwhere
L: Layer<Route> + DocumentedLayer + Clone + Send + Sync + 'static,
L::Service: Service<Request> + Clone + Send + Sync + 'static,
<L::Service as Service<Request>>::Response: IntoResponse + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
layer exactly like
OpenApiRouter::layer, and stamp
layer.contribution() onto every operation currently
present in the router. Routes added after this call (via
further merge,
nest, or
route) are unaffected — same
semantic as axum::Router::layer. Read more