Code Monkey home page Code Monkey logo

flasgger's Introduction

Flasgger

Easy Swagger UI for your Flask API

Build Status Code Health Coverage Status PyPI Donate with Paypal

flasgger

Flasgger is a Flask extension to extract OpenAPI-Specification from all Flask views registered in your API.

Flasgger also comes with SwaggerUI embedded so you can access http://localhost:5000/apidocs and visualize and interact with your API resources.

Flasgger also provides validation of the incoming data, using the same specification it can validates if the data received as a POST, PUT, PATCH is valid against the schema defined using YAML, Python dictionaries or Marshmallow Schemas.

Flasgger can work with simple function views or MethodViews using docstring as specification, or using @swag_from decorator to get specification from YAML or dict and also provides SwaggerView which can use Marshmallow Schemas as specification.

Flasgger is compatible with Flask-RESTful so you can use Resources and swag specifications together, take a look at restful example.

Flasgger also supports Marshmallow APISpec as base template for specification, if you are using APISPec from Marshmallow take a look at apispec example.

Table of Contents

Created by gh-md-toc

Top Contributors

Examples and demo app

There are some example applications and you can also play with examples in Flasgger demo app

NOTE: all the examples apps are also test cases and run automatically in Travis CI to ensure quality and coverage.

Docker

The examples and demo app can also be built and run as a Docker image/container:

docker build -t flasgger .
docker run -it --rm -p 5000:5000 --name flasgger flasgger

Then access the Flasgger demo app at http://localhost:5000 .

Installation

under your virtualenv do:

Ensure you have latest setuptools

pip install -U setuptools

then install beta version (recommended)

pip install flasgger==0.9.7b2

or (latest stable for legacy apps)

pip install flasgger==0.9.5

or (dev version)

pip install https://github.com/flasgger/flasgger/tarball/master

NOTE: If you want to use Marshmallow Schemas you also need to run pip install marshmallow apispec

How to run tests

(You may see the command in .travis.yml for before_install part)

In your virtualenv:

pip install -r requirements.txt
pip install -r requirements-dev.txt
make test

Getting started

Using docstrings as specification

Create a file called for example colors.py

from flask import Flask, jsonify
from flasgger import Swagger

app = Flask(__name__)
swagger = Swagger(app)

@app.route('/colors/<palette>/')
def colors(palette):
    """Example endpoint returning a list of colors by palette
    This is using docstrings for specifications.
    ---
    parameters:
      - name: palette
        in: path
        type: string
        enum: ['all', 'rgb', 'cmyk']
        required: true
        default: all
    definitions:
      Palette:
        type: object
        properties:
          palette_name:
            type: array
            items:
              $ref: '#/definitions/Color'
      Color:
        type: string
    responses:
      200:
        description: A list of colors (may be filtered by palette)
        schema:
          $ref: '#/definitions/Palette'
        examples:
          rgb: ['red', 'green', 'blue']
    """
    all_colors = {
        'cmyk': ['cyan', 'magenta', 'yellow', 'black'],
        'rgb': ['red', 'green', 'blue']
    }
    if palette == 'all':
        result = all_colors
    else:
        result = {palette: all_colors.get(palette)}

    return jsonify(result)

app.run(debug=True)

Now run:

python colors.py

And go to: http://localhost:5000/apidocs/

You should get:

colors

Using external YAML files

Save a new file colors.yml

Example endpoint returning a list of colors by palette
In this example the specification is taken from external YAML file
---
parameters:
  - name: palette
    in: path
    type: string
    enum: ['all', 'rgb', 'cmyk']
    required: true
    default: all
definitions:
  Palette:
    type: object
    properties:
      palette_name:
        type: array
        items:
          $ref: '#/definitions/Color'
  Color:
    type: string
responses:
  200:
    description: A list of colors (may be filtered by palette)
    schema:
      $ref: '#/definitions/Palette'
    examples:
      rgb: ['red', 'green', 'blue']

lets use the same example changing only the view function.

from flasgger import swag_from

@app.route('/colors/<palette>/')
@swag_from('colors.yml')
def colors(palette):
    ...

If you do not want to use the decorator you can use the docstring file: shortcut.

@app.route('/colors/<palette>/')
def colors(palette):
    """
    file: colors.yml
    """
    ...

Using dictionaries as raw specs

Create a Python dictionary as:

