【问题标题】:Problem with swagger array of objects: validation error: None is not of type 'array'对象数组大摇大摆的问题:验证错误:无不是“数组”类型
【发布时间】:2019-08-29 01:29:25
【问题描述】:

我正在尝试使用 connexion 制作 REST API,但不知道如何设置使用对象数组的 get 操作。最后,我需要一次获取多个项目的信息。

使用 Python 3.7、connexion-2.3.0 和 Swagger 以及 OpenAPI 规范版本。 2.

我的招摇文件:

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"

我的带有测试功能的 Python 文件:

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

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

尝试通过 Swagger UI 传递 JSON:

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

我的测试函数的预期响应:

{
  "result": "it is fine"
}

实际反应:

{
  "detail": "None is not of type 'array'",
  "status": 400,
  "title": "Bad Request",
  "type": "about:blank"
}

【问题讨论】:

  • 路径操作是get——应该是post吧? GET 请求实际上不应该具有请求正文。
  • @Helen 谢谢!看起来确实在获取操作中出现问题,如果我将其更改为发布一切正常。但我不确定为什么我不能将请求正文与 get 一起使用。我需要一次获取多个项目的信息。

标签: python swagger connexion


【解决方案1】:

问题是GET动词,不能使用body。你应该使用类似的东西:

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

数组元素的类型必须为:字符串、数字、整数、布尔值或数组

【讨论】:

  • 我不能放一个类型的数组:对象吗?有什么办法吗?
  • 使用 get ?我不明白
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 2019-02-08
  • 2021-09-03
  • 2013-05-11
  • 2012-08-01
相关资源
最近更新 更多