diff --git a/openapi/__init__.py b/openapi/__init__.py index d776a26..716d19e 100644 --- a/openapi/__init__.py +++ b/openapi/__init__.py @@ -1,3 +1,3 @@ """Minimal OpenAPI asynchronous server application """ -__version__ = '0.2.2' +__version__ = '0.2.3' diff --git a/openapi/spec/spec.py b/openapi/spec/spec.py index ed671f1..90b7796 100644 --- a/openapi/spec/spec.py +++ b/openapi/spec/spec.py @@ -192,7 +192,8 @@ def _build_paths(self, app): paths[path] = self._build_path_object(handler, app) def _build_path_object(self, handler, path_obj): - path_obj = {} + path_obj = load_yaml_from_docstring(handler.__doc__) or {} + tags = self._extend_tags(path_obj.pop('tags', None)) for method in METHODS: method_handler = getattr(handler, method, None) if method_handler is None: @@ -205,15 +206,16 @@ def _build_path_object(self, handler, path_obj): ) continue - method_doc = load_yaml_from_docstring(method_handler.__doc__) + method_doc = load_yaml_from_docstring(method_handler.__doc__) or {} + mtags = tags.copy() + mtags.update(self._extend_tags(method_doc.pop('tags', None))) op_attrs = asdict(operation) self._add_schemas_from_operation(op_attrs) responses = self._get_resonse_object(op_attrs, method_doc) request_body = self._get_request_body_object(op_attrs, method_doc) - path_obj[method] = { - 'description': method_doc['summary'] - } + method_doc['tags'] = list(mtags) + path_obj[method] = method_doc if responses is not None: path_obj[method]['responses'] = responses @@ -275,6 +277,20 @@ def _add_schemas_from_operation(self, operation_obj): schema_obj = schema_obj[0] self.schemas_to_parse.add(schema_obj) + def _extend_tags(self, tags): + names = set() + for tag in (tags or ()): + if isinstance(tag, str): + tag = {'name': tag} + name = tag.get('name') + if name: + if name not in self.tags: + self.tags[name] = tag + else: + self.tags[name].update(tag) + names.add(name) + return names + async def spec_root(request): """Return the OpenApi spec diff --git a/tests/example/endpoints.py b/tests/example/endpoints.py index 016a4d9..3727792 100644 --- a/tests/example/endpoints.py +++ b/tests/example/endpoints.py @@ -67,7 +67,9 @@ class TaskPath(SqlApiPath): --- summary: Create and query tasks tags: - - task + - name: task + description: simple description + - name: random """ table = 'tasks' path_schema = TaskPathSchema diff --git a/tests/test_db.py b/tests/test_db.py index 95f89ff..fd573e3 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -138,3 +138,7 @@ async def test_spec_root(cli): response = await cli.get('/spec') spec = await jsonBody(response) assert 'paths' in spec + assert 'tags' in spec + assert len(spec['tags']) == 2 + assert spec['tags'][1]['name'] == 'task' + assert spec['tags'][1]['description'] == 'simple description'