当前位置:  开发笔记 > 前端 > 正文

整数返回类型的Swagger数组

如何解决《整数返回类型的Swagger数组》经验,为你挑选了1个好方法。

我刚刚开始使用swagger-editor来定义我的RESTful API,我对这些响应感到困惑.我的许多方法只返回一个整数数组,我不知道如何在YAML中指定它.



1> Helen..:

OpenAPI(fka Swagger)规范2.0使用JSON Schema v4的子集.您可以参考JSON Schema文档或本指南,了解如何使用JSON Schema描述不同的数据类型.但请记住,在OpenAPI/Swagger中,JSON Schema的某些功能不受支持或工作方式不同.规范提到了究竟支持的内容.

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

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

推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有