Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: diverging axum route and openapi spec #1199

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion utoipa-axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ mod tests {
let paths = router.to_openapi().paths;
let expected_paths = utoipa::openapi::path::PathsBuilder::new()
.path(
"/api/customer/",
"/api/customer",
utoipa::openapi::PathItem::new(
utoipa::openapi::path::HttpMethod::Get,
utoipa::openapi::path::OperationBuilder::new()
Expand Down
22 changes: 20 additions & 2 deletions utoipa-axum/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,26 @@ where
/// .nest("/api", search_router);
/// ```
pub fn nest(self, path: &str, router: OpenApiRouter<S>) -> Self {
let api = self.1.nest(path_template(path), router.1);
let path = if path.is_empty() { "/" } else { path };
// from axum::routing::path_router::path_for_nested_route
// method is private, so we need to replicate it here
fn path_for_nested_route<'a>(prefix: &'a str, path: &'a str) -> String {
debug_assert!(prefix.starts_with('/'));
debug_assert!(path.starts_with('/'));

if prefix.ends_with('/') {
format!("{prefix}{}", path.trim_start_matches('/')).into()
} else if path == "/" {
prefix.into()
} else {
format!("{prefix}{path}").into()
}
}

let api = self.1.nest_with_path_composer(
path_for_nested_route(path, "/"),
router.1,
|a: &str, b: &str| path_for_nested_route(a, b),
);
let router = self.0.nest(&colonized_params(path), router.0);

Self(router, api)
Expand Down
23 changes: 21 additions & 2 deletions utoipa/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,26 @@ impl OpenApi {
/// .build();
/// let nested = api.nest("/api/v1/user", user_api);
/// ```
pub fn nest<P: Into<String>, O: Into<OpenApi>>(mut self, path: P, other: O) -> Self {
pub fn nest<P: Into<String>, O: Into<OpenApi>>(self, path: P, other: O) -> Self {
self.nest_with_path_composer(path, other, |base, path| format!("{base}{path}"))
}

/// Nest `other` [`OpenApi`] with custom path composer.
///
/// In most cases you should use [`OpenApi::nest`] instead.
/// Only use this method if you need custom path composition for a specific use case.
///
/// `composer` is a function that takes two strings, the base path and the path to nest, and returns the composed path for the API Specification.
pub fn nest_with_path_composer<
P: Into<String>,
O: Into<OpenApi>,
F: Fn(&str, &str) -> String,
>(
mut self,
path: P,
other: O,
composer: F,
) -> Self {
let path: String = path.into();
let mut other_api: OpenApi = other.into();

Expand All @@ -288,7 +307,7 @@ impl OpenApi {
.paths
.into_iter()
.map(|(item_path, item)| {
let path = format!("{path}{item_path}");
let path = composer(&path, &item_path);
(path, item)
})
.collect::<PathsMap<_, _>>();
Expand Down
Loading