| 1 | """ |
| 2 | A test to ensure routes from Blueprints are swagged as expected. |
| 3 | """ |
| 4 | from flask import Blueprint, Flask, jsonify |
| 5 | |
| 6 | from flasgger import Swagger |
| 7 | from flasgger.utils import swag_from |
| 8 | |
| 9 | app = Flask(__name__) |
| 10 | |
| 11 | example_blueprint = Blueprint("example_blueprint", __name__) |
| 12 | |
| 13 | |
| 14 | @example_blueprint.route('/usernames/<username>', methods=['GET', 'POST']) |
| 15 | @swag_from('username_specs.yml', methods=['GET']) |
| 16 | @swag_from('username_specs.yml', methods=['POST']) |
| 17 | def usernames(username): |
| 18 | return jsonify({'username': username}) |
| 19 | |
| 20 | |
| 21 | @example_blueprint.route('/usernames2/<username>', methods=['GET', 'POST']) |
| 22 | def usernames2(username): |
| 23 | """ |
| 24 | This is the summary defined in yaml file |
| 25 | First line is the summary |
| 26 | All following lines until the hyphens is added to description |
| 27 | the format of the first lines until 3 hyphens will be not yaml compliant |
| 28 | but everything below the 3 hyphens should be. |
| 29 | --- |
| 30 | tags: |
| 31 | - users |
| 32 | parameters: |
| 33 | - in: path |
| 34 | name: username |
| 35 | type: string |
| 36 | required: true |
| 37 | responses: |
| 38 | 200: |
| 39 | description: A single user item |
| 40 | schema: |
| 41 | id: rec_username |
| 42 | properties: |
| 43 | username: |
| 44 | type: string |
| 45 | description: The name of the user |
| 46 | default: 'steve-harris' |
| 47 | """ |
| 48 | return jsonify({'username': username}) |
| 49 | |
| 50 | |
| 51 | @example_blueprint.route('/users', endpoint='user-without-id', methods=['GET']) |
| 52 | @example_blueprint.route('/users/<user_id>', endpoint='user-with-id', methods=['GET']) |
| 53 | @swag_from('user_with_id_specs.yml', endpoint='example_blueprint.user-with-id', methods=['GET']) |
| 54 | @swag_from('user_without_id_specs.yml', endpoint='example_blueprint.user-without-id', methods=['GET']) |
| 55 | def usernames(user_id=None): |
| 56 | if user_id: |
| 57 | return jsonify({'user_id': user_id}) |
| 58 | else: |
| 59 | return jsonify([]) |
| 60 | |
| 61 | |
| 62 | app.register_blueprint(example_blueprint) |
| 63 | |
| 64 | swag = Swagger(app) |
| 65 | |
| 66 | if __name__ == "__main__": |
| 67 | app.run(debug=True) |
| 68 |