Search code examples
python-2.7flask-restplus

Decorating GET method with api.expect() Flask-RESTPlus


I have an endpoint that I decorated with api.expect(), but I am getting this error.

{
    "message": "Input payload validation failed",
    "errors": {
        "": "None is not of type u'object'"
    }
}

This is what my code looks like for simplicity.

@api.route('/history')
class ProfitAnalyticsResource(Resource):

    method_decorators = [jwt_required]

    @api.expect(api.model('HistoryParameters', {
        'jobId': fields.Integer(required=True, description='Job ID')
    }), validate=True)
    def get(self):
        """ Gets the profit analytics report for the current user"""

        return "test only"

I am testing this on Postman, passing in jobId as a url parameter.


Solution

  • Instead of using api.model(), I used api.parser()

    E.g

    @api.route("/list")
    @api.doc(parser=parser)
    class RoleListResource(Resource):
        method_decorators = [jwt_required]
    
        query_parser = api.parser()
        query_parser.add_argument('page', required=False, type=int, default=1)
        query_parser.add_argument('page_size', required=False, type=int, default=10)
    
        @api.expect(query_parser, validate=True)
        def get(self, query_parser=query_parser):
            args = query_parser.parse_args()
            page = args['page']
            page_size = args['page_size']