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>
Auto Trait Implementations§
impl<S> Freeze for OpenApiRouter<S>
impl<S = ()> !RefUnwindSafe for OpenApiRouter<S>
impl<S> Send for OpenApiRouter<S>
impl<S> Sync for OpenApiRouter<S>
impl<S> Unpin for OpenApiRouter<S>
impl<S = ()> !UnwindSafe for OpenApiRouter<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more