Forum Discussion

godlessendeavor's avatar
godlessendeavor
Occasional Visitor
5 years ago

Swagger array response in Python not parsed as expected for generated client

I am implementing a REST API in Python using Swagger with Flask and connexion. I generated a client using swagger-codegen in Python. When using an interface with a GET call which returns an array I can see on the debugging logging that the response is arriving as expected to the client but the client does not process it correctly.

Here's my swagger yaml definition

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 }

And this is the implementation of my API

from flask import jsonify

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


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

This is the logging of my client call

>>> 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}

As you can see the last line shows the result which is not the array I was expecting.

No RepliesBe the first to reply