Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
operationId on every operation of the API spec (#443)
Murtaza Nasire2cea12bff4f
diff --git a/src/api/api_v1.py b/src/api/api_v1.pyindex c0c1400e..a599207f 100644--- a/src/api/api_v1.py+++ b/src/api/api_v1.py@@ -629,27 +629,50 @@ OPENAPI_SPEC = {def _openapi_with_scopes():"""OPENAPI_SPEC with x-required-scopes on every operation, read from the- routes' require_scope marks so the document cannot drift from the code."""+ routes' require_scope marks so the document cannot drift from the code.++ Every operation also gets an operationId (#443), which tools that turn+ an OpenAPI document into LLM tools (Open WebUI, for one) require. It is+ the view function's name, with the method appended when one function+ serves several methods, so it stays the same across releases unless the+ function is renamed."""import copyimport refrom flask import current_appspec = copy.deepcopy(OPENAPI_SPEC)norm = lambda path: re.sub(r'<[^>]+>|\{[^}]+\}', '{}', path)by_route = {}+ view_names = {}for rule in current_app.url_map.iter_rules():if not rule.rule.startswith('/api/v1/'):continue+ name = rule.endpoint.rsplit('.', 1)[-1]+ methods = rule.methods - {'HEAD', 'OPTIONS'}+ for method in methods:+ view_names[(norm(rule.rule[len('/api/v1'):]), method.lower())] = (name, len(methods) > 1)view = current_app.view_functions.get(rule.endpoint)scopes = getattr(view, '_required_scopes', None)if scopes is None:continue- for method in rule.methods - {'HEAD', 'OPTIONS'}:+ for method in methods:by_route[(norm(rule.rule[len('/api/v1'):]), method.lower())] = sorted(scopes)+ used_ids = set()for path, operations in spec.get('paths', {}).items():for method, operation in operations.items():+ if not isinstance(operation, dict):+ continuescopes = by_route.get((norm(path), method))- if scopes is not None and isinstance(operation, dict):+ if scopes is not None:operation['x-required-scopes'] = scopes+ if 'operationId' not in operation:+ name, shared = view_names.get(+ (norm(path), method),+ (re.sub(r'\W+', '_', path).strip('_'), True))+ op_id = f'{name}_{method}' if shared else name+ while op_id in used_ids:+ op_id += '_'+ operation['operationId'] = op_id+ used_ids.add(operation['operationId'])return specdiff --git a/tests/test_openapi_operation_ids.py b/tests/test_openapi_operation_ids.pynew file mode 100644index 00000000..f5695a2b--- /dev/null+++ b/tests/test_openapi_operation_ids.py@@ -0,0 +1,28 @@+"""Every operation in /api/v1/openapi.json has a unique operationId (#443).++Tools that turn an OpenAPI document into LLM tools, Open WebUI among them,+create a tool only for an operation that has one.+"""++from src.app import app+from src.api.api_v1 import _openapi_with_scopes+++def _operations():+ with app.test_request_context():+ spec = _openapi_with_scopes()+ return [(method, path, op) for path, ops in spec['paths'].items()+ for method, op in ops.items() if isinstance(op, dict)]+++def test_every_operation_has_a_unique_operation_id():+ ops = _operations()+ ids = [op.get('operationId') for _, _, op in ops]+ assert all(ids)+ assert len(ids) == len(set(ids))+++def test_operation_ids_come_from_the_view_functions():+ ids = {(m, p): op['operationId'] for m, p, op in _operations()}+ assert ids[('get', '/recordings')] == 'list_recordings'+ assert ids[('get', '/search')] == 'search_recordings_v1'