代码之家  ›  专栏  ›  技术社区  ›  alejomarchan

Python在Swagger web服务响应中放置转义符号

  •  0
  • alejomarchan  · 技术社区  · 6 年前

    我有一个存储过程Oracle,它返回一个变量类型CLOB,其中包含JSON格式的信息。她被一条蟒蛇抓住了 我把它换成网络服务。存储过程输出的样式如下:

    {"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\"}"}
    

    enter image description here

    我的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
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Oluwafemi Sule    6 年前

    Attribute 定义为渲染为 fields.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.
        }
    })