20

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
  • Thank you for answer AND pointing to the other question. Commented Apr 15, 2018 at 5:54

1 Answer 1

47

An array of User objects is defined as follows:

UserArray:
  type: array
  items:
    $ref: '#/components/schemas/User'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.