specs_dict = {
  "parameters": [
    {
      "name": "palette",
      "in": "path",
      "type": "string",
      "enum": [
        "all",
        "rgb",
        "cmyk"
      ],
      "required": "true",
      "default": "all"
    }
  ],
  "definitions": {
    "Palette": {
      "type": "object",
      "properties": {
        "palette_name": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Color"
          }
        }
      }
    },
    "Color": {
      "type": "string"
    }
  },
  "responses": {
    "200": {
      "description": "A list of colors (may be filtered by palette)",
      "schema": {
        "$ref": "#/definitions/Palette"
      },
      "examples": {
        "rgb": [
          "red",
          "green",
          "blue"
        ]
      }
    }
  }
}

Now take the same function and use the dict in the place of YAML file.

@app.route('/colors/<palette>/')
@swag_from(specs_dict)
def colors(palette):
    """Example endpoint returning a list of colors by palette
    In this example the specification is taken from specs_dict
    """
    ...

Using Marshmallow Schemas

FIRST: pip install marshmallow apispec

USAGE #1: SwaggerView

from flask import Flask, jsonify
from flasgger import Swagger, SwaggerView, Schema, fields


class Color(Schema):
    name = fields.Str()

class Palette(Schema):
    pallete_name = fields.Str()
    colors = fields.Nested(Color, many=True)

class PaletteView(SwaggerView):
    parameters = [
        {
            "name": "palette",
            "in": "path",
            "type": "string",
            "enum": ["all", "rgb", "cmyk"],
            "required": True,
            "default": "all"
        }
    ]
    responses = {
        200: {
            "description": "A list of colors (may be filtered by palette)",
            "schema": Palette
        }
    }

    def get(self, palette):
        """
        Colors API using schema
        This example is using marshmallow schemas
        """
        all_colors = {
            'cmyk': ['cyan', 'magenta', 'yellow', 'black'],
            'rgb': ['red', 'green', 'blue']
        }
        if palette == 'all':
            result = all_colors
        else:
            result = {palette: all_colors.get(palette)}
        return jsonify(result)

app = Flask(__name__)
swagger = Swagger(app)

app.add_url_rule(
    '/colors/<palette>',
    view_func=PaletteView.as_view('colors'),
    methods=['GET']
)

app.run(debug=True)

USAGE #2: Custom Schema from flasgger

  • Body - support all fields in marshmallow
  • Query - support simple fields in marshmallow (Int, String and etc)
  • Path - support only int and str
from flask import Flask, abort
from flasgger import Swagger, Schema, fields
from marshmallow.validate import Length, OneOf

app = Flask(__name__)
Swagger(app)

swag = {"swag": True,
        "tags": ["demo"],
        "responses": {200: {"description": "Success request"},
                      400: {"description": "Validation error"}}}


class Body(Schema):
    color = fields.List(fields.String(), required=True, validate=Length(max=5), example=["white", "blue", "red"])

    def swag_validation_function(self, data, main_def):
        self.load(data)

    def swag_validation_error_handler(self, err, data, main_def):
        abort(400, err)


class Query(Schema):
    color = fields.String(required=True, validate=OneOf(["white", "blue", "red"]))

    def swag_validation_function(self, data, main_def):
        self.load(data)

    def swag_validation_error_handler(self, err, data, main_def):
        abort(400, err)

    swag_in = "query"


@app.route("/color/<id>/<name>", methods=["POST"], **swag)
def index(body: Body, query: Query, id: int, name: str):
    return {"body": body, "query": query, "id": id, "name": name}

if __name__ == "__main__":
    app.run(debug=True)

NOTE: take a look at examples/validation.py for a more complete example.

NOTE: when catching arguments in path rule always use explicit types, bad: /api/<username> good: /api/<string:username>

Using Flask RESTful Resources

Flasgger is compatible with Flask-RESTful you only need to install pip install flask-restful and then:

from flask import Flask
from flasgger import Swagger
from flask_restful import Api, Resource

app = Flask(__name__)
api = Api(app)
swagger = Swagger(app)

class Username(Resource):
    def get(self, username):
        """
        This examples uses FlaskRESTful Resource
        It works also with swag_from, schemas and spec_dict
        ---
        parameters:
          - in: path
            name: username
            type: string
            required: true
        responses:
          200:
            description: A single user item
            schema:
              id: User
              properties:
                username:
                  type: string
                  description: The name of the user
                  default: Steven Wilson
        """
        return {'username': username}, 200


