[英]how to hide password with ********* on swagger ui with flask_restplus in python
嗨下面是我的运行代码,可以通过以下 URL 访问: http : //127.0.0.1 : 5000/api/documentation
from flask import Flask, Blueprint
from flask_restplus import Api, Resource, fields
app = Flask(__name__)
blueprint = Blueprint('api', __name__, url_prefix='/api')
api = Api(blueprint, doc='/documentation') #,doc=False
app.register_blueprint(blueprint)
app.config['SWAGGER_UI_JSONEDITOR'] = True
login_details = api.model('LoginModel',{'user_name' : fields.String('The Username.'),'pass_word' : fields.String('The password.'),})
# pass_word = api.model('Pwd', {'pass_word' : fields.String('The password.')})
credentials = []
python = {'user_name' : '1234','pwd':'23213413'}
credentials.append(python)
@api.route('/login')
class Language(Resource):
@api.marshal_with(login_details, envelope='the_data',mask='pass_word')
def get(self):
return credentials
@api.expect(login_details)
@api.marshal_with(login_details, envelope='the_data',mask='pass_word')
def post(self):
login_details = api.payload
print(login_details)
login_details['id'] = len(credentials) + 1
credentials.append(login_details)
return {'result' : 'credentials added'}, 201
if __name__ == '__main__':
app.run(debug=True)
当我进入 swagger UI 时,你能告诉我应该怎么做才能用 ***** 隐藏密码,并且值应该正确传递给参数。
根据有关模型的flask-restful文档,您可以在开头看到fields.Raw
类可以采用format
参数:
它可以:
修改现有对象键值的显示方式
因此,您可以将此format
参数与值'password'
,如Swagger 文档中关于“字符串”部分下的 数据类型的记录:
可选的格式修饰符用作字符串内容和格式的提示。 OpenAPI 定义了以下内置字符串格式:
[...]
密码——提示用户界面屏蔽输入
所以你可以像这样在你的字段定义中使用这个format='password'
:
pass_word = fields.String('The password.', format='password')
但问题是您使用的是带有标准Model
定义的expect
装饰器,这不允许您轻松自定义请求解析器。 我建议使用Marshmallow以便能够更好地控制对象序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.