Skip to content
Clone
c9fb93e
42 lines 1.2 KB Rust
use utoipa::Modify;
use utoipa::openapi::OpenApi as OpenApiValue;
/// `#[openapi(modifiers(&PrefixModifier { prefix: ... }))]`.
pub struct PrefixModifier {
pub prefix: String,
}
impl Modify for PrefixModifier {
fn modify(&self, openapi: &mut OpenApiValue) {
let paths = std::mem::take(&mut openapi.paths.paths);
for (path, item) in paths {
let new_path = if path.starts_with('/') {
format!("{}{}", self.prefix, path)
} else {
format!("{}/{}", self.prefix, path)
};
openapi.paths.paths.insert(new_path, item);
}
}
}
#[macro_export]
macro_rules! openapi_modify_prefix {
($doc:ty, $prefix:expr) => {{
let mut __spec = <$doc as ::utoipa::OpenApi>::openapi();
let __modif = $crate::openapi::PrefixModifier {
prefix: ($prefix).to_string(),
};
::utoipa::Modify::modify(&__modif, &mut __spec);
__spec
}};
}
#[macro_export]
macro_rules! openapi_concat {
($first:ty $(, $rest:ty)* $(,)?) => {{
let mut __acc = <$first as ::utoipa::OpenApi>::openapi();
$( __acc.merge(<$rest as ::utoipa::OpenApi>::openapi()); )*
__acc
}};
}