jmcarp / jmcarp/flask-apispec

Flask-APIspec and flask-injector with inject decorator doesn't work together for views.

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

Description

I have upgraded my packages to:
flask-2.2.2
injector-0.20.1
Flask-injector-0.14.1
apispec-6.0.2
flask-apispec-0.11.4

I'm using Flask-apispec to auto-generate api documentation, and I'm using injector library. both independently works well in my code but when I use them together, it throws the error missing positional argument.
I tried to debug the code, in stack trace, it skips the inject decorator.

Here is a sample code
```
import marshmallow as ma
from flask_apispec import doc, marshal_with, use_kwargs
from injector import inject
from flask_injector import FlaskInjector
from injector import inject, singleton

def configure(binder):
binder.bind(
str,
to="MyCat",
scope=singleton,
)

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

class PetSchema(ma.Schema):
name = ma.fields.Str()
type = ma.fields.Str()

###

import flask
import flask.views

from flask_apispec import FlaskApiSpec, MethodResource

app = flask.Flask(__name__)
docs = FlaskApiSpec(app)

@doc(
tags=['pets'],
params={'pet_id': {'description': 'the pet name'}},
)
class CatResource(MethodResource):
@inject
def __init__(self, pet_name: str):
self.pet_name = pet_name
super().__init__()
@marshal_with(PetSchema)
def get(self, pet_id):
return Pet(self.pet_name, 'cat')

@marshal_with(PetSchema)
def put(self, pet_id):
return Pet('calici', 'cat')

app.add_url_rule('/cat/', view_func=CatResource.as_view('CatResource'))

# registering view
docs.register(CatResource, endpoint='CatResource')
FlaskInjector(app=app, modules=[configure])

if __name__ == '__main__':
app.run(debug=True)
```
Everything works well if I comment out the "docs.register(CatResource, endpoint='CatResource')"'
Note: Everything was working fine before upgrading libraries to the latest

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the example around CatResource, its inject-decorated __init__, CatResource.as_view, and docs.register(CatResource, endpoint='CatResource'). Compare behavior with and without registration and inspect the decorator and view-registration entry points used by flask-apispec. Done means the sample works with both FlaskInjector and documentation registration, with a regression test covering the integration.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend, documentation
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.