Skip to content

Commit

Permalink
Merge pull request sphinx-contrib#78 from sphinx-contrib/pr/64
Browse files Browse the repository at this point in the history
Add methods option
  • Loading branch information
ikalnytskyi authored Mar 22, 2020
2 parents 660fca0 + 12c6071 commit ac78f12
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ The ``openapi`` directive supports the following options:
Would render ``/persons`` and ``/evidence`` endpoints, but not
``/evidence/{pk}`` endpoints

``methods``
A line separated list of http methods to filter included openapi
spec. For example:

.. code:: restructuredtext
.. openapi:: specs/openapi.yml
:methods:
get
post
put
:encoding: utf-8
Would render paths with get, post or put method

``exclude``, ``include`` and ``paths`` can also be used together (``exclude``
taking precedence over ``include`` and ``paths``)

Expand Down
4 changes: 4 additions & 0 deletions sphinxcontrib/openapi/openapi20.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ def openapihttpdomain(spec, **options):

for endpoint in paths:
for method, properties in spec['paths'][endpoint].items():
if options.get('methods') and method not in options.get('methods'):
continue
key = properties.get('tags', [''])[0]
groups.setdefault(key, []).append(_httpresource(
endpoint,
Expand All @@ -249,6 +251,8 @@ def openapihttpdomain(spec, **options):
else:
for endpoint in paths:
for method, properties in spec['paths'][endpoint].items():
if options.get('methods') and method not in options.get('methods'):
continue
generators.append(_httpresource(
endpoint,
method,
Expand Down
2 changes: 2 additions & 0 deletions sphinxcontrib/openapi/renderers/_httpdomain_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class HttpdomainOldRenderer(abc.RestructuredTextRenderer):
# delimited.
"include": lambda s: s.split(),
"exclude": lambda s: s.split(),
# Endpoints to be included based on HTTP method names.
"methods": lambda s: s.split(),
# Render the request body structure when passed.
"request": directives.flag,
# Render request/response examples when passed.
Expand Down
84 changes: 84 additions & 0 deletions tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,48 @@ def test_exclude_option(self):
error
''').lstrip()

def test_method_option(self):
spec = collections.defaultdict(collections.OrderedDict)
spec['paths']['/resource_a'] = {
'get': {
'description': 'resource a',
'responses': {
'200': {'description': 'ok'},
}
},
'post': {
'description': 'resource a',
'responses': {
'201': {'description': 'ok'},
}
},
'put': {
'description': 'resource a',
'responses': {
'404': {'description': 'error'},
}
}
}

renderer = renderers.HttpdomainOldRenderer(
None,
{
'methods': ['post'],
'paths': ['/resource_a'],
},
)
text = '\n'.join(renderer.render_restructuredtext_markup(spec))

assert text == textwrap.dedent('''
.. http:post:: /resource_a
:synopsis: null
resource a
:status 201:
ok
''').lstrip()

def test_root_parameters(self):
spec = {'paths': {}}
spec['paths']['/resources/{name}'] = collections.OrderedDict()
Expand Down Expand Up @@ -1487,6 +1529,48 @@ def test_callback(self):
''').lstrip()

def test_method_option(self):
spec = collections.defaultdict(collections.OrderedDict)
spec['paths']['/resource_a'] = {
'get': {
'description': 'resource a',
'responses': {
'200': {'description': 'ok'},
}
},
'post': {
'description': 'resource a',
'responses': {
'201': {'description': 'ok'},
}
},
'put': {
'description': 'resource a',
'responses': {
'404': {'description': 'error'},
}
}
}

renderer = renderers.HttpdomainOldRenderer(
None,
{
'methods': ['post'],
'paths': ['/resource_a'],
},
)
text = '\n'.join(renderer.render_restructuredtext_markup(spec))

assert text == textwrap.dedent('''
.. http:post:: /resource_a
:synopsis: null
resource a
:status 201:
ok
''').lstrip()


class TestResolveRefs(object):

Expand Down

0 comments on commit ac78f12

Please sign in to comment.