Flask-RESTful plugin.
Includes a path helper that allows you to pass a Flask-RESTful resource object to add_path.
Inspired by AndrewPashkin/apispec_restful plugin.
pip install apispec_flask_restful
from pprint import pprint
from flask_restful import Api, Resource
from flask import Flask
from apispec import APISpec
import apispec_flask_restful
class HelloResource(Resource):
def get(self, hello_id):
'''A greeting endpoint.
---
description: get a greeting
responses:
200:
description: a pet to be returned
schema:
$ref: #/definitions/Pet
'''
pass
app = Flask(__name__)
api = Api(app)
spec = APISpec(
title='Spec',
version='1.0',
openapi_version='3.0.2',
plugins=[apispec_flask_restful.FlaskRestfulPlugin()]
)
api.add_resource(HelloResource, '/hello')
spec.path(resource=HelloResource, api=api)
pprint(spec.to_dict()['paths'])
# OrderedDict([('/hello',
# {'get': {'description': 'get a greeting',
# 'responses': {200: {'description': 'a pet to be returned',
# 'schema': {'$ref': None}}}}})])
Method add_path can be invoked with a resource path in a path parameter instead of api parameter:
spec.path(resource=HelloResource, path='/hello')
Flask blueprints are supported too by passing Flask app in app parameter:
spec.path(resource=HelloResource, api=api, app=app)