From cc2ea3e74bb5ad44cedbeac4139dcb1681b555d8 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 30 May 2023 11:18:35 +0200 Subject: [PATCH] Extract the logic for getting the operation_id to a plugin method (#313) --- spectree/plugins/base.py | 13 +++++++++++++ spectree/spec.py | 8 +++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/spectree/plugins/base.py b/spectree/plugins/base.py index 96357379..819c68e9 100644 --- a/spectree/plugins/base.py +++ b/spectree/plugins/base.py @@ -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 diff --git a/spectree/spec.py b/spectree/spec.py index 66836bbc..17dc2847 100644 --- a/spectree/spec.py +++ b/spectree/spec.py @@ -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),