|
| 1 | +flask-swagger |
| 2 | +============= |
| 3 | + |
| 4 | +A swagger 2.0 spec extractor for flask |
| 5 | + |
| 6 | +Install: |
| 7 | + |
| 8 | +:: |
| 9 | + |
| 10 | + pip install flask-swagger |
| 11 | + |
| 12 | +flask-swagger provides a method (swagger) that inspects the flask app |
| 13 | +for endpoints that contain YAML docstrings with swagger 2.0 |
| 14 | +`Operation <https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#operation-object>`__ |
| 15 | +objects. |
| 16 | + |
| 17 | +:: |
| 18 | + |
| 19 | + class UserAPI(MethodView): |
| 20 | + |
| 21 | + def post(self): |
| 22 | + """ |
| 23 | + Create a new user |
| 24 | + --- |
| 25 | + tags: |
| 26 | + - users |
| 27 | + parameters: |
| 28 | + - in: body |
| 29 | + name: body |
| 30 | + schema: |
| 31 | + id: User |
| 32 | + required: |
| 33 | + - email |
| 34 | + - name |
| 35 | + properties: |
| 36 | + email: |
| 37 | + type: string |
| 38 | + description: email for user |
| 39 | + name: |
| 40 | + type: string |
| 41 | + description: name for user |
| 42 | + responses: |
| 43 | + 201: |
| 44 | + description: User created |
| 45 | + """ |
| 46 | + return {} |
| 47 | + |
| 48 | +flask-swagger supports docstrings in MethodView classes and regular |
| 49 | +flask view functions. |
| 50 | + |
| 51 | +Following YAML conventions, flask-swagger searches for ``---``, |
| 52 | +everything preceding is provided as ``summary`` (first line) and |
| 53 | +``description`` (following lines) for the endpoint while everything |
| 54 | +after is parsed as a swagger |
| 55 | +`Operation <https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#operation-object>`__ |
| 56 | +object. |
| 57 | + |
| 58 | +In order to support inline definition of |
| 59 | +`Schema <https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#schemaObject>`__ |
| 60 | +objects in |
| 61 | +`Operation <https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#operation-object>`__ |
| 62 | +and |
| 63 | +`Response <https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#responsesObject>`__ |
| 64 | +items, flask-swagger veers a little off from the standard. We require an |
| 65 | +``id`` field for the inline Schema which is then used to correctly place |
| 66 | +the |
| 67 | +`Schema <https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#schemaObject>`__ |
| 68 | +object in the |
| 69 | +`Definitions <https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#definitionsObject>`__ |
| 70 | +object. |
| 71 | + |
| 72 | +To expose your swagger specification to the world you provide a flask |
| 73 | +route that does something along these lines |
| 74 | + |
| 75 | +:: |
| 76 | + |
| 77 | + from flask import Flask, jsonify |
| 78 | + from flask_swagger import swagger |
| 79 | + |
| 80 | + app = Flask(__name__) |
| 81 | + |
| 82 | + @app.route("/spec") |
| 83 | + def spec(): |
| 84 | + return jsonify(swagger(app)) |
| 85 | + |
| 86 | +Note that the swagger specification returned by ``swagger(app)`` is as |
| 87 | +minimal as it can be. It's your job to override and add to the |
| 88 | +specification as you see fit. |
| 89 | + |
| 90 | +:: |
| 91 | + |
| 92 | + @app.route("/spec") |
| 93 | + def spec(): |
| 94 | + swag = swagger(app) |
| 95 | + swag['info']['version'] = "1.0" |
| 96 | + swag['info']['title'] = "My API" |
| 97 | + return jsonify(swag) |
| 98 | + |
| 99 | +`Swagger-UI <https://github.com/swagger-api/swagger-ui>`__ |
| 100 | + |
| 101 | +Swagger-UI is the reason we embarked on this mission to begin with, |
| 102 | +flask-swagger does not however include Swagger-UI. Simply follow the |
| 103 | +awesome documentation over at https://github.com/swagger-api/swagger-ui |
| 104 | +and point your |
| 105 | +`swaggerUi.url <https://github.com/swagger-api/swagger-ui#swaggerui>`__ |
| 106 | +to your new flask-swagger endpoint and enjoy. |
| 107 | + |
| 108 | +Acknowledgments |
| 109 | + |
| 110 | +flask-swagger builds on ideas and code from |
| 111 | +`flask-sillywalk <https://github.com/hobbeswalsh/flask-sillywalk>`__ and |
| 112 | +`flask-restful-swagger <https://github.com/rantav/flask-restful-swagger>`__ |
0 commit comments