jmcarp / flask-apispec

MIT License
655 stars 157 forks source link

How to use with flask_restful? #187

Open arkady opened 4 years ago

arkady commented 4 years ago

I have a flask_restful-based app and would like to layer flask-apispec on top of it, but I am probably not understanding how to do this correctly.

Current code is something like this:

from flask import Flask
from flask_restful import Api, Resource, reqparse

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

@api.resource('/<string:pet_type>',
              '/<string:pet_type>/pet_id')
class Pets(Resource):
    def get(self, pet_type, pet_id=None):
        if pet_id:
            return get_pet(pet_id)
        else:
            return get_pets(pet_type)

    def post(self, pet_type, pet_id=None):
        p = reqparse.RequestParser()
        p.add_argument('name', required=True)
        args = p.parse_args()
        if pet_id:
            pet = modify_pet(pet_id, name=args.name)
        else:
            pet = create_pet(pet_type, args.name)

        return pet.json()

I am having trouble with getting flask_apispec decorators applied properly to these methods. Is there a "right way" for this?

romaia commented 3 years ago

Had this same doubt and found this blog post that explained it well:

https://medium.com/koko-networks/automated-swagger-api-doc-with-flask-restful-a78119bc4473

Basically, you need to make your Pets Resource also inherit from flask_apispec.views.MethodResource, and later register it with docs.register(Pets)

andresichelero commented 3 years ago

Had this same doubt and found this blog post that explained it well:

https://medium.com/koko-networks/automated-swagger-api-doc-with-flask-restful-a78119bc4473

Basically, you need to make your Pets Resource also inherit from flask_apispec.views.MethodResource, and later register it with docs.register(Pets)

So, I'm doing this(docs.register(MyClass)), but when I register my docs, the following error occurs:

Traceback:
  File "flask-api-template\app\resources\domain_resource.py", line 9, in <module> from app.services import DomainService
  File "flask-api-template\app\__init__.py", line 42, in <module>register_resources(app, docs)
  File "flask-api-template\app\resources\__init__.py", line 11, in register_resources domains.register_module(app, docs)
  File "flask-api-template\app\resources\domain_resource.py", line 55, in register_module docs.register(DomainService)
  File "flask-api-template\venv\lib\site-packages\flask_apispec\extension.py", line 124, in register self._defer(self._register, target, endpoint, blueprint,
  File "flask-api-template\venv\lib\site-packages\flask_apispec\extension.py", line 72, in _defer bound()
  File "flask-api-template\venv\lib\site-packages\flask_apispec\extension.py", line 150, in _register raise TypeError() TypeError
Process finished with exit code 1

I've made several changes and nothing solves it, could you give me any suggestions?

brianantonelli commented 1 year ago

@andresichelero Im seeing the same thing. Did you ever figure this out?

brianantonelli commented 1 year ago

Figured it out for anyone else who runs into this issue. Class has to also inherit from MethodResource:

class Pets(Resource, MethodResource):
  ....