jmcarp / jmcarp/flask-apispec

Running Quickstart code got KeyError: 'petresource'

Open
#243 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
652
Forks
151
PR merge metrics
No merged PRs in 30d

Description

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
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
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the Quickstart example with the listed package versions and inspect the registration path around FlaskApiSpec and docs.register(PetResource). Compare the resource registration with the URL map referenced in the traceback. Done means the Quickstart runs without the KeyError and the resource endpoint is registered successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.