How to write an OpenAPI schema for a nested arrays with mixed type values?

Viewed 44

My API returns the response in following format:

[
  [
    "Time",
    "Attempts",
    "Successes",
    "Failures",
    "BookabilityRatio"
  ],
  [
    "2022-07-30",
    995935,
    734009,
    61926,
    73.70
  ],
  [
    "2022-07-31",
    949576,
    683740,
    265836,
    72.00
  ]
]

It's a list or list or array or array where each element has a different data type. How to create an OpenAPI schema for this response?

1 Answers

An array of mixed-type arrays can be defined as follows:

type: array
items:
  type: array
  items: {}   # An empty schema means "any type"

Or if the values are limited to just strings and numbers (i.e. no booleans, objects or more nested arrays) AND you use OpenAPI 3.x, you could use:

# OpenAPI 3.1 version
type: array
items:
  type: array
  items:
    type: [string, integer, number]

# OpenAPI 3.0 version
type: array
items:
  type: array
  items:
    anyOf:
      - type: string
      - type: integer
      - type: number
Related