Skip to content

Commit

Permalink
Optional prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
VersBinarii committed Jan 17, 2023
1 parent 7ada5b6 commit 717f407
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 12 additions & 0 deletions poem-openapi/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ pub struct OpenApiService<T, W> {
cookie_key: Option<CookieKey>,
extra_response_headers: Vec<(ExtraHeader, MetaSchemaRef, bool)>,
extra_request_headers: Vec<(ExtraHeader, MetaSchemaRef, bool)>,
url_prefix: Option<String>,
}

impl<T> OpenApiService<T, ()> {
Expand All @@ -244,6 +245,7 @@ impl<T> OpenApiService<T, ()> {
cookie_key: None,
extra_response_headers: vec![],
extra_request_headers: vec![],
url_prefix: None,
}
}
}
Expand All @@ -260,6 +262,7 @@ impl<T, W> OpenApiService<T, W> {
cookie_key: self.cookie_key,
extra_response_headers: self.extra_response_headers,
extra_request_headers: self.extra_request_headers,
url_prefix: None,
}
}

Expand Down Expand Up @@ -373,6 +376,14 @@ impl<T, W> OpenApiService<T, W> {
}
}

/// Sets optional URl prefix to be added to path
pub fn url_prefix(self, url_prefix: impl Into<String>) -> Self {
Self {
url_prefix: Some(url_prefix.into()),
..self
}
}

/// Create the OpenAPI Explorer endpoint.
#[must_use]
#[cfg(feature = "openapi-explorer")]
Expand Down Expand Up @@ -554,6 +565,7 @@ impl<T, W> OpenApiService<T, W> {
webhooks,
registry,
external_document: self.external_document.as_ref(),
url_prefix: self.url_prefix.as_deref(),
};
doc.remove_unused_schemas();

Expand Down
10 changes: 7 additions & 3 deletions poem-openapi/src/registry/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ impl Serialize for MetaSchemaRef {
}
}

struct PathMap<'a>(&'a [MetaApi]);
struct PathMap<'a>(&'a [MetaApi], Option<&'a str>);

impl<'a> Serialize for PathMap<'a> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut s = serializer.serialize_map(Some(self.0.len()))?;
for api in self.0 {
for path in &api.paths {
s.serialize_entry(path.path, path)?;
match self.1 {
Some(p) => s.serialize_entry(&format!("{}{}", p, path.path), path)?,
None => s.serialize_entry(path.path, path)?,
}
}
}
s.end()
Expand Down Expand Up @@ -80,6 +83,7 @@ pub(crate) struct Document<'a> {
pub(crate) webhooks: Vec<MetaWebhook>,
pub(crate) registry: Registry,
pub(crate) external_document: Option<&'a MetaExternalDocument>,
pub(crate) url_prefix: Option<&'a str>,
}

impl<'a> Serialize for Document<'a> {
Expand All @@ -101,7 +105,7 @@ impl<'a> Serialize for Document<'a> {
if !self.webhooks.is_empty() {
s.serialize_entry("webhooks", &WebhookMap(&self.webhooks))?;
}
s.serialize_entry("paths", &PathMap(&self.apis))?;
s.serialize_entry("paths", &PathMap(&self.apis, self.url_prefix))?;
s.serialize_entry(
"components",
&Components {
Expand Down

0 comments on commit 717f407

Please sign in to comment.