3

I'm trying to make REST API with connexion but can't figure out how to set up get operation that consumes an array of objects. In the end, I need to get information about several items at once.

Using Python 3.7, connexion-2.3.0 and Swagger with OpenAPI Specification ver. 2.

My swagger file:

swagger: "2.0"
info:
  description: "This is documentation for REST api test."
  version: "1.0.0"
  title: test

consumes:
  - application/json
produces:
  - application/json

basePath: /api

paths:
  /test:
    get:
      operationId: test.test
      summary: "Just testing array."

      parameters:
        - name: query
          in: body
          schema:
            type: array
            items:
              $ref: '#/definitions/query'

      responses:
        200:
          description: All ok!

definitions:
  query:
      type: object

      required:
        - a
        - b

      properties:
        a:
          type: integer
          description: "Test propertie 1."
          example: 42

        b:
          type: string
          description: "Test propertie 2."
          example: "hi"

        c:
          type: string
          description: "Test propertie 3."
          example: "abc"

My Python file with test function:

def test(query):
    for item in query:
        print(item)

    return {'result': 'it is fine'}

Trying to pass JSON through Swagger UI:

[
  {
    "a": 42,
    "b": "hi",
    "c": "abc"
  }
]

Expected response from my test function:

{
  "result": "it is fine"
}

Actual response:

{
  "detail": "None is not of type 'array'",
  "status": 400,
  "title": "Bad Request",
  "type": "about:blank"
}
Rusllan
  • 33
  • 2
  • 6
  • The path operation is `get` - should it be `post` maybe? GET requests are not really supposed to have a request body. – Helen Aug 27 '19 at 20:07
  • @Helen Thanks! Looks like problem indeed in get operation, if I change it to post everything works fine. But I don't sure why I can't use a request body with get. I need to get information about several items at once. – Rusllan Aug 28 '19 at 06:08

1 Answers1

1

The problem is the GET verb, you can not use body. You should use something like that :

      parameters:
        - name: objects
          in: query
          required: true
          type: array
          items:
            type: string

The array itens must be of types : string , number, integer, boolean or array

Kevin Martins
  • 590
  • 7
  • 20