小编Old*_*ter的帖子

如何在OpenAPI 3中引用数组项目示例?

使用此架构定义:

schemas:
  AllContacts:
    type: array
    items:
      $ref: '#/definitions/ContactModel1'
    example:
      - id: 1
        firstName: Sherlock
        lastName: Holmes
      - id: 2
        firstName: John
        lastName: Watson
Run Code Online (Sandbox Code Playgroud)

我得到了预期的结果:

[
  {
     "id": 1,
     "firstName": "Sherlock",
     "lastName": "Holmes"
  },
  {
     "id": 2,
     "firstName": "John",
     "lastName": "Watson"
  }
]
Run Code Online (Sandbox Code Playgroud)

现在,我想为单个用户(ContactModel1)和用户数组(AllContacts)的一部分重用Holmes示例。但是,如果我使用引用的示例:

schemas:

  AllContacts:
    type: array
    items:
      $ref: '#/definitions/ContactModel1'
    example:
      Homes:
        $ref: '#/components/examples/Homes'
      Watson:
        $ref: '#/components/examples/Watson'

  examples:

    Holmes:
      value:
        id: 1
        first_name: Sherlock
        last_name: Holmes

    Watson:
      value:
        id: 2
        first_name: John
        last_name: Watson
Run Code Online (Sandbox Code Playgroud)

我在Swagger UI中得到了这个意外的结果: …

swagger-ui openapi

4
推荐指数
1
解决办法
5989
查看次数

如何在OpenAPI 3中定义另一个模式的数组?

我定义了以下架构:

User:
  type: object
  required:
    - id
    - username
  properties:
    id:
      type: integer
      format: int32
      readOnly: true
      xml:
        attribute: true
      description: The user ID
    username:
      type: string
      readOnly: true
      description: The username
    first_name:
      type: string
      description: Users First Name
    last_name:
      type: string
      description: Users Last Name
    avatar:
      $ref: '#/components/schemas/Image'
  example:
    id: 10
    username: jsmith
    first_name: Jessica
    last_name: Smith
    avatar: image goes here
  xml:
    name: user
Run Code Online (Sandbox Code Playgroud)

效果很好。该GET /user/{id}调用会很好地显示示例数据。

我有第二个架构,可以创建上述架构的数组:

ArrayOfUsers:
  type: array
  items:
    type: object
    required:
      - id
      - …
Run Code Online (Sandbox Code Playgroud)

swagger openapi

3
推荐指数
1
解决办法
1618
查看次数

标签 统计

openapi ×2

swagger ×1

swagger-ui ×1