Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify operation-id default gen rule #1306

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 38 additions & 33 deletions utoipa-gen/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,27 +334,6 @@ impl<'p> Path<'p> {

impl<'p> ToTokensDiagnostics for Path<'p> {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) -> Result<(), Diagnostics> {
let fn_name = &*self.fn_ident.to_string();
let operation_id = self
.path_attr
.operation_id
.clone()
.or(Some(
ExprLit {
attrs: vec![],
lit: Lit::Str(LitStr::new(fn_name, Span::call_site())),
}
.into(),
))
.ok_or_else(|| {
Diagnostics::new("operation id is not defined for path")
.help(format!(
"Try to define it in #[utoipa::path(operation_id = {})]",
&fn_name
))
.help("Did you define the #[utoipa::path(...)] over function?")
})?;

let methods = if !self.path_attr.methods.is_empty() {
&self.path_attr.methods
} else {
Expand Down Expand Up @@ -404,26 +383,52 @@ impl<'p> ToTokensDiagnostics for Path<'p> {
diagnostics()
})?;

let path_with_context_path = self
.path_attr
.context_path
.as_ref()
let context_path_opt = self.path_attr.context_path.as_ref();

let path_with_context_path = context_path_opt
.map(|context_path| {
let context_path = context_path.to_token_stream();
let context_path_tokens = quote! {
format!("{}{}",
#context_path,
#path
)
};
context_path_tokens
let context_path_tokens = context_path.to_token_stream();
quote! {
format!("{}{}", #context_path_tokens, #path)
}
})
.unwrap_or_else(|| {
quote! {
String::from(#path)
}
});

let path_with_context_path_str = context_path_opt
.map(|context_path| format!("{}{}", context_path, path))
.unwrap_or_else(|| format!("{}", path));

let operation_id = self
.path_attr
.operation_id
.clone()
.or_else(|| {
Some(
ExprLit {
attrs: vec![],
lit: Lit::Str(LitStr::new(
&path_with_context_path_str
.replace("\"", "")
.replace("/", "_"),
Span::call_site(),
)),
}
.into(),
)
})
.ok_or_else(|| {
Diagnostics::new("operation id is not defined for path")
.help(format!(
"Try to define it in #[utoipa::path(operation_id = {})]",
path_with_context_path_str
))
.help("Did you define the #[utoipa::path(...)] over function?")
})?;

let split_comment = self.doc_comments.as_ref().map(|comments| {
let mut split = comments.split(|comment| comment.trim().is_empty());
let summary = split
Expand Down
103 changes: 70 additions & 33 deletions utoipa/src/openapi/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,9 @@ builder! {
impl PathItem {
/// Construct a new [`PathItem`] with provided [`Operation`] mapped to given [`HttpMethod`].
pub fn new<O: Into<Operation>>(http_method: HttpMethod, operation: O) -> Self {
let mut path_item = Self::default();
match http_method {
HttpMethod::Get => path_item.get = Some(operation.into()),
HttpMethod::Put => path_item.put = Some(operation.into()),
HttpMethod::Post => path_item.post = Some(operation.into()),
HttpMethod::Delete => path_item.delete = Some(operation.into()),
HttpMethod::Options => path_item.options = Some(operation.into()),
HttpMethod::Head => path_item.head = Some(operation.into()),
HttpMethod::Patch => path_item.patch = Some(operation.into()),
HttpMethod::Trace => path_item.trace = Some(operation.into()),
};
let path_item = Self::default();

path_item
Self::parse_http_method(path_item, http_method, &operation.into())
}

/// Constructs a new [`PathItem`] with given [`Operation`] set for provided [`HttpMethod`]s.
Expand All @@ -314,21 +304,73 @@ impl PathItem {
let mut path_item = Self::default();
let operation = operation.into();
for method in http_methods {
match method {
HttpMethod::Get => path_item.get = Some(operation.clone()),
HttpMethod::Put => path_item.put = Some(operation.clone()),
HttpMethod::Post => path_item.post = Some(operation.clone()),
HttpMethod::Delete => path_item.delete = Some(operation.clone()),
HttpMethod::Options => path_item.options = Some(operation.clone()),
HttpMethod::Head => path_item.head = Some(operation.clone()),
HttpMethod::Patch => path_item.patch = Some(operation.clone()),
HttpMethod::Trace => path_item.trace = Some(operation.clone()),
};
path_item = Self::parse_http_method(path_item, method, &operation);
}

path_item
}

/// parse http method and operation, handler operation_id with path and method name
fn parse_http_method(
mut path_item: PathItem,
http_method: HttpMethod,
operation: &Operation,
) -> PathItem {
match http_method {
HttpMethod::Get => {
path_item.get = Some(operation.clone());
if let Some(ref mut op) = path_item.get {
op.operation_id = Some(format!("{}_get", &op.operation_id.as_ref().unwrap()));
}
}
HttpMethod::Put => {
path_item.put = Some(operation.clone());
if let Some(ref mut op) = path_item.put {
op.operation_id = Some(format!("{}_put", &op.operation_id.as_ref().unwrap()));
}
}
HttpMethod::Post => {
path_item.post = Some(operation.clone());
if let Some(ref mut op) = path_item.post {
op.operation_id = Some(format!("{}_post", &op.operation_id.as_ref().unwrap()));
}
}
HttpMethod::Delete => {
path_item.delete = Some(operation.clone());
if let Some(ref mut op) = path_item.delete {
op.operation_id =
Some(format!("{}_delete", &op.operation_id.as_ref().unwrap()));
}
}
HttpMethod::Options => {
path_item.options = Some(operation.clone());
if let Some(ref mut op) = path_item.options {
op.operation_id =
Some(format!("{}_options", &op.operation_id.as_ref().unwrap()));
}
}
HttpMethod::Head => {
path_item.head = Some(operation.clone());
if let Some(ref mut op) = path_item.head {
op.operation_id = Some(format!("{}_head", &op.operation_id.as_ref().unwrap()));
}
}
HttpMethod::Patch => {
path_item.patch = Some(operation.clone());
if let Some(ref mut op) = path_item.patch {
op.operation_id = Some(format!("{}_patch", &op.operation_id.as_ref().unwrap()));
}
}
HttpMethod::Trace => {
path_item.trace = Some(operation.clone());
if let Some(ref mut op) = path_item.trace {
op.operation_id = Some(format!("{}_trace", &op.operation_id.as_ref().unwrap()));
}
}
};
path_item
}

/// Merge all defined [`Operation`]s from given [`PathItem`] to `self` if `self` does not have
/// existing operation.
pub fn merge_operations(&mut self, path_item: PathItem) {
Expand Down Expand Up @@ -362,17 +404,12 @@ impl PathItem {
impl PathItemBuilder {
/// Append a new [`Operation`] by [`HttpMethod`] to this [`PathItem`]. Operations can
/// hold only one operation per [`HttpMethod`].
pub fn operation<O: Into<Operation>>(mut self, http_method: HttpMethod, operation: O) -> Self {
match http_method {
HttpMethod::Get => self.get = Some(operation.into()),
HttpMethod::Put => self.put = Some(operation.into()),
HttpMethod::Post => self.post = Some(operation.into()),
HttpMethod::Delete => self.delete = Some(operation.into()),
HttpMethod::Options => self.options = Some(operation.into()),
HttpMethod::Head => self.head = Some(operation.into()),
HttpMethod::Patch => self.patch = Some(operation.into()),
HttpMethod::Trace => self.trace = Some(operation.into()),
};
pub fn operation<O: Into<Operation> + Clone>(
mut self,
http_method: HttpMethod,
operation: O,
) -> Self {
self = PathItem::parse_http_method(self.into(), http_method, &operation.into()).into();

self
}
Expand Down