Skip to content

Commit

Permalink
Merge pull request #191 from zrayn/add-requestBody-support
Browse files Browse the repository at this point in the history
Adding support for requestBody
  • Loading branch information
rochacbruno committed Apr 25, 2018
2 parents dc41202 + abc9672 commit 54f5e40
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 3 deletions.
78 changes: 78 additions & 0 deletions examples/request_body.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
In this example `openapi` version is used instead of `swagger` version.
"""
from flask import Flask, jsonify, request
from flasgger import Swagger, swag_from

app = Flask(__name__)
swag = Swagger(app, config={
'headers': [],
'specs': [
{
'endpoint': 'apispec',
'route': '/apispec.json'
}
],
'openapi': '3.0.1'
})


@swag.definition('Pet')
class Pet(object):
"""
Pet Object
---
properties:
name:
type: string
"""
def __init__(self, name):
self.name = str(name)

def dump(self):
return dict(vars(self).items())


@app.route('/requestBody', methods=['POST'])
@swag_from()
def request_body_endpoint():
"""
An endpoint for testing requestBody documentation.
---
description: Post a request body
requestBody:
content:
application/json:
schema:
$ref: '#/definitions/Pet'
required: true
responses:
200:
description: The posted request body
content:
application/json:
schema:
$ref: '#/definitions/Pet'
"""
return jsonify(request.json)


def test_swag(client, specs_data):
"""
This test is runs automatically in Travis CI
:param client: Flask app test client
:param specs_data: {'url': {swag_specs}} for every spec in app
"""
for url, spec in specs_data.items():
assert 'Pet' in spec['definitions']

assert 'paths' in spec
paths = spec['paths']
for path_def in paths.values():
for method_def in path_def.values():
assert 'requestBody' in method_def


if __name__ == '__main__':
app.run(debug=True)
34 changes: 34 additions & 0 deletions examples/use_openapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
In this example `openapi` version is used instead of `swagger` version.
"""
from flask import Flask
from flasgger import Swagger

app = Flask(__name__)
swag = Swagger(app, config={
'headers': [],
'specs': [
{
'endpoint': 'apispec',
'route': '/apispec.json'
}
],
'openapi': '3.0.1'
})


def test_swag(client, specs_data):
"""
This test is runs automatically in Travis CI
:param client: Flask app test client
:param specs_data: {'url': {swag_specs}} for every spec in app
"""
for spec in specs_data.values():
assert 'openapi' in spec
assert '3.0.1' == spec['openapi']
assert 'swagger' not in spec


if __name__ == '__main__':
app.run(debug=True)
23 changes: 20 additions & 3 deletions flasgger/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ def get(self):
The Swagger view get method outputs to /apispecs_1.json
"""
data = {
"swagger": self.config.get('swagger') or self.config.get(
'swagger_version', "2.0"
),
# try to get from config['SWAGGER']['info']
# then config['SWAGGER']['specs'][x]
# then config['SWAGGER']
Expand All @@ -145,6 +142,14 @@ def get(self):
"definitions": self.config.get('definitions') or defaultdict(dict)
}

openapi_version = self.config.get('openapi')
if openapi_version:
data["openapi"] = openapi_version
else:
data["swagger"] = self.config.get('swagger') or self.config.get(
'swagger_version', "2.0"
)

# Support extension properties in the top level config
top_level_extension_options = get_vendor_extension_fields(self.config)
if top_level_extension_options:
Expand Down Expand Up @@ -210,6 +215,16 @@ def get(self):
verb=verb,
prefix_ids=prefix_ids)

request_body = swag.get('requestBody')
if request_body:
content = request_body.get("content", {})
extract_definitions(
list(content.values()),
endpoint=rule.endpoint,
verb=verb,
prefix_ids=prefix_ids
)

responses = None
if 'responses' in swag:
responses = swag.get('responses', {})
Expand Down Expand Up @@ -237,6 +252,8 @@ def get(self):
operation['summary'] = swag.get('summary')
if swag.get('description'):
operation['description'] = swag.get('description')
if request_body:
operation['requestBody'] = request_body
if responses:
operation['responses'] = responses
# parameters - swagger ui dislikes empty parameter lists
Expand Down

0 comments on commit 54f5e40

Please sign in to comment.