| 1 | """ |
| 2 | Example of swagger document auto loading by endpoint |
| 3 | name(view function/method name), parsed by |
| 4 | flask_restful.reqparse.RequestParser and validated by jsonschema |
| 5 | """ |
| 6 | import os |
| 7 | |
| 8 | from flask import Flask, request, jsonify, Response |
| 9 | from flask.views import MethodView |
| 10 | from flasgger import Swagger |
| 11 | |
| 12 | |
| 13 | app = Flask(__name__) |
| 14 | app.config['SWAGGER'] = { |
| 15 | 'title': 'Flasgger Parsed Method/Function View Example', |
| 16 | 'doc_dir': './examples/docs/' |
| 17 | } |
| 18 | swag = Swagger( |
| 19 | app, |
| 20 | template_file=os.path.join( |
| 21 | os.getcwd(), 'examples', 'docs', 'template.yml'), |
| 22 | parse=True) |
| 23 | |
| 24 | |
| 25 | class ItemsView(MethodView): |
| 26 | """ |
| 27 | Flasgger will try to load "./examples/docs/items/{method}.yml" as |
| 28 | swagger document |
| 29 | """ |
| 30 | def get(self): |
| 31 | """ |
| 32 | If we set "parse" is True in Flasgger app, we will get parsed and |
| 33 | validated data stored in "flask.request.parsed_data". |
| 34 | |
| 35 | In "parsed_data", different location's var stored in different key, |
| 36 | there is a map between RequestParser's location and swagger |
| 37 | doc's "in" parameter, eg: 'query' -> 'args'.See |
| 38 | "Swagger.SCHEMA_LOCATIONS" for more locations |
| 39 | """ |
| 40 | return jsonify( |
| 41 | [{'name': 'test', 'id': 1, |
| 42 | 'type': request.parsed_data['args']['type']}, |
| 43 | {'name': 'test2', 'id': 2, |
| 44 | 'type': request.parsed_data['args']['type']}]) |
| 45 | |
| 46 | def post(self): |
| 47 | return jsonify( |
| 48 | {'name': request.parsed_data['json']['name'], 'id': 3, |
| 49 | 'type': request.parsed_data['json']['type']}) |
| 50 | |
| 51 | |
| 52 | class ItemMethodView(MethodView): |
| 53 | def get(self, id): |
| 54 | return jsonify({'name': 'test', 'id': id, 'type': 'NORMAL'}) |
| 55 | |
| 56 | def put(self, id): |
| 57 | return jsonify( |
| 58 | {'name': request.parsed_data['json']['name'], |
| 59 | 'id': 3, 'type': 'NORMAL'}) |
| 60 | |
| 61 | |
| 62 | class EmptyView(MethodView): |
| 63 | """In this view, we do not provide api doc""" |
| 64 | def get(self): |
| 65 | return Response(status=200) |
| 66 | |
| 67 | |
| 68 | @app.route('/api/users/<group>/', methods=['POST']) |
| 69 | def users(group): |
| 70 | """Create one user with nested json body. |
| 71 | --- |
| 72 | tags: |
| 73 | - FunctionView |
| 74 | parameters: |
| 75 | - name: group |
| 76 | in: path |
| 77 | type: string |
| 78 | required: true |
| 79 | - name: User |
| 80 | in: body |
| 81 | schema: |
| 82 | type: object |
| 83 | required: |
| 84 | - data |
| 85 | properties: |
| 86 | data: |
| 87 | type: object |
| 88 | required: |
| 89 | - name |
| 90 | - age |
| 91 | properties: |
| 92 | age: |
| 93 | type: integer |
| 94 | name: |
| 95 | type: string |
| 96 | tags: |
| 97 | type: array |
| 98 | minItems: 1 |
| 99 | items: |
| 100 | type: integer |
| 101 | definitions: |
| 102 | User: |
| 103 | type: object |
| 104 | properties: |
| 105 | id: |
| 106 | type: integer |
| 107 | name: |
| 108 | type: string |
| 109 | group: |
| 110 | type: string |
| 111 | age: |
| 112 | type: integer |
| 113 | responses: |
| 114 | 200: |
| 115 | description: ok |
| 116 | schema: |
| 117 | $ref: '#/definitions/User' |
| 118 | examples: |
| 119 | {'id': 1, 'name': 'test', 'group': 1, 'age': 20} |
| 120 | 400: |
| 121 | description: Miss data |
| 122 | """ |
| 123 | return jsonify( |
| 124 | {'id': 1, |
| 125 | 'name': request.parsed_data['json']['data']['name'], |
| 126 | 'age': request.parsed_data['json']['data']['age'], |
| 127 | 'group': request.parsed_data['path']['group']}) |
| 128 | |
| 129 | |
| 130 | @app.route('/api/user/') |
| 131 | def user(): |
| 132 | """Flasgger will try to load './examples/docs/user.yml' as swagger doc |
| 133 | """ |
| 134 | return jsonify({'id': request.parsed_data['args']['id'], 'name': 'test'}) |
| 135 | |
| 136 | |
| 137 | app.add_url_rule( |
| 138 | '/api/items/', view_func=ItemsView.as_view(name='items'), |
| 139 | methods=['GET', 'POST']) |
| 140 | app.add_url_rule( |
| 141 | '/api/items/<int:id>/', view_func=ItemMethodView.as_view(name='item'), |
| 142 | methods=['GET', 'PUT']) |
| 143 | app.add_url_rule( |
| 144 | '/api/empty/', view_func=EmptyView.as_view(name='empty'), |
| 145 | methods=['GET']) |
| 146 | |
| 147 | |
| 148 | def test_swag(client, specs_data): |
| 149 | """ |
| 150 | This test is runs automatically in Travis CI |
| 151 | |
| 152 | :param client: Flask app test client |
| 153 | :param specs_data: {'url': {swag_specs}} for every spec in app |
| 154 | """ |
| 155 | res = client.get('/api/items/') |
| 156 | assert res.status_code == 400 |
| 157 | res = client.get('/api/items/?type=myLengthIsTooLong') |
| 158 | assert res.status_code == 400 |
| 159 | res = client.get('/api/items/?type=NORMAL') |
| 160 | |
| 161 | assert res.status_code == 200 |
| 162 | res = client.get('/api/items/1/') |
| 163 | assert res.status_code == 200 |
| 164 | res = client.put('/api/items/1/') |
| 165 | if res.status_code == 400: |
| 166 | pass |
| 167 | elif res.status_code == 415: |
| 168 | import logging |
| 169 | logging.warning('new flask versions will return 415 error instead') |
| 170 | else: |
| 171 | raise AssertionError(f'unexpected status code {res.status_code}') |
| 172 | res = client.put('/api/items/1/', json={'name': 'test'}) |
| 173 | assert res.status_code == 200 |
| 174 | |
| 175 | res = client.post('/api/users/1/', json={'name': 'miss data'}) |
| 176 | assert res.status_code == 400 |
| 177 | res = client.post( |
| 178 | '/api/users/1/', |
| 179 | json={'data': {'name': 'test', 'age': 20}, 'tags': ['error_tag']}) |
| 180 | assert res.status_code == 400 |
| 181 | res = client.post( |
| 182 | '/api/users/1/', |
| 183 | json={'data': {'name': 'test', 'age': 20}, 'tags': [1, 2]}) |
| 184 | assert res.status_code == 200 |
| 185 | |
| 186 | |
| 187 | if __name__ == '__main__': |
| 188 | app.run(debug=True) |