Skip to content

Commit

Permalink
fix(aide): nest_api_service will no longer accept arbitrary services.
Browse files Browse the repository at this point in the history
this is a **breaking** change.
  • Loading branch information
tamasfe committed Nov 29, 2022
1 parent 4c41fa0 commit e6c5b46
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
46 changes: 22 additions & 24 deletions crates/aide/src/axum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,31 +448,28 @@ where

/// Alternative to [`nest_service`](Self::nest_service()) which besides nesting the service nests
/// the generated documentation as well.
pub fn nest_api_service<T>(
///
/// Due to Rust's limitations, currently this function will not
/// accept arbitrary services but only types that can be
/// converted into an [`ApiRouter`].
///
/// Thus the primary and probably the only use-case
/// of this function is nesting routers with different states.
pub fn nest_api_service(
mut self,
mut path: &str,
service: impl Into<ServiceOrApiRouter<B, T>>,
) -> Self
where
T: Service<Request<B>, Error = Infallible> + Clone + Send + 'static,
T::Response: IntoResponse,
T::Future: Send + 'static,
{
match service.into() {
ServiceOrApiRouter::Router(router) => {
path = path.trim_end_matches('/');
self.paths.extend(
router
.paths
.into_iter()
.map(|(route, path_item)| (path.to_string() + &route, path_item)),
);
self.router = self.router.nest_service(path, router.router);
}
ServiceOrApiRouter::Service(service) => {
self.router = self.router.nest_service(path, service);
}
}
service: impl Into<ApiRouter<(), B>>,
) -> Self {
let router: ApiRouter<(), B> = service.into();

path = path.trim_end_matches('/');
self.paths.extend(
router
.paths
.into_iter()
.map(|(route, path_item)| (path.to_string() + &route, path_item)),
);
self.router = self.router.nest_service(path, router.router);
self
}

Expand Down Expand Up @@ -705,6 +702,7 @@ where
}

#[cfg(test)]
#[allow(clippy::unused_async)]
mod tests {
use crate::axum::{routing, ApiRouter};
use axum::extract::State;
Expand Down Expand Up @@ -740,7 +738,7 @@ mod tests {
.api_route("/test1", routing::get(test_handler1))
.api_route(
"/test2",
routing::get(test_handler2).with_state(state.field1.clone()),
routing::get(test_handler2).with_state(state.field1),
)
.with_state(state);
let _service = app.into_make_service();
Expand Down
12 changes: 11 additions & 1 deletion crates/aide/src/axum/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ where
router: MethodRouter::<S, B, E>::new(),
}
}
/// See [axum::routing::MethodRouter] and [axum::extract::State] for more information.
/// See [`axum::routing::MethodRouter`] and [`axum::extract::State`] for more information.
pub fn with_state<S2>(self, state: S) -> ApiMethodRouter<S2, B, E> {
let router = self.router.with_state(state);
ApiMethodRouter::<S2, B, E> {
Expand All @@ -323,6 +323,16 @@ where
}
}

impl<S, B, E> Default for ApiMethodRouter<S, B, E>
where
B: HttpBody + Send + 'static,
S: Clone,
{
fn default() -> Self {
Self::new()
}
}

method_router_top_level!(delete, delete_with);
method_router_top_level!(get, get_with);
method_router_top_level!(head, head_with);
Expand Down

0 comments on commit e6c5b46

Please sign in to comment.