How to define an array of another schema in OpenAPI 3? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-07 17:12:29

问题


I have this schema defined:

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

Works great. The GET /user/{id} call displays the sample data just fine.

I have a second schema that creates an array of the above schema:

ArrayOfUsers:
  type: array
  items:
    type: object
    required:
      - id
      - username
    properties:
      id:
        type: integer
        format: int32
        xml:
          attribute: true
        description: The user ID
      username:
        type: string
        description: The username
      first_name:
        type: string
        description: Users First Name
      last_name:
        type: string
        description: Users Last Name
      avatar:
        $ref: '#/components/schemas/Image'

This also works great. The GET /user call displays the proper structure in an array just fine.

But I'd rather not define this schema twice.

I would like to create a schema that utilizes the first one and stick in an array.

I have failed in this attempt.

I tried it this way:

UserArray:
  type: array
  items:
    type: object
    required:
      - id
      - username
  properties:
    things:
      type: array
      items:
        oneOf:
          - $ref: '#/components/schemas/User'

This attempt gives me an empty array:

[
  {}
]

This is not my desired result.

Any hints on this?


回答1:


An array of User objects is defined as follows:

UserArray:
  type: array
  items:
    $ref: '#/components/schemas/User'


来源:https://stackoverflow.com/questions/49827240/how-to-define-an-array-of-another-schema-in-openapi-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!