整数 return 类型的 Swagger 数组

Swagger array of integer return type

我刚刚开始使用 swagger-editor 来定义我的 RESTful API,我对回复感到困惑。我的许多方法只是 return 一个整数数组,我不明白如何在 YAML 中指定它。

OpenAPI (fka Swagger) 规范 2.0 使用 JSON Schema v4 的子集。具体支持什么可以参考JSON Schema docs or this great guide to learn how to describe different data types using JSON Schema. But keep in mind that some features of JSON Schema are not supported or work differently in OpenAPI/Swagger. The Specification mentions

回到你的问题,整数数组定义为:

type: array
items:
  type: integer

或在回复的上下文中:

paths:
  /something:
    get:
      responses:
        200:
          description: OK
          schema:
            type: array
            items:
              type: integer

如果在规范中多处使用整数数组,您可以在全局 definitions 部分定义数组,然后使用 $ref 引用它:

paths:
  /something:
    get:
      responses:
        200:
          description: OK
          schema:
            $ref: "#/definitions/ArrayOfInt"

definitions:
  ArrayOfInt:
    type: array
    items:
      type: integer

您还可以为数组指定 example 值。 Swagger UI 将显示此示例,一些模拟工具将在生成示例响应时使用它。

definitions:
  ArrayOfInt:
    type: array
    items:
      type: integer
    example: [1, 2, 3, 4]
    # Make sure to put the multi-item "example"
    # on the same level as the "type" and "items" keywords