Skip to content

Commit

Permalink
Extract the logic for getting the operation_id to a plugin method
Browse files Browse the repository at this point in the history
  • Loading branch information
david-simeonovski committed May 30, 2023
1 parent 0508b70 commit d7b55a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
13 changes: 13 additions & 0 deletions spectree/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,16 @@ def parse_func(self, route: BackendRoute):
get the endpoint function from routes
"""
raise NotImplementedError

def get_func_operation_id(self, func: Callable, path: str, method: str):
"""
:param func: route function (endpoint)
:param method: URI path for this route function
:param method: HTTP method for this route function
get the operation_id value for the endpoint
"""
operation_id = getattr(func, "operation_id", None)
if not operation_id:
operation_id = f"{method.lower()}_{path.replace('/', '_')}"
return operation_id
8 changes: 3 additions & 5 deletions spectree/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,11 @@ def _generate_spec(self) -> Dict[str, Any]:
tag.dict() if isinstance(tag, Tag) else {"name": tag}
)

operation_id = getattr(func, "operation_id", None)
if not operation_id:
operation_id = f"{method.lower()}_{path.replace('/', '_')}"

routes[path][method.lower()] = {
"summary": summary or f"{name} <{method}>",
"operationId": operation_id,
"operationId": self.backend.get_func_operation_id(
func, path, method
),
"description": desc or "",
"tags": [str(x) for x in getattr(func, "tags", ())],
"parameters": parse_params(func, parameters[:], self.models),
Expand Down

0 comments on commit d7b55a9

Please sign in to comment.