From 395447c04aa605f38d893f3ba46e3c6fe8c747ae Mon Sep 17 00:00:00 2001 From: David Simeonovski Date: Tue, 30 May 2023 11:11:06 +0200 Subject: [PATCH] Extract the logic for getting the operation_id to a plugin method --- spectree/plugins/base.py | 13 +++++++++++++ spectree/spec.py | 6 +----- 2 files changed, 14 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..d623a2e4 100644 --- a/spectree/spec.py +++ b/spectree/spec.py @@ -300,13 +300,9 @@ 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),