首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在SwaggerHub中返回对象数组?

如何在SwaggerHub中返回对象数组?
EN

Stack Overflow用户
提问于 2017-09-12 12:33:58
回答 2查看 19.5K关注 0票数 18

我正在使用API2.0在SwaggerHub中定义一个OpenAPI规范。/contacts请求返回一个联系人数组。定义如下:

代码语言:javascript
复制
  /contacts:     
    get:
      tags:
      - contacts
      summary: Get all the contacts
      description: This displays all the contacts present for the user.
      operationId: getContact
      produces:
      - application/json
      - application/xml  
      responses:
       200:
        description: successful operation
        schema:
          $ref: '#/definitions/AllContacts'
       400:
        description: Invalid id supplied
       404:
        description: Contact not found
       500:
        description: Server error
    definitions:
      AllContacts:
       type: array
       items:
       -  $ref: '#/definitions/ContactModel1'
       -  $ref: '#/definitions/ContactModel2'
    
        
      ContactModel1:
        type: object
        properties:
          id:
            type: integer
            example: 1
          firstName:
            type: string
            example: 'someValue'
          lastName:
            type: string
            example: 'someValue'
            
       ContactModel2:
        type: object
        properties:
          id:
            type: integer
            example: 2
          firstName:
            type: string
            example: 'someValue1'
          lastName:
            type: string
            example: 'someValue1'

由于某种原因,它只返回第二个对象,而不是整个对象数组。

我使用的是OpenAPI 2.0,我怀疑这个版本对数组的支持不是很好。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-13 16:27:37

对象数组的定义如下。items的值必须是描述数组项的单个模型。

代码语言:javascript
复制
definitions:
  AllContacts:
    type: array
    items:
      $ref: '#/definitions/ContactModel'

  ContactModel:
    type: object
    properties:
      id:
        type: integer
        example: 1
      firstName:
        type: string
        example: Sherlock
      lastName:
        type: string
        example: Holmes

默认情况下,Swagger UI仅显示包含一项的数组示例,如下所示:

代码语言:javascript
复制
[
  {
     "id": 1,
     "firstName": "Sherlock",
     "lastName": "Holmes"
  }
]

如果希望数组示例包含多个项,请在数组模型中指定多项example

代码语言:javascript
复制
definitions:
  AllContacts:
    type: array
    items:
      $ref: '#/definitions/ContactModel1'
    example:
      - id: 1
        firstName: Sherlock
        lastName: Holmes
      - id: 2
        firstName: John
        lastName: Watson
票数 40
EN

Stack Overflow用户

发布于 2021-10-12 15:12:34

我意识到这有点离题,但我来到这里是为了寻找OpenApi 3.0的示例。对于其他正在寻找同样的东西的人来说,这是如何做到的:

代码语言:javascript
复制
paths:
  /product-category:
    get:
      summary: 'Returns all product categories'
      operationId: readProductCategory
      tags:
        - productCategory
      responses:
        '200':
          description: 'Details about all product categories'
          content:
            application/json:
              schema:
                type: array
                items:
                  allOf:
                    - $ref: '#/components/schemas/Identifier'
                    - $ref: '#/components/schemas/ProductCategory'
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46167981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档