apispec-webframeworks icon indicating copy to clipboard operation
apispec-webframeworks copied to clipboard

Flask DocumentedBlueprint: Add to spec all views defined in the blueprint.

Open JavierLuna opened this issue 5 years ago • 9 comments

Flask DocumentBlueprint

Is a blueprint which adds all views defined in it to the spec. You can ommit one view from being added to the spec setting documented=False in the Blueprint's route function (see usage below).

Usage


from flask import Flask
from flask.views import MethodView

app = Flask(__name__)

documented_blueprint = DocumentedBlueprint('gistapi', __name__)

@documented_blueprint.route('/gists/<gist_id>')
def gist_detail(gist_id):
    '''Gist detail view.
    ---
    x-extension: metadata
    get:
        responses:
            200:
                schema:
                    $ref: '#/definitions/Gist'
    '''
    return 'detail for gist {}'.format(gist_id)


@documented_blueprint.route('/repos/<repo_id>', documented=False)
def repo_detail(repo_id):
    '''This endpoint won't be documented
    ---
    x-extension: metadata
    get:
        responses:
            200:
                schema:
                    $ref: '#/definitions/Repo'
    '''
    return 'detail for repo {}'.format(repo_id)

app.register_blueprint(documented_blueprint)

print(spec.to_dict()['paths'])
# {'/gists/{gist_id}': {'get': {'responses': {200: {'schema': {'$ref': '#/definitions/Gist'}}}},
#                  'x-extension': 'metadata'}}

Has a test coverage of 100%.

JavierLuna avatar Jan 12 '19 10:01 JavierLuna