flask-restx icon indicating copy to clipboard operation
flask-restx copied to clipboard

Input validation does not happen when using RequestParser

Open vincent-heatseekr opened this issue 5 years ago • 7 comments

According to the documentation at https://flask-restx.readthedocs.io/en/latest/swagger.html#the-api-expect-decorator "you can use RequestParser to define the expected input". However, it seems that validation of parameters does not happen when using a RequestParser, even when validation=True is set in the @api.expect decorator, unless parser.parse_args is called within the method handler itself.

Full "Working" Code

from flask import Flask
from flask_restx import Resource, Api

app = Flask(__name__)
api = Api(app)

parser = api.parser()
parser.add_argument('param', type=int, required=True)

@api.route('/<string:bar>')
class FooBar(Resource):
    @api.expect(parser, validate=True)
    def get(self, bar):
        # args = parser.parse_args(strict=True)
        return bar

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

Expected Behavior

When the above code is run and the url http://localhost:5000/abc is visited the expected response is:

{"errors": {"param": "Missing required parameter in the JSON body or the post body or the query string"}, "message": "Input payload validation failed"}

Actual Behavior

Actual result from http://localhost:5000/abc is:

"abc"

Further information

Without the commented-out line of code as shown in the example above, validation does not happen when using a RequestParser to define the expected input.

vincent-heatseekr avatar May 15 '20 01:05 vincent-heatseekr

+1

Clickative avatar Aug 24 '20 19:08 Clickative

+1

iamnavpreet avatar Sep 18 '20 15:09 iamnavpreet

+1

makegofast avatar Feb 09 '21 02:02 makegofast

+1

hashirirfantk avatar Sep 22 '21 11:09 hashirirfantk

I second that, and api.payload is not populated either. So you can only get access with the line args = parser.parse_args()

fmerges avatar Nov 06 '21 15:11 fmerges

i got the error too, any alternative to fix it?

perymerdeka avatar Jul 07 '22 19:07 perymerdeka

not populated either. So you can only get access with the line args = parser.parse_args()

how to populate it?, I got this error after

perymerdeka avatar Jul 08 '22 09:07 perymerdeka