| 1 | """ |
| 2 | In this example `openapi` version is used instead of `swagger` version. |
| 3 | This example will test validation when post |
| 4 | """ |
| 5 | from flask import Flask, jsonify, request |
| 6 | from flasgger import Swagger, swag_from |
| 7 | from flask_restful import Api, Resource |
| 8 | |
| 9 | app = Flask(__name__) |
| 10 | api = Api(app) |
| 11 | swag = Swagger(app, template_file='parse_openapi3_product_schema.yml', |
| 12 | parse=True, config={ |
| 13 | 'headers': [], |
| 14 | 'specs': [ |
| 15 | { |
| 16 | 'endpoint': 'apispec', |
| 17 | 'route': '/apispec.json', |
| 18 | 'test': 'test' |
| 19 | } |
| 20 | ], |
| 21 | 'openapi': '3.0.1' |
| 22 | }) |
| 23 | |
| 24 | class NewProduct(Resource): |
| 25 | def post(self): |
| 26 | pass |
| 27 | |
| 28 | api.add_resource(NewProduct, '/product') |
| 29 | |
| 30 | def test_swag(client, specs_data): |
| 31 | """ |
| 32 | This test is runs automatically in Travis CI |
| 33 | |
| 34 | :param client: Flask app test client |
| 35 | :param specs_data: {'url': {swag_specs}} for every spec in app |
| 36 | """ |
| 37 | |
| 38 | res = client.post('product', json={'id': 0, 'upc': 1}) |
| 39 | assert res.status_code == 200 |
| 40 | |
| 41 | res = client.post('product', json={'id': 0, 'upc': 'astring'}) |
| 42 | assert res.status_code == 400 |
| 43 | |
| 44 | if __name__ == '__main__': |
| 45 | app.run(debug=True) |