简体   繁体   English

当使用 use_kwargs 装饰器时,flask_apispec 库使 Flask 应用程序崩溃

[英]flask_apispec library makes Flask app to crash when use_kwargs decorator is used

I am building a Restful API in Python 3.8 using flask_restful, flask_apispec, and marshmallow.我正在使用flask_restful、flask_apispec 和marshmallow 在Python 3.8 中构建Restful API。 When I enable use_kwargs decorator my app is crushing on a POST with TypeError: post() takes 1 positional argument but 2 were given当我启用use_kwargs装饰器时,我的应用程序在带有TypeError: post() takes 1 positional argument but 2 were given的 POST 上崩溃TypeError: post() takes 1 positional argument but 2 were given

I'd appreciate any help in solving this bug.我很感激在解决这个错误方面的任何帮助。

# requirements.txt
aniso8601==9.0.1
apispec==5.1.1
certifi==2021.10.8
charset-normalizer==2.0.7
click==8.0.3
Flask==2.0.2
flask-apispec==0.11.0
Flask-RESTful==0.3.9
gunicorn==20.1.0
idna==3.3
itsdangerous==2.0.1
Jinja2==3.0.2
MarkupSafe==2.0.1
marshmallow==3.14.0
pytz==2021.3
requests==2.26.0
six==1.16.0
tableauserverclient==0.17.0
urllib3==1.26.7
webargs==8.0.1
Werkzeug==2.0.2

from apispec import APISpec
from flask import Flask, request
from flask_restful import Resource, Api
from apispec.ext.marshmallow import MarshmallowPlugin
from flask_apispec.extension import FlaskApiSpec
from marshmallow import Schema, fields, post_load, ValidationError
from flask_apispec.views import MethodResource
from flask_apispec import use_kwargs, marshal_with

app = Flask(__name__)  # Flask app instance initiated
api = Api(app)  # Flask restful wraps Flask app around it.
app.config.update({
    'APISPEC_SPEC': APISpec(
        title='Kube Controller',
        version='v1',
        plugins=[MarshmallowPlugin()],
        openapi_version='2.0.0'
    ),
    'APISPEC_SWAGGER_URL': '/swagger/',  # URI to access API Doc JSON
    'APISPEC_SWAGGER_UI_URL': '/swagger-ui/'  # URI to access UI of API Doc
})
docs = FlaskApiSpec(app)


class AwesomeRequestSchema(Schema):
    api_type = fields.String(required=True)

    @post_load
    def create(self, data, **kwargs):
        return MyAPI(**data)


class MyAPI:
    def __init__(self, api_type):
        self.api_type = api_type
        self.message = "hi"


class AwesomeAPI(MethodResource, Resource):
    @use_kwargs(AwesomeRequestSchema)
    @marshal_with(AwesomeRequestSchema, code=200, description='Something created')
    def post(self):
        """
        POST
        """
        try:
            schema = AwesomeRequestSchema()
            data = schema.load(request.json)
            print(data.api_type)
            return request.json
        except ValidationError as err:
            return err.messages


api.add_resource(AwesomeAPI, '/')
docs.register(AwesomeAPI)

if __name__ == '__main__':
    app.run(debug=True)

Thanks!谢谢!

I had the same issue.我遇到过同样的问题。 The use_kwargs decorator will try to populate and inject the AwesomeRequestSchema object into the post() method: https://flask-apispec.readthedocs.io/en/latest/api_reference.html#flask_apispec.annotations.use_kwargs use_kwargs 装饰器将尝试填充 AwesomeRequestSchema 对象并将其注入 post() 方法: https ://flask-apispec.readthedocs.io/en/latest/api_reference.html#flask_apispec.annotations.use_kwargs

To fix, replace要修复,请更换

def post(self):

with

def post(self, populated_request_object):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM