yourapp.marshal_with(model)必须跟紧youapp.doc()才会在swagger页面上展示model
先看这段代码:
@api.doc('GetList')
@api.param('changenumber', '变更单号')
@api.param('mindate', '起始日期')
@api.param('maxdate', '截至日期')
@api.response(ARGUMENTS_ERROR, 'need at least one argument')
@api.response(HTTP_NOT_FOUND, 'no record found')
@api.response(HTTP_GET_SUCCESS, 'query success')
@api.marshal_with(list_model)
def get(self):
pass
有没有发现什么异常?
咋一看,该有的都有,很全面呀,可是在swagger页面上:

嗯?marshal_with定义的model呢?被天狗吃掉啦???
后来对比了下官方DEMO,发现doc和marshal_with()或者expect()要放到一起写才行:
@api.doc('Get_Plan_List')
@api.marshal_with(record_list_model)
@api.param('changenumber', '变更单号')
@api.param('mindate', '起始日期')
@api.param('maxdate', '截至日期')
@api.response(ARGUMENTS_ERROR, 'need at least one argument')
@api.response(HTTP_NOT_FOUND, 'no record found')
@api.response(HTTP_GET_SUCCESS, 'query success')
def get(self):
pass
渲染出来的swagger页面:

没啥好说,拧耳朵,按规矩办!
本文解决了一个在使用Flask-RESTPlus框架时,Swagger页面无法正确显示model的问题。关键在于确保marshal_with()或expect()紧跟在doc()装饰器之后,以确保参数和返回类型能被正确解析并展示。
834

被折叠的 条评论
为什么被折叠?



