对于Flask 1.1.2,棉花糖3.6.1和webargs 6.1.0,我的所有参数都是missing。
模式:
class ExportSearchSchema(Schema):
limit = fields.Integer(required=False, allow_none=False, default=10, missing=10)
offset = fields.Integer(required=False, allow_none=False, default=0, missing=0)
status = fields.Str(required=False)
class Meta:
unknown = RAISE
@validates('status')
def validate_status(self, value):
if value and value not in ['complete', 'pending', 'failed']:
raise ValidationError('Invalid status: {}'.format(value))
@validates('limit')
def validate_limit(self, value):
if value > 100:
raise ValidationError('Max limit is 100')
if value < 1:
raise ValidationError('Limit must be a positive number and less than 100')
@validates('offset')
def validate_offset(self, value):
if value < 0:
raise ValidationError('Offset must be equal to, or greater than 0')blueprint.py:
from flask import jsonify, Response
from flask import Blueprint
from marshmallow import Schema, fields, validates, ValidationError, RAISE
from webargs.flaskparser import use_args
exports = Blueprint('exports', __name__)
@exports.route('exports/',
methods=['GET'], strict_slashes=False)
@use_args(ExportSearchSchema(unknown=RAISE))
def get_export_list(qparams):
log.info("qparams {}".format(qparams)
response = jsonify({'data': 'export_list'})
response.mimetype = 'application/json'
return response当我为limit或offset卷曲任何值时,它总是使用default值。
curl http://localhost:8000/exports?limit=5930
log: "qparams {'limit': 10, 'offset': 0}"}
我预计ValidationError会提高,因为限制应该大于100。
当我卷曲一个未知参数时,我希望引发一个ValidationError,因为它是一个未知参数。这也不能像预期的那样工作。
curl http://localhost:8000/exports?lkfjdskl=fkjdsl
返回200并且没有qparams。
在这里,我将webargs、Flask和marshmallow组合在一起,到底做错了什么
发布于 2020-06-22 03:10:25
在webargs 6中,逻辑发生了变化。
在webargs 6之前,解析器将迭代模式的字段,并在默认情况下搜索多个位置以查找值。
在webargs 6中,解析器只是将数据从一个位置传递到模式。位置默认为"json"。
由于您使用的是查询参数,因此需要显式指定:
@use_args(ExportSearchSchema(unknown=RAISE), location="query")因为您没有指定位置,所以假设使用json body,什么也找不到,并使用默认值。
在webargs文档:"upgrading to 6.0"中记录了这一点。
https://stackoverflow.com/questions/62480777
复制相似问题