我正在尝试使用flask-restx和棉花糖创建一个restx风格的web服务。
我对请求和响应验证都使用了marshmallow。
因为flask-restx api文档不支持swagger ui中的marshmallow模式,所以我想使用doc修饰器添加它。
控制器代码:
@ns.route('/')
class Test(Resource):
@ns.doc(params={'test': 'test'})
def get(self):
_input_schema = MySchema()
errors = _input_schema.validate(request.args)
if errors:
return Response(str(errors), status=400)
other_things()架构代码:
class MySchema(Schema):
title = fields.Str()
id = fields.Integer()
slug = fields.Str()我正在尝试将模式中的参数自动添加到api文档中,就像这样
@ns.doc(params=MySchema.ReturnAFieldDict())它会给出类似这样的东西
@ns.doc(params={"title":"A string", "id": "Int value with min and max", "slug":"A str"})有没有办法做到这一点?
发布于 2021-11-09 20:26:34
您可以使用api.marshal_with来记录响应
from flask_restx.fields import Integer, String
test_get = api.model(
"TestGet",
{
"name": String(required=True),
"age": Integer(required=True),
},
)
@api.marshal_with(schema.test_get, code=200)
def get(self):
return {"name": "test", "age": 123}marshal_with映射返回的字典或对象,并根据架构映射字段。在这里,在这种情况下,返回的字典将映射到测试模式,其中名称将是“test_get”,年龄将是123。
发布于 2021-09-10 14:46:37
我发现获取文档的一种方法是使用@api.expect(模式)装饰器。
https://stackoverflow.com/questions/69005498
复制相似问题