Flask,棉花糖3和webargs use_args无法解析参数 [英] Flask, Marshmallow 3, and webargs use_args fails to parse arguments

查看:187
本文介绍了Flask,棉花糖3和webargs use_args无法解析参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Flask 1.1.2,棉花糖3.6.1和webargs 6.1.0,我所有的参数总是 missing .

With Flask 1.1.2, marshmallow 3.6.1 and webargs 6.1.0 all of my arguments are always 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:

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 值.

When I curl any value for limit or offset it always uses the default value.

curl http://localhost:8000/exports?limit = 5930

日志:"qparams {'limit':10,'offset':0}"}

我希望引发 ValidationError ,因为限制应大于100.

I expect a ValidationError to be raised because the limit should be > 100.

当我卷曲一个未知参数时,我希望引发一个 ValidationError ,因为它是一个未知参数.这也无法按预期工作.

When I curl an unknown parameter I expect a ValidationError to be raised because it's an unknown parameter. This also does not work as expected.

curl http://localhost:8000/exports?lkfjdskl = fkjdsl

返回200,并且没有 qparams .

returns a 200 and has no qparams.

在组合 webargs Flask 棉花糖时,我在做什么错了?

What am I doing wrong here in combining webargs, Flask, and marshmallow?

推荐答案

逻辑在webargs 6中已更改.

The logic changed in webargs 6.

在webargs 6之前,解析器将迭代架构的字段,并且默认情况下在多个位置搜索以找到该值.

Before webargs 6, the parser would iterate over the fields of the schema and, by default, search over multiple locations to find the value.

在webargs 6中,解析器仅将数据从单个位置传递到架构.该位置默认为"json" .

In webargs 6, the parser just passes the data from a single location to the schema. The location defaults to "json".

由于您使用的是查询参数,因此需要明确指定:

Since you're using query arguments, you need to specify it explicitly:

@use_args(ExportSearchSchema(unknown=RAISE), location="query")

由于您未指定位置,因此假设使用json正文,但未找到任何内容,并且使用默认值.

Since you don't specify the location, json body is assumed, nothing is found, and the default values are used.

这在webargs文档中有记录:升级到6.0" .

This is documented in webargs docs: "upgrading to 6.0".

这篇关于Flask,棉花糖3和webargs use_args无法解析参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