flask-restx
flask-restx copied to clipboard
Input validation does not happen when using RequestParser
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.
+1
+1
+1
+1
I second that, and api.payload is not populated either. So you can only get access with the line args = parser.parse_args()
i got the error too, any alternative to fix it?
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