Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #37 from lendingblock/master
Browse files Browse the repository at this point in the history
tags
  • Loading branch information
lsbardel authored Jul 5, 2018
2 parents fbaadf2 + 17ae3c6 commit 0092daa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion openapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Minimal OpenAPI asynchronous server application
"""
__version__ = '0.2.2'
__version__ = '0.2.3'
26 changes: 21 additions & 5 deletions openapi/spec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/example/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 0092daa

Please sign in to comment.