Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group all samples after the description of parameters and status code #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions sphinxcontrib/openapi/openapi30.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,31 +261,22 @@ def _httpresource(endpoint, method, properties, render_examples):
name=param['name'])
for line in param.get('description', '').splitlines():
yield '{indent}{indent}{line}'.format(**locals())

# print request example
if render_examples:
request_content = properties.get('requestBody', {}).get('content', {})
for line in _example(
request_content, method, endpoint=endpoint, nb_indent=1):
yield line
if param.get('required', False):
yield '{indent}{indent}(Required)'.format(**locals())

# print response status codes
for status, response in responses.items():
yield '{indent}:status {status}:'.format(**locals())
for line in response['description'].splitlines():
yield '{indent}{indent}{line}'.format(**locals())

# print response example
if render_examples:
for line in _example(
response.get('content', {}), status=status, nb_indent=2):
yield line

# print request header params
for param in filter(lambda p: p['in'] == 'header', parameters):
yield indent + ':reqheader {name}:'.format(**param)
for line in param.get('description', '').splitlines():
yield '{indent}{indent}{line}'.format(**locals())
if param.get('required', False):
yield '{indent}{indent}(Required)'.format(**locals())

# print response headers
for status, response in responses.items():
Expand All @@ -294,6 +285,19 @@ def _httpresource(endpoint, method, properties, render_examples):
for line in header['description'].splitlines():
yield '{indent}{indent}{line}'.format(**locals())

if render_examples:
# print request example
request_content = properties.get('requestBody', {}).get('content', {})
for line in _example(
request_content, method, endpoint=endpoint, nb_indent=1):
yield line

# print response example
for status, response in responses.items():
for line in _example(
response.get('content', {}), status=status, nb_indent=1):
yield line

yield ''


Expand Down
88 changes: 44 additions & 44 deletions tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,24 +659,24 @@ def test_example_generation(self):
Show up to `limit` entries.
:status 200:
An array of resources.
:reqheader If-None-Match:
Last known resource ETag.

**Example response:**
**Example response:**

.. sourcecode:: http
.. sourcecode:: http

HTTP/1.1 200 OK
Content-Type: application/json
HTTP/1.1 200 OK
Content-Type: application/json

[
{
"kind": "string",
"description": "string",
"data": "c3RyaW5n"
}
]
[
{
"kind": "string",
"description": "string",
"data": "c3RyaW5n"
}
]

:reqheader If-None-Match:
Last known resource ETag.

.. http:post:: /resources/
:synopsis: Create Resource
Expand All @@ -685,6 +685,8 @@ def test_example_generation(self):

~ some useful description ~

:status 200:
The created resource.

**Example request:**

Expand All @@ -699,21 +701,19 @@ def test_example_generation(self):
"data": "c3RyaW5n"
}

:status 200:
The created resource.

**Example response:**
**Example response:**

.. sourcecode:: http
.. sourcecode:: http

HTTP/1.1 200 OK
Content-Type: application/json
HTTP/1.1 200 OK
Content-Type: application/json

{
"kind": "string",
"description": "string",
"data": "c3RyaW5n"
}
{
"kind": "string",
"description": "string",
"data": "c3RyaW5n"
}


.. http:get:: /resources/{kind}
Expand All @@ -728,18 +728,18 @@ def test_example_generation(self):
:status 200:
The created resource.

**Example response:**
**Example response:**

.. sourcecode:: http
.. sourcecode:: http

HTTP/1.1 200 OK
Content-Type: application/json
HTTP/1.1 200 OK
Content-Type: application/json

{
"kind": "string",
"description": "string",
"data": "c3RyaW5n"
}
{
"kind": "string",
"description": "string",
"data": "c3RyaW5n"
}


.. http:patch:: /resources/{kind}
Expand All @@ -751,6 +751,8 @@ def test_example_generation(self):

:param string kind:
Kind of resource to list.
:status 200:
The created resource.

**Example request:**

Expand All @@ -765,21 +767,19 @@ def test_example_generation(self):
"data": "c3RyaW5n"
}

:status 200:
The created resource.

**Example response:**
**Example response:**

.. sourcecode:: http
.. sourcecode:: http

HTTP/1.1 200 OK
Content-Type: application/json
HTTP/1.1 200 OK
Content-Type: application/json

{
"kind": "string",
"description": "string",
"data": "c3RyaW5n"
}
{
"kind": "string",
"description": "string",
"data": "c3RyaW5n"
}

''').lstrip()

Expand Down