我尝试使用带有fields.String枚举的req解析器来显示swagger中的一个不错的下拉列表:
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=fields.String(enum=['true', 'false', 'all'], default='all')
@seen_api.route('/')
class SeenSearchAPI(APIResource):
@api.response(404, 'Page does not exist')
@api.response(200, 'Successfully retrieved seen objects', seen_search_schema)
@api.doc(parser=seen_search_parser)
def get(self, session):
""" Search for seen entries """
args = seen_search_parser.parse_args()
我使用expect
包装器,我确实很好地看到了这一点。但是当我尝试发送请求时,我收到验证错误。调试时,我从reqparse.convert
方法获得了error: String object is not callable
:
{
"errors": {
"local_seen": "'String' object is not callable"
},
"message": "Input payload validation failed"
}
为什么?我明显在参数下发送一个字符串值。我做错了什么?
答案 0 :(得分:0)
我只需在github issue
上复制粘贴答案 RequestParser
期望来自flask_restplus.inputs
模块的类型。 (例如,参见https://github.com/noirbizarre/flask-restplus/blob/master/flask_restplus/inputs.py#L372-L393)
在你的情况下,你可以自己编写:
def local_seen(value):
if value not in ('true', 'false', 'all'):
raise ValueError('Unexpected value {0}'.format(value))
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=local_seen, default='all',
help='Filter list by local status.')
class MyResource(Resource):
@api.expect(seen_search)
def get(self):
args = seen_search..parse_args()
local_seen = args['local_seen']
if local_seen == 'true':
# true case
elif local_seen == 'false':
# false case
elif local_seen == 'all':
# all case
我的建议,使用布尔值并在未指定时假定为'all':
from flask_restplus.inputs import boolean
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=boolean, help='Filter list by local status.')
class MyResource(Resource):
@api.expect(seen_search)
def get(self):
args = seen_search..parse_args()
local_seen = args['local_seen']
if local_seen is True:
# true case
elif local_seen is False:
# false case
elif local_seen is None:
# all case
答案 1 :(得分:0)
在git问题中回答:
我忘了,但RequestParser也可以处理字符串枚举
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=str, default='all',
choices=('true', 'false', 'all'),
help='Filter list by local status.')
class MyResource(Resource):
@api.expect(seen_search)
def get(self):
args = seen_search..parse_args()
local_seen = args['local_seen']
if local_seen == 'true':
# true case
elif local_seen == 'false':
# false case
elif local_seen == 'all':
# all case