api.add_resource(Username, '/username/<username>')

app.run(debug=True)

Auto-parsing external YAML docs and MethodViews

Flasgger can be configured to auto-parse external YAML API docs. Set a doc_dir in your app.config['SWAGGER'] and Swagger will load API docs by looking in doc_dir for YAML files stored by endpoint-name and method-name. For example, 'doc_dir': './examples/docs/' and a file ./examples/docs/items/get.yml will provide a Swagger doc for ItemsView method get.

Additionally, when using Flask RESTful per above, by passing parse=True when constructing Swagger, Flasgger will use flask_restful.reqparse.RequestParser, locate all MethodViews and parsed and validated data will be stored in flask.request.parsed_data.

Handling multiple http methods and routes for a single function

You can separate specifications by endpoint or methods

from flasgger.utils import swag_from

@app.route('/api/<string:username>', endpoint='with_user_name', methods=['PUT', 'GET'])
@app.route('/api/', endpoint='without_user_name')
@swag_from('path/to/external_file.yml', endpoint='with_user_name')
@swag_from('path/to/external_file_no_user_get.yml', endpoint='without_user_name', methods=['GET'])
@swag_from('path/to/external_file_no_user_put.yml', endpoint='without_user_name', methods=['PUT'])
def fromfile_decorated(username=None):
    if not username:
        return "No user!"
    return jsonify({'username': username})

And the same can be achieved with multiple methods in a MethodView or SwaggerView by registering the url_rule many times. Take a look at examples/example_app

Use the same data to validate your API POST body.

Setting swag_from's validation parameter to True will validate incoming data automatically:

from flasgger import swag_from

@swag_from('defs.yml', validation=True)
def post():
    # if not validate returns ValidationError response with status 400
    # also returns the validation message.

Using swagger.validate annotation is also possible:

from flasgger import Swagger

swagger = Swagger(app)

@swagger.validate('UserSchema')
def post():
    '''
    file: defs.yml
    '''
    # if not validate returns ValidationError response with status 400
    # also returns the validation message.

Yet you can call validate manually:

from flasgger import swag_from, validate

@swag_from('defs.yml')
def post():
    validate(request.json, 'UserSchema', 'defs.yml')
    # if not validate returns ValidationError response with status 400
    # also returns the validation message.

It is also possible to define validation=True in SwaggerView and also use specs_dict for validation.

Take a look at examples/validation.py for more information.

All validation options can be found at http://json-schema.org/latest/json-schema-validation.html

Custom validation

By default Flasgger will use python-jsonschema to perform validation.

Custom validation functions are supported as long as they meet the requirements:

  • take two, and only two, positional arguments:
    • the data to be validated as the first; and
    • the schema to validate against as the second argument
  • raise any kind of exception when validation fails.

Any return value is discarded.

Providing the function to the Swagger instance will make it the default:

from flasgger import Swagger

swagger = Swagger(app, validation_function=my_validation_function)

Providing the function as parameter of swag_from or swagger.validate annotations or directly to the validate function will force it's use over the default validation function for Swagger:

from flasgger import swag_from

@swag_from('spec.yml', validation=True, validation_function=my_function)
...
from flasgger import Swagger

swagger = Swagger(app)

@swagger.validate('Pet', validation_function=my_function)
...
from flasgger import validate

...

    validate(
        request.json, 'Pet', 'defs.yml', validation_function=my_function)

Validation Error handling

By default Flasgger will handle validation errors by aborting the request with a 400 BAD REQUEST response with the error message.

A custom validation error handling function can be provided to supersede default behavior as long as it meets the requirements:

  • take three, and only three, positional arguments:
    • the error raised as the first;
    • the data which failed validation as the second; and
    • the schema used in to validate as the third argument

Providing the function to the Swagger instance will make it the default:

from flasgger import Swagger

swagger = Swagger(app, validation_error_handler=my_handler)

Providing the function as parameter of swag_from or swagger.validate annotations or directly to the validate function will force it's use over the default validation function for Swagger:

from flasgger import swag_from

@swag_from(
    'spec.yml', validation=True, validation_error_handler=my_handler)
...
from flasgger import Swagger

swagger = Swagger(app)

@swagger.validate('Pet', validation_error_handler=my_handler)
...
from flasgger import validate

...

    validate(
        request.json, 'Pet', 'defs.yml',
        validation_error_handler=my_handler)

Examples of use of a custom validation error handler function can be found at example validation_error_handler.py

Get defined schemas as python dictionaries

You may wish to use schemas you defined in your Swagger specs as dictionaries without replicating the specification. For that you can use the get_schema method:

from flask import Flask, jsonify
from flasgger import Swagger, swag_from

app = Flask(__name__)
swagger = Swagger(app)

@swagger.validate('Product')
def post():
    """
    post endpoint
    ---
    tags:
      - products
    parameters:
      - name: body
        in: body
        required: true
        schema:
          id: Product
          required:
            - name
          properties:
            name:
              type: string
              description: The product's name.
              default: "Guarana"
    responses:
      200:
        description: The product inserted in the database
        schema:
          $ref: '#/definitions/Product'
    """
    rv = db.insert(request.json)
    return jsonify(rv)

...

product_schema = swagger.get_schema('product')

This method returns a dictionary which contains the Flasgger schema id, all defined parameters and a list of required parameters.

HTML sanitizer

By default Flasgger will try to sanitize the content in YAML definitions replacing every \n with <br> but you can change this behaviour setting another kind of sanitizer.

from flasgger import Swagger, NO_SANITIZER

app =Flask()
swagger = Swagger(app, sanitizer=NO_SANITIZER)

You can write your own sanitizer

swagger = Swagger(app, sanitizer=lambda text: do_anything_with(text))

There is also a Markdown parser available, if you want to be able to render Markdown in your specs description use MK_SANITIZER

Swagger UI and templates

You can override the templates/flasgger/index.html in your application and this template will be the index.html for SwaggerUI. Use flasgger/ui2/templates/index.html as base for your customization.

Flasgger supports Swagger UI versions 2 and 3, The version 3 is still experimental but you can try setting app.config['SWAGGER']['uiversion'].

app = Flask(__name__)
app.config['SWAGGER'] = {
    'title': 'My API',
    'uiversion': 3
}
swagger = Swagger(app)

OpenAPI 3.0 Support

There is experimental support for OpenAPI 3.0 that should work when using SwaggerUI 3. To use OpenAPI 3.0, set app.config['SWAGGER']['openapi'] to a version that the current SwaggerUI 3 supports such as '3.0.2'.

For an example of this that uses callbacks and requestBody, see the callbacks example.

Externally loading Swagger UI and jQuery JS/CSS

Starting with Flasgger 0.9.2 you can specify external URL locations for loading the JavaScript and CSS for the Swagger and jQuery libraries loaded in the Flasgger default templates. If the configuration properties below are omitted, Flasgger will serve static versions it includes - these versions may be older than the current Swagger UI v2 or v3 releases.

The following example loads Swagger UI and jQuery versions from unpkg.com:

swagger_config = Swagger.DEFAULT_CONFIG
swagger_config['swagger_ui_bundle_js'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js'
swagger_config['swagger_ui_standalone_preset_js'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui-standalone-preset.js'
swagger_config['jquery_js'] = '//unpkg.com/[email protected]/dist/jquery.min.js'
swagger_config['swagger_ui_css'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui.css'
Swagger(app, config=swagger_config)

Initializing Flasgger with default data.

You can start your Swagger spec with any default data providing a template:

template = {
  "swagger": "2.0",
  "info": {
    "title": "My API",
    "description": "API for my data",
    "contact": {
      "responsibleOrganization": "ME",
      "responsibleDeveloper": "Me",
      "email": "[email protected]",
      "url": "www.me.com",
    },
    "termsOfService": "http://me.com/terms",
    "version": "0.0.1"
  },
  "host": "mysite.com",  # overrides localhost:500
  "basePath": "/api",  # base bash for blueprint registration
  "schemes": [
    "http",
    "https"
  ],
  "operationId": "getmyData"
}

swagger = Swagger(app, template=template)

And then the template is the default data unless some view changes it. You can also provide all your specs as template and have no views. Or views in external APP.

Getting default data at runtime

Sometimes you need to get some data at runtime depending on dynamic values ex: you want to check request.is_secure to decide if schemes will be https you can do that by using LazyString.

from flask import Flask
from flasgger import, Swagger, LazyString, LazyJSONEncoder

app = Flask(__init__)

# Set the custom Encoder (Inherit it if you need to customize)
app.json_encoder = LazyJSONEncoder


template = dict(
    info={
        'title': LazyString(lambda: 'Lazy Title'),
        'version': LazyString(lambda: '99.9.9'),
        'description': LazyString(lambda: 'Hello Lazy World'),
        'termsOfService': LazyString(lambda: '/there_is_no_tos')
    },
    host=LazyString(lambda: request.host),
    schemes=[LazyString(lambda: 'https' if request.is_secure else 'http')],
    foo=LazyString(lambda: "Bar")
)
Swagger(app, template=template)

The LazyString values will be evaluated only when jsonify encodes the value at runtime, so you have access to Flask request, session, g, etc.. and also may want to access a database.

Behind a reverse proxy

Sometimes you're serving your swagger docs behind an reverse proxy (e.g. NGINX). When following the Flask guidance, the swagger docs will load correctly, but the "Try it Out" button points to the wrong place. This can be fixed with the following code:

from flask import Flask, request
from flasgger import Swagger, LazyString, LazyJSONEncoder

app = Flask(__name__)
app.json_encoder = LazyJSONEncoder

template = dict(swaggerUiPrefix=LazyString(lambda : request.environ.get('HTTP_X_SCRIPT_NAME', '')))
swagger = Swagger(app, template=template)

Customize default configurations

Custom configurations such as a different specs route or disabling Swagger UI can be provided to Flasgger:

swagger_config = {
    "headers": [
    ],
    "specs": [
        {
            "endpoint": 'apispec_1',
            "route": '/apispec_1.json',
            "rule_filter": lambda rule: True,  # all in
            "model_filter": lambda tag: True,  # all in
        }
    ],
    "static_url_path": "/flasgger_static",
    # "static_folder": "static",  # must be set by user
    "swagger_ui": True,
    "specs_route": "/apidocs/"
}

swagger = Swagger(app, config=swagger_config)

Extracting Definitions

Definitions can be extracted when id is found in spec, example:

from flask import Flask, jsonify
from flasgger import Swagger

app = Flask(__name__)
swagger = Swagger(app)

@app.route('/colors/<palette>/')
def colors(palette):
    """Example endpoint returning a list of colors by palette
    ---
    parameters:
      - name: palette
        in: path
        type: string
        enum: ['all', 'rgb', 'cmyk']
        required: true
        default: all
    responses:
      200:
        description: A list of colors (may be filtered by palette)
        schema:
          id: Palette
          type: object
          properties:
            palette_name:
              type: array
              items:
                schema:
                  id: Color
                  type: string
        examples:
          rgb: ['red', 'green', 'blue']
    """
    all_colors = {
        'cmyk': ['cyan', 'magenta', 'yellow', 'black'],
        'rgb': ['red', 'green', 'blue']
    }
    if palette == 'all':
        result = all_colors
    else:
        result = {palette: all_colors.get(palette)}

    return jsonify(result)

app.run(debug=True)

In this example you do not have to pass definitions but need to add id to your schemas.

Python2 Compatibility

Version 0.9.5.* will be the last version that supports Python2. Please direct discussions to #399.

flasgger's People

Contributors

aminhp avatar anilpai avatar arthurio avatar atlithorn avatar belkka avatar bhr724 avatar billyrrr avatar brentthew avatar ced42 avatar cesarferradas avatar daym avatar decentral1se avatar javabrett avatar jkcdarunday avatar joeystevens00 avatar lounarok avatar markchoward7 avatar microstudi avatar mtronrd avatar ngr-t avatar nikoladsp avatar rochacbruno avatar roo-oliv avatar samizdis avatar strongbugman avatar talitarossari avatar tobixx avatar vasekch avatar vshih avatar zrayn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flasgger's Issues

customize apidocs/index.html

this is awesome! It is working and wonder can we overwrite the apidocs/index.html.

Is that possible you add some doc on how / where to put the swaggerui to customize the css template etc?

thanks in advance

Authorization Header + UI input view

Anyone can help me with how to implement custom headers e.g. "Authorization header" in requests + adding UI element for it with flasgger?
I need to use JWT with some of the endpoints.
I was able to achieve this by modifying flasgger source code, but that shouldn't be the way!

Add Travis testing for examples

Would you be interested? I'd try to set it up, we could just figure out a way to fire up each examples Flask server and check that it runs and then kill it. This would give guarantees the examples are working.

AttributeError: 'function' object has no attribute 'swag_path'

When doing something like:

@op_blueprint.route('/auth', methods=['GET', 'POST'])
@swag_from('docs/auth_get.yml', methods=['GET'])
@swag_from('docs/auth_post.yml', methods=['POST'])
def auth():
    """The authorization endpoint."""
    # do stuff ...

I see:

  File "/home/lwm/wages/fsfe/oidcp/.venv/lib/python3.5/site-packages/flask/cli.py", line 231, in load_app
    rv = locate_app(self.app_import_path)
  File "/home/lwm/wages/fsfe/oidcp/.venv/lib/python3.5/site-packages/flask/cli.py", line 90, in locate_app
    __import__(module)
  File "/home/lwm/wages/fsfe/oidcp/wsgi.py", line 7, in <module>
    from oidcp.app import create_app
  File "/home/lwm/wages/fsfe/oidcp/oidcp/app.py", line 22, in <module>
    from oidcp.views import op_blueprint
  File "/home/lwm/wages/fsfe/oidcp/oidcp/views.py", line 30, in <module>
    @swag_from('../swagger_docs/auth/auth_post.yml', methods=['POST'])
  File "/home/lwm/wages/fsfe/oidcp/.venv/lib/python3.5/site-packages/flasgger/utils.py", line 71, in decorator
    'filepath': function.swag_path,
AttributeError: 'function' object has no attribute 'swag_path'

Where the function has the swag_paths attribute (from my digging around).

[RESOLVED] Definitions cause infinite loop loading Swagger UI

Hi folks, great work. When I include a "definition" in my swagger spec, which otherwise works, I get this error:

127.0.0.1 - - [20/Mar/2017 22:00:17] "GET /specs HTTP/1.1" 200 -
127.0.0.1 - - [20/Mar/2017 22:00:17] "GET /v1/spec HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flask/views.py", line 149, in dispatch_request
    return meth(*args, **kwargs)
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flasgger/base.py", line 267, in get
    verb=verb)
  File "/Users/scox/dev/exposures/venv/lib/python3.6/site-packages/flasgger/base.py", line 123, in _extract_definitions
    schema = item.get("schema")
AttributeError: 'str' object has no attribute 'get'

I'm using an external yaml file for a single operation so I could see how a definition might not be in the correct context for that file. So I looked for an example using definitions in this repo but didn't see any. What am I doing wrong?

conversion of swagger document format

Hi Bruno,
First of all thanks for this awesome library :)
I can see that the format flasgger requires is slightly different from the one used by swagger.
That works fine, but in order to test my apis through tools like soapui i need the swagger format. Is there a converter available i can use , files that are created or do i need to use my own?

cheers.

cache the specs

@swag_from('filename.yml', cache='redis|local|memcached', ttl=3000)

Create APISpec example

Using the example code from: http://apispec.readthedocs.io/en/latest/

from apispec import APISpec
from flask import Flask, jsonify
from marshmallow import Schema, fields

# Create an APISpec
spec = APISpec(
    title='Swagger Petstore',
    version='1.0.0',
    plugins=[
        'apispec.ext.flask',
        'apispec.ext.marshmallow',
    ],
)

# Optional marshmallow support
class CategorySchema(Schema):
    id = fields.Int()
    name = fields.Str(required=True)

class PetSchema(Schema):
    category = fields.Nested(CategorySchema, many=True)
    name = fields.Str()

# Optional Flask support
app = Flask(__name__)

@app.route('/random')
def random_pet():
    """A cute furry animal endpoint.
    ---
    get:
        description: Get a random pet
        responses:
            200:
                description: A pet to be returned
                schema: PetSchema
    """
    pet = get_random_pet()
    return jsonify(PetSchema().dump(pet).data)

ctx = app.test_request_context()
ctx.push()

# Register entities and paths
spec.definition('Category', schema=CategorySchema)
spec.definition('Pet', schema=PetSchema)
spec.add_path(view=random_pet)

should be possible to use Flasgger as:

Swagger(app, template=spec.to_dict())

and with the above flasgger will generate the /apidocs using the apispec.

Create test for regex path extractor

All the following

api/v1/setup/<id_setup>/<int:id_status>
/api/v1/setup/<id_setup>/<id_status>
/api/v1/setup/<id_setup>/status/<id_status>

Should parse correctly on the test
create a test using all werkzeug path types

Add support for method based path declarations in docstring.

REF: https://github.com/gangverk/flask-swagger/pull/32/files

If found get, post, put, patch, delete, option, head.. swaggify based in those paths.

@app.route('/user', methods=["GET", "PUT"])
def user():
    """
    get:
        Get a user
        Use this endpoint to get a user object
        ---
        tags:
            - User API
        definitions:
          - schema:
                id: User
                properties:
                    name:
                        type: string
                        description: the user's name
                    age:
                        type: integer
                        description: the user's age
        responses:
            200:
                description: user obtained
                schema:
                    $ref: "#/definitions/User"
    put:
        Update a user
        Use this endpoint to update a user
        ---
        tags:
            - User API
        parameters:
            - name: name
              in: formData
              type: string
              required: true
            - name: age
              in: formData
              type: integer
              required: true
        responses:
            202:
                description: user updated
                schema:
                    $ref: "#/definitions/User"
    """
    pass

IMPORTANT: Keep compatibility with current examples/*

Unable to create global definitions

Due to the use of 'id' for schemas $ref to a defined schema doesn't work.

When running example application 'example_app.py' you can see that the GET method (get /v1/users/{team_id}) using $ref to reference #/definitions/User returns ' [ null ]'

With the current implementation the reference should be done like this:
$ref: '#/definitions/should_be_v1_only_post_post_User'

but that seems to be odd if you would like to define "global" objects that can be used by more than one method.

Index url path

I can see the documentation if I type in /apidocs/index.html but it doesn't load if I only type in /apidocs. Shouldn't it load from both?

I looked down into the blueprint setup but I couldn't see how to get this to work.

Fix regex to add missing type to rules

Currently rules only work if type is defined

bad

@route('/<param>/<another>')

good

@route('/<string:param>/<string:another>')

But we should accept the first, so regex have to be fixed.

remove the large .js from egg

Could we remove the 2.4MB swagger-ui.js from the swaggerui folder in the egg? the .min should be sufficient. This will make the egg 80% smaller. that would be awesome. Thanks

Hardcoded optional_fields

Optional fields for operation definition are hardcoded.

So, if you want to add some non-standard fields (I want to generate swagger definition for Amazon API Gateway, so I have to use their extensions to swagger) you have to patch it, using inheritance or monkey-patching.

It is very inconvenient. I suggest to allow overwriting this list in config or just allow using any optional fields without any filtering (maybe by adding to config "strict"/"non-strict" switcher, where "non-strict" will mean that any optional fields are allowed).

New release

Could you please publish a new release? I'm waiting to python 3 support.
Thank you.

AttributeError: 'function' object has no attribute 'root_path'

Issue introduced between 0.5.14 and 0.6.1:

Traceback (most recent call last):
  File "/usr/local/bin/cci", line 7, in <module>
    from cci.main import start
  File "/usr/local/lib/python2.7/dist-packages/cci/main.py", line 13, in <module>
    from api import app
  File "/usr/local/lib/python2.7/dist-packages/cci/api/__init__.py", line 33, in <module>
    import views  # noqa
  File "/usr/local/lib/python2.7/dist-packages/cci/api/views.py", line 80, in <module>
    @swag_from(swag_file('api_versions.yml'))
  File "/usr/local/lib/python2.7/dist-packages/flasgger/utils.py", line 72, in decorator
    'root': function.root_path
AttributeError: 'function' object has no attribute 'root_path'

Our code:

def swag_file(yml):
    return pkg_resources.resource_filename('cci', os.path.join("api/swag", yml))

@app.route('/api/versions/', methods=['GET'])
@swag_from(swag_file('api_versions.yml'))
def get_version():
   // ...

swaggerUI 404

I am getting the api spec in /v1/spec but swagger UI at /apidocs/index.html returns 404. what could be the reason for this. I am using both Resource based and MethodView based apis in flask

Why docsstring in head method is not working in flasgger ?

Docstring in triple quotes is not working in the head method. I am using the package flassger. I am not able to use docstring in head method for swagger ui. However, it is working in patch, post, put, and get methods.

@app.route('/flight/<flight_no>', methods=['HEAD'])
def get_flight_exist(flight_no):
    """
        show Flight Existence
        This resource returns flight exist response
        ---
        tags:
          - hello
        parameters:
        - name: flight_no
          in: path
          type: string
          description: Flight_no
          required: true


        responses:
            '200':
              description: Flight data  response
              schema:
                 description: Flight object
                 properties:
                   flight_name:
                     type: string
                     description: name of the flight
                   flight_no:
                     type: string
                     description: flight number
                   total_seat:
                     type: integer
                 required:
                   - flight_name
                   - flight_no
                   - total_seat

            '404':
              description: Flight not found

        """

    flight_data = mongo.db.flight_details
    info = flight_data.find_one({'flight_no': flight_no})
    if info:
        if request.headers['Accept'] == 'application/json':
            flight_exist_response = make_response()
            flight_exist_response.status_code = 200
            flight_exist_response.mimetype = 'application/json'
            return flight_exist_response

        else:
            flight_not_exist_response = make_response()
            flight_not_exist_response.status_code = 404
            flight_not_exist_response.mimetype = 'application/json'
            return flight_not_exist_response

Handling multiple http methods and routes for a single function.

How does flasgger handle multiple http methods in a single route? It seems that there is no way to have two different sets of documentation for view function that has a GET and POST. Also I have view functions that have multiple routes such as /events and events/:id. How can I produce two separate definition objects?

Create test for MethodView docstrings

When the user creates resources that are objects derived from flask.views.MethodView the requests are handled in methods named after the HTTP method, like get and post. A test should be added to check that the docstring of these methods is correctly retrieved. This case is important for Flask_restful users.

Allow a custom specs url for Swagger UI

My project has a standalone Flask server behind nginx at '/api'. I cannot register a specs endpoint at '/specs', only '/api/specs'. Would it make sense to use app.config['SWAGGER']['basePath'] to prepend 'specs_url' in SwaggerUI/index.html? I can edit this myself, but will lose changes when updating this package.

Static url path change

Thanks for the great work it works fine!

One thing I noticed when I tried to change the static url path from apidocs to for instance 'sampleUrl' by adding my own DEFAULT_CONFIG object to the constructor everything worked great. But due to a typo I noticed that in the index.html there is still a fixed path reference to /apidocs in this redirect function

function redirect_to(url){
54 window.location.href = '/apidocs/index.html?url=' + url;

Maybe this is something to fix in a next version?

Getting empty results with example_app

Hi there,

using the example_app which is provided with this project, Im getting a empty result for the "paths" object of the specification. So the UI is empty for those routes.

Anyone else with this problem?

Cheers,
Christian

Import another yaml file

Hello!
Thanks for this useful library.
I want to import another yaml file in my swagger specification. For example i have a file named username_definitions.yml. Now i want to import bad_request.yml to specify a "bad request" response.

How can i do something like that?
If there is no way, will it good idea to implement an import property myself?

username_definitions.yml

This is the summary defined in yaml file
First line is the summary
All following lines until the hyphens is added to description
the format of the first lines until 3 hyphens will be not yaml compliant
but everything below the 3 hyphens should be.
---
tags:
  - users
parameters:
  - in: path
    name: username
    type: string
    required: true
responses:
  200:
    description: A single user item
    schema:
      id: rec_username
      properties:
        username:
          type: string
          description: The name of the user
          default: 'steve-harris'

  import: "bad_request.yml" 

bad_request.yml

400:
  description: Bad request

Flasgger miss insert `<br>` elements whem we format html elements inside yml description.

Flasgger miss insert <br> elements whem we format html elements inside yml description.

Compare:

def post(self):
    """
    Some text

    <table><tr><th>**Title 1**</th><th>**Title 2**</th></tr><tr><td>field 1</td><td>desc</td></tr><tr><td>field 2</td><td>desc descdesc desc.</td></tr><tr><td>field 3</td><td>bhahahhahaha</td></tr><tr><td>field 4</td><td>put your code here kkk.</td></tr></table>
    """
    pass

with:

def post(self):
    """
    Some text

    <table>
        <tr>
            <th>**Title 1**</th>
            <th>**Title 2**</th>
        </tr>
        <tr>
            <td>field 1</td>
            <td>desc</td>
        </tr>
        <tr>
            <td>field 2</td>
            <td>desc descdesc desc.</td>
        </tr>
        <tr>
            <td>field 3</td>
            <td>bhahahhahaha</td>
        </tr>
        <tr>
            <td>field 4</td>
            <td>put your code here kkk.</td>
        </tr>
    </table>
    """
    pass

note that the second snippet insert many <br> elements between 'Some text' and the <table> content.

How to fold Flasgger API listings

Would be cool to join the /api/auth/ GET/POST under the one /api/auth.

I've seen this doable in django-rest-swagger libraries. Is it possible in flasgger?

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.