Skip to content

Commit

Permalink
fix: add missing Configure app_data and trim paths (paperclip-rs#312)
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 authored and silathdiir committed Dec 13, 2021
1 parent 80de67a commit 1e4c9a2
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 @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Expose openapi v3 spec through `with_json_spec_v3_at` and `with_raw_json_spec_v3` - this is done through a conversion from
the v2 types to v3 and so all existing code should be valid. It also means that we're not yet exposing any specific
v3 features.
- Added new method `trim_base_path` to trim the api base path from all method paths.

### Changed
- Actix plugin: `#[api_v2_errors]` macro now supports adding different error schemes per response code.
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 @@ -344,6 +344,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 @@ -758,4 +758,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 1e4c9a2

Please sign in to comment.