Forum Discussion

marcschacher's avatar
marcschacher
New Contributor
5 years ago

Array items

Hi,
i've a question about arrays and the number of valid elements. are the following assumptions correct?

 

example1:

 

example1:
  type: object
  properties:
    name:
      type: string
      maxLength: 50
    comments:
      type: array
      items:
        type: string
        maxLength: 50
  required:
    - name

 

assumption: number of valid comment entries: 0 - n?

 

example 2:

example2:
  type: object
  properties:
    name:
      type: string
      maxLength: 50
    comments:
      type: array
      items:
        type: string
        maxLength: 50
  required:
    - name
    - comments

assumption: number of valid comment entries: 1 - n

 

example 3:

example3:
  type: object
  properties:
    name:
      type: string
      maxLength: 50
    comments:
      type: array
      items:
        type: string
        maxLength: 50
      minItems: 1
  required:
    - name

 assumption: number of valid comment entries: 1 - n

 

example 4

example4:
  type: object
  properties:
    name:
      type: string
      maxLength: 50
    comments:
      type: array
      items:
        type: string
        maxLength: 50
      minItems: 0
  required:
    - name

assumption: number of valid comment entries: 0 - n

 

example 5:

  /operation1:
    get:
      tags:
        - example
      operationId: operation1
      responses:
        "200":
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string

assumption: nr of valid items: 0 - n

 

example 6:

  /operation1:
    get:
      tags:
        - example
      operationId: operation1
      responses:
        "200":
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
                minItems: 1

assumption: nr of valid items: 1 - n

 

thanks 🙂

  • By default, arrays can have any number of elements including none - that is, an empty array [ ] is valid. Array length can be restricted by minItems (default is 0) and maxItems.

     

    The required attribute doesn't control the array length, it just means that the array property must be present in the payload, regardless of the number of elements.

     

    1 - correct

    2 - no, it's also 0 - n.

    3 - correct

    4 - correct

    5 - correct

    6 - correct

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    By default, arrays can have any number of elements including none - that is, an empty array [ ] is valid. Array length can be restricted by minItems (default is 0) and maxItems.

     

    The required attribute doesn't control the array length, it just means that the array property must be present in the payload, regardless of the number of elements.

     

    1 - correct

    2 - no, it's also 0 - n.

    3 - correct

    4 - correct

    5 - correct

    6 - correct