Skip to content

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

Open
@pratik261199

Description

@pratik261199

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/<pet_id>', 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions