Python中的Swagger数组响应不适用于生成的clien

2024-09-30 00:40:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在Python中使用Swagger和Flask和connexion实现restapi。 我在Python中使用swagger codegen生成了一个客户机。 当使用一个带有GET调用的接口(返回一个数组)时,我可以在调试日志中看到响应如预期的那样到达客户机,但是客户机没有正确地处理它。你知道吗

这是我的大摇大摆yaml定义

schemes:
 - http
paths:             
  /elements: 
      get:
        summary: 'Fetch elements from the database'
        operationId: elements.get_elements
        responses:
          '200':
              description: Fetch elements from the database
              schema:
                 type: array
                 items:
                 $ref: '#/definitions/element'   


definitions:
  element:
    type: object
    properties:
      id:           { type: string }
      desc:         { type: string }

这是我的API的实现

from flask import jsonify

elements = [{'id':'1', 'desc':'description1'},{'id':'2', 'desc':'description2'}]


def get_elements():
    """
        Gets elements
    """
    return jsonify(elements)

这是我的客户电话记录

>>> api_instance.elements_get_elements()
2019-11-26 11:28:02,946 DEBUG Starting new HTTP connection (1): localhost:5123
2019-11-26 11:28:02,946 DEBUG Starting new HTTP connection (1): localhost:5123
send: b'GET /elements HTTP/1.1\r\nHost: localhost:5123\r\nAccept-Encoding: identity\r\nUser-Agent: Swagger-Codegen/1.0.0/python\r\nContent-Type: application/json\r\n\r\n'
reply: 'HTTP/1.0 200 OK\r\n'
header: Content-Type: application/json
header: Content-Length: 108
header: Server: Werkzeug/0.16.0 Python/3.6.8
header: Date: Tue, 26 Nov 2019 10:28:02 GMT
2019-11-26 11:28:02,953 DEBUG http://localhost:5123 "GET /elements HTTP/1.1" 200 108
2019-11-26 11:28:02,953 DEBUG http://localhost:5123 "GET /elements HTTP/1.1" 200 108
2019-11-26 11:28:02,954 DEBUG response body: [
  {
    "desc": "description1", 
    "id": "1"
  }, 
  {
    "desc": "description2", 
    "id": "2"
  }
]

2019-11-26 11:28:02,954 DEBUG response body: [
  {
    "desc": "description1", 
    "id": "1"
  }, 
  {
    "desc": "description2", 
    "id": "2"
  }
]

{'desc': None, 'id': None}

如您所见,最后一行显示的结果不是我预期的响应数组解释


Tags: fromdebugidlocalhosthttpget客户机type

热门问题