Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running Quickstart code got KeyError: 'petresource' #243

Open
khadkarajesh opened this issue Oct 28, 2022 · 0 comments
Open

Running Quickstart code got KeyError: 'petresource' #243

khadkarajesh opened this issue Oct 28, 2022 · 0 comments

Comments

@khadkarajesh
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant