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"
}
2
  • The path operation is get - should it be post maybe? GET requests are not really supposed to have a request body. Commented Aug 27, 2019 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. Commented Aug 28, 2019 at 6:08

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Can't I put there an array of type: object? Is there any way to do it?
using get ? I dont get it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.