python - Python 在 Swagger Web 服务响应中放置转义符号
问题描述
我有一个存储过程 Oracle,它返回一个带有 JSON 格式信息的变量类型 CLOB。该变量在 Python 中捕获了她,我将其返回为 Web 服务。存储过程输出是这种风格:
{"role":"Proof_Rol","identification":"31056235002761","class":"Proof_Clase","country":"ARGENTINA","stateOrProvince":"Santa Fe","city":"Rosario","locality":"Rosario","streetName":"Brown","streetNr":"2761","x":"5438468,710153","y":"6356634,962204"}
但在 Python 服务出口处显示如下:
{"Atributos": "{\"role\":\"Proof_Rol\",\"identification\":\"31056235002761\",\"class\":\"Proof_Clase\",\"country\":\"ARGENTINA\",\"stateOrProvince\":\"Santa Fe\",\"city\":\"Rosario\",\"locality\":\"Rosario\",\"streetName\":\"Brown\",\"streetNr\":\"2761\",\"x\":\"5438468,710153\",\"y\":\"6356634,962204\"}"}
有谁知道如何防止转义字符在服务退出时输入该字符串?
我的 Python 代码的一部分是:
api = Api(APP, version='1.0', title='attributes API',
description='Attibute Microservice\n'
'Conection DB:' + db_str + '\n'
'Max try:' + limite)
ns = api.namespace('attributes', description='Show descriptions of an object')
md_respuesta = api.model('attributes', {
'Attribute': fields.String(required=True, description='Attribute List')
})
class listAtriClass:
Attribute = None
@ns.route('/<string:elementId>')
@ns.response(200, 'Success')
@ns.response(404, 'Not found')
@ns.response(429, 'Too many request')
@ns.param('elementId', 'Id Element (ej:31056235002761)')
class attibuteClass(Resource):
@ns.doc('attributes')
@ns.marshal_with(md_respuesta)
def post(self, elementId):
try:
cur = database.db.cursor()
listOutput = cur.var(cx_Oracle.CLOB)
e, l = cur.callproc('attributes.get_attributes', (elementId, listOutput))
except Exception as e:
database.init()
if database.db is not None:
log.err('Reconection OK')
cur = database.db.cursor()
listOutput = cur.var(cx_Oracle.CLOB)
e, l = cur.callproc('attributes.get_attributes', (elementId, listOutput))
print(listOutput)
else:
log.err('Conection Fails')
listOutput = None
result = listAtriClass()
result.Attribute =listOutput.getvalue()
print(result.Attribute)
return result, 200
解决方案
Attribute
被定义为呈现为 afields.String
但实际上它应该被定义为呈现为fields.Nested
。
attribute_fields = {
"role": fields.String,
"identification": fields.String,
"class": fields.String,
# ...you get the idea.
}
md_respuesta = api.model('attributes', {
'Attribute': fields.Nested(attribute_fields)
})
flask-restplus 的更新
在flask-restplus
中,嵌套字段还必须注册模型。
attribute_fields = api.model('fields', {
"role": fields.String,
"identification": fields.String,
"class": fields.String,
# ...you get the idea.
})
另一种方法是内联attribute_fields
而不是为其注册单独的模型。
md_respuesta = api.model('attributes', {
'Attribute': {
'role': fields.String,
'identification': fields.String,
'class': fields.String,
# ...you get the idea.
}
})
推荐阅读
- reactjs - 如果不转到主页,则将应用程序重定向到 404
- angular - 在 *ngIf 中更改变量时组件未加载
- permissions - Laravel 5.8 中 AuthServiceProvider 中基于角色的权限问题 - SQLSTATE[HY000] [2002] 没有这样的文件或目录
- c# - Nuget DLL Hell:无法加载文件或程序集 System.Runtime
- typescript - 如何检查字符串 | 数字在数组中,可能是不同的类型?
- node.js - HandsonTable:无法通过对齐和合并检索数据
- c++ - 如何解决以下问题?致命错误 C1083:无法打开包含文件:'stdlib.h':没有这样的文件或目录
- php - 在 Prestashop 1.7 的结帐中添加字段并处理值
- python - 如何有效且非破坏性地对地理执行着色器分配?
- python - 如何通过对一列中的每一行执行 api 调用来更新 MySQL 数据库列