diff --git a/CHANGELOG.md b/CHANGELOG.md index 69121edfb..6e14425b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add support for actix-web-macros methods routing [PR#289](https://github.com/wafflespeanut/paperclip/pull/289) - Actix plugin: add an empty impl for actix-web `ReqData` - Add support for the `#[serde(skip)]` attribute in structs and enums. +- Added new method `trim_base_path` to trim the api base path from all method paths. ### Changed diff --git a/plugins/actix-web/src/lib.rs b/plugins/actix-web/src/lib.rs index 8e44ea30c..031d2ed2d 100644 --- a/plugins/actix-web/src/lib.rs +++ b/plugins/actix-web/src/lib.rs @@ -286,6 +286,25 @@ where self.inner.expect("missing app?") } + /// Trim's the Api base path from the start of all method paths. + /// **NOTE:** much like `with_raw_json_spec` this only has the API spec built until + /// this function call. Any route handler added after this call won't have the base path trimmed. + /// So, it's important to call this function after adding all route handlers. + pub fn trim_base_path(self) -> Self { + { + let mut spec = self.spec.write(); + let base_path = spec.base_path.clone().unwrap_or_default(); + spec.paths = spec.paths.iter().fold(BTreeMap::new(), |mut i, (k, v)| { + i.insert( + k.trim_start_matches(base_path.as_str()).to_string(), + v.clone(), + ); + i + }); + } + self + } + /// Updates the underlying spec with definitions and operations from the given factory. fn update_from_mountable(&mut self, factory: &mut F) where diff --git a/plugins/actix-web/src/web.rs b/plugins/actix-web/src/web.rs index 65fc49511..c9576b450 100644 --- a/plugins/actix-web/src/web.rs +++ b/plugins/actix-web/src/web.rs @@ -774,4 +774,12 @@ impl<'a> ServiceConfig<'a> { self.inner.external_resource(name, url); self } + + /// Proxy for [`actix_web::web::ServiceConfig::app_data`](https://docs.rs/actix-web/3.3.2/actix_web/web/struct.ServiceConfig.html#method.app_data). + /// + /// **NOTE:** This doesn't affect spec generation. + pub fn app_data(&mut self, data: U) -> &mut Self { + self.inner.app_data(data); + self + } }