Skip to content

Commit

Permalink
Merge 69a0171 into 37ee864
Browse files Browse the repository at this point in the history
  • Loading branch information
jkcdarunday committed Dec 19, 2018
2 parents 37ee864 + 69a0171 commit 84e6000
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
68 changes: 68 additions & 0 deletions examples/callbacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
The simple example using declared definitions, and external static js/css.
"""

from flask import Flask, jsonify

from flasgger import Swagger

app = Flask(__name__)
app.config['SWAGGER'] = {
'title': 'OA3 Callbacks'
}
swagger_config = Swagger.DEFAULT_CONFIG
swagger_config['swagger_ui_bundle_js'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js'
swagger_config['swagger_ui_standalone_preset_js'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui-standalone-preset.js'
swagger_config['jquery_js'] = '//unpkg.com/jquery@2.2.4/dist/jquery.min.js'
swagger_config['swagger_ui_css'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui.css'
Swagger(app, config=swagger_config)

@app.route('/run_callback/', methods=['POST'])
def run_callback():
"""Example endpoint that specifies OA3 callbacks
This is using docstring for specifications
---
tags:
- callbacks
requestBody:
description: Test
required: true
content:
application/json:
schema:
properties:
callback_url:
type: string
format: uri
description: Callback URL for request
callbacks:
onSomeEvent:
'{$request.body.callback_url}':
post:
requestBody:
description: status payload
content:
application/json:
schema:
properties:
status:
type: string
"""

return jsonify({'result': 'ok'})


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 'callbacks' in spec['paths']['/run_callback/']['post']
assert 'onSomeEvent' in spec['paths']['/run_callback/']['post']['callbacks']


if __name__ == "__main__":
app.run(debug=True)
15 changes: 15 additions & 0 deletions flasgger/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,19 @@ def get_apispecs(self, endpoint='apispec_1'):
prefix_ids=prefix_ids
)

callbacks = swag.get("callbacks", {})
if callbacks:
callbacks = {
str(key): value
for key, value in callbacks.items()
}
extract_definitions(
list(callbacks.values()),
endpoint=rule.endpoint,
verb=verb,
prefix_ids=prefix_ids
)

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

0 comments on commit 84e6000

Please sign in to comment.