Skip to content

Commit

Permalink
fix: add missing Configure app_data and trim paths
Browse files Browse the repository at this point in the history
Trimming the base path is useful when using scopes otherwise you could
 get the base uri repeated, eg: "/v0/v0". Obviously this depends on the
 use case so call only if you want this.
  • Loading branch information
tiagolobocastro committed Oct 10, 2021
1 parent 87f8f76 commit 02afc8c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`
- 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

Expand Down
19 changes: 19 additions & 0 deletions plugins/actix-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>(&mut self, factory: &mut F)
where
Expand Down
8 changes: 8 additions & 0 deletions plugins/actix-web/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<U: 'static>(&mut self, data: U) -> &mut Self {
self.inner.app_data(data);
self
}
}

0 comments on commit 02afc8c

Please sign in to comment.