【问题标题】:Python Swagger TroublePython 招摇麻烦
【发布时间】:2018-11-12 19:17:55
【问题描述】:

我有一个 oracle 程序,当接收一些参数时,将结果抛出一个 JSON 格式的字符串,如下所示:

{"list_pais": [ {"pais":"URUGUAY"},{"pais":"ARGENTINA"},{"pais":"PARAGUAY"},{"pais":"VENEZUELA"}] }

这个过程是从我用 FlaskResfutl 开发的 Python 中的 web 服务调用的,以创建 swagger,这是解码代码:

import cx_Oracle
import json
from app import APP
import log
import graypy
import database
from flask_restplus import Api, Resource, fields

with open('./config/config_countries.json', 'r') as config_file:
    config = json.load(config_file)

log.init()

#Se inicializa la base de datos
#Importando de database la conexión a la BD
database.init()

#Invoca al archivo de configuración
with open('./config/config_countries.json', 'r') as config_file:
    config = json.load(config_file)

with open('./config/config_general.json', 'r') as config_file:
    config_general = json.load(config_file)

srv_name = config["CONFIG"]["LOG_TAG"]
db_str = config["CONFIG"]["DB"]
max_return = config_general["CONFIG"]["MAX_RETURN"]
limite = config["CONFIG"]["LIMIT_PER_SECOND"]
api = Api(APP, version='1.0', title=srv_name,
          description='getCountries de Callejero Predictivo\n'
                      'Conexión a BD:' + db_str + '\n'
                      'Cantidad máxima de invocaciones por segundo:' + limite)

ns = api.namespace('getCountry', description='getCountries de Callejero Predictivo')


md_respuesta = {'pais': fields.String(required=True, description='Agrupador de Paises'), 'list_pais': fields.Nested(fields.String)}


@ns.route('/<string:country>')
@ns.response(200, 'Success')
@ns.response(404, 'Not found')
@ns.response(429, 'Too many request')
@ns.param('country', 'Introducir Cadena de Caracteres para el Pais')
class getCountryClass(Resource):
    @ns.doc('getCountry')
    @ns.marshal_list_with(md_respuesta)
    def post(self, country):
        try:
            cur = database.db.cursor()
            listOutput = cur.var(cx_Oracle.STRING)
            cur.callproc('PREDICTIVO.get_pais', (country, max_return, listOutput))
        except Exception as e:
            database.init()
            if database.db is not None:
                log.err('Reconexion OK')
                cur = database.db.cursor()
                listOutput = cur.var(cx_Oracle.STRING)
                cur.callproc('PREDICTIVO.get_pais', (country, max_return, listOutput))
            else:
                log.err('Sin conexion a la base de datos')
                listOutput = None


        return listOutput, 200

但是在执行的时候,会产生如下错误:

Running on http://127.0.0.1:5200/ (Press CTRL+C to quit)
127.0.0.1 - - [12/Nov/2018 15:52:21] "GET / HTTP/1.1" 200 -
Unable to render schema
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\site-packages\flask_restplus\api.py", line 483, in __schema__
    self._schema = Swagger(self).as_dict()
  File "C:\Program Files\Python37\lib\site-packages\flask_restplus\swagger.py", line 177, in as_dict
    paths[extract_path(url)] = self.serialize_resource(ns, resource, url, kwargs)
  File "C:\Program Files\Python37\lib\site-packages\flask_restplus\swagger.py", line 346, in serialize_resource
    path[method] = self.serialize_operation(doc, method)
  File "C:\Program Files\Python37\lib\site-packages\flask_restplus\swagger.py", line 352, in serialize_operation
    'responses': self.responses_for(doc, method) or None,
  File "C:\Program Files\Python37\lib\site-packages\flask_restplus\swagger.py", line 464, in responses_for
    responses[code]['schema'] = self.serialize_schema(model)
  File "C:\Program Files\Python37\lib\site-packages\flask_restplus\swagger.py", line 509, in serialize_schema
    'items': self.serialize_schema(model),
  File "C:\Program Files\Python37\lib\site-packages\flask_restplus\swagger.py", line 529, in serialize_schema
    raise ValueError('Model {0} not registered'.format(model))
ValueError: Model {'pais': <flask_restplus.fields.String object at 0x00000189FFB94C88>, 'list_pais': <flask_restplus.fields.Nested object at 0x00000189FFB94F28>} not registered
127.0.0.1 - - [12/Nov/2018 15:52:22] "GET /swagger.json HTTP/1.1" 500 -

我找不到解决方法。 谁能给我一些线索来解决它?

这里是生产中的好版本,没有大摇大摆的工作 https://github.com/alemarchan/sources_predictivo_prod

【问题讨论】:

    标签: python python-3.x python-2.7 flask swagger


    【解决方案1】:

    查看https://flask-restplus.readthedocs.io/en/stable/_modules/flask_restplus/swagger.htmlhttps://flask-restplus.readthedocs.io/en/stable/quickstart.html

    注册的model不能是dict,需要model,尝试替换

    md_respuesta = {'pais': fields.String(required=True, description='Agrupador de Paises'), 'list_pais': fields.Nested(fields.String)}
    

    md_respuesta = api.model('Pais', {'pais': fields.String(required=True, description='Agrupador de Paises'), 'list_pais': fields.Nested(fields.String)})
    

    【讨论】:

    • 在招摇中再次出现“规范中没有定义的操作!”。我不知道该怎么办;-(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    相关资源
    最近更新 更多