Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 546
Old Man Walter Avatar asked Apr 14 '18 02:04

Old Man Walter


1 Answers

An array of User objects is defined as follows:

UserArray:
  type: array
  items:
    $ref: '#/components/schemas/User'
like image 197
Helen Avatar answered Nov 01 '22 21:11

Helen