jmcarp / flask-apispec

MIT License
655 stars 156 forks source link

Running Quickstart code got KeyError: 'petresource' #243

Open khadkarajesh opened 2 years ago

khadkarajesh commented 2 years ago

Running Quickstart guide I got the following stack trace

Traceback (most recent call last):
  File "/Users/mac_user/Library/Application Support/JetBrains/PyCharm2022.2/scratches/api_spec_sample.py", line 58, in <module>
    docs.register(PetResource)
  File "/Users/mac_user/PycharmProjects/web-app/venv/lib/python3.9/site-packages/flask_apispec/extension.py", line 127, in register
    self._defer(self._register, target, endpoint, blueprint,
  File "/Users/mac_user/PycharmProjects/web-app/venv/lib/python3.9/site-packages/flask_apispec/extension.py", line 71, in _defer
    bound()
  File "/Users/mac_user/PycharmProjects/web-app/venv/lib/python3.9/site-packages/flask_apispec/extension.py", line 147, in _register
    paths = self.resource_converter.convert(
  File "/Users/mac_user/PycharmProjects/web-app/venv/lib/python3.9/site-packages/flask_apispec/apidoc.py", line 38, in convert
    rules = self.app.url_map._rules_by_endpoint[endpoint]
KeyError: 'petresource'

Code

from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from flask import Flask, make_response
from flask_apispec import use_kwargs, marshal_with, MethodResource, FlaskApiSpec
from marshmallow import Schema

class Pet:
    def __init__(self, name, type):
        self.name = name
        self.type = type

app = Flask(__name__)

class PetSchema(Schema):
    class Meta:
        fields = ('name', 'category', 'size')

class PetResource(MethodResource):

    @marshal_with(PetSchema)
    def get(self, pet_id):
        return Pet.query.filter(Pet.id == pet_id).one()

    @use_kwargs(PetSchema)
    @marshal_with(PetSchema, code=201)
    def post(self, **kwargs):
        return Pet(**kwargs)

    @use_kwargs(PetSchema)
    @marshal_with(PetSchema)
    def put(self, pet_id, **kwargs):
        pet = Pet.query.filter(Pet.id == pet_id).one()
        pet.__dict__.update(**kwargs)
        return pet

    @marshal_with(None, code=204)
    def delete(self, pet_id):
        pet = Pet.query.filter(Pet.id == pet_id).one()
        pet.delete()
        return make_response('', 204)

app.config.update({
    'APISPEC_SPEC': APISpec(
        openapi_version='2.0',
        title='pets',
        version='v1',
        plugins=[MarshmallowPlugin()],
    ),
    'APISPEC_SWAGGER_URL': '/swagger/',
})

docs = FlaskApiSpec(app)
docs.register(PetResource)

if __name__ == "__main__":
    app.run(port=5000)

Package Version

Flask==2.2.2
flask-apispec==0.11.4
marshmallow ==3.18.0
apispec==6.0.0