-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters