mrks
mrks

Reputation: 5631

OpenAPI 3: Use array of objects for requestBody example

How can I define workingDays as array so that dayIndex, dayStart and dayEnd are one object inside?

 * /business-calendar/:
 *    post:
 *      description: Add a new business calender
 *      responses:
 *        '200':
 *          description: Business calendar added
 *      requestBody:
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *            example:
 *              name: "Standard"
 *              validFrom: "2021-01-01T00:00:00.000Z"
 *              validTo: "2021-12-31T00:00:00.000Z"
 *              useHolidays: true
 *              workingDays:
 *                dayIndex: 0
 *                dayStart: "8:00"
 *                dayEnd: "20:00"
 */

Current output:

{
  "name": "Standard",
  "validFrom": "2021-01-01T00:00:00.000Z",
  "validTo": "2021-12-31T00:00:00.000Z",
  "useHolidays": true,
  "workingDays": {
    "dayIndex": 0,
    "dayStart": "8:00",
    "dayEnd": "20:00"
  }
}

Desired output:

{
  "name": "Standard",
  "validFrom": "2021-01-01T00:00:00.000Z",
  "validTo": "2021-12-31T00:00:00.000Z",
  "useHolidays": true,
  "workingDays": [
    {
      "dayIndex": 0,
      "dayStart": "8:00",
      "dayEnd": "20:00"
    }
  ]
}

Upvotes: 3

Views: 14884

Answers (1)

Mindflower
Mindflower

Reputation: 56

The way I would approach this would be to create a workingDay object, and pass an array of those objects in the request body. To do this, you'll want workingDay to be defined as a schema with the desired properties in the schema section under components. Notice the use of '$ref' to point an example workingDay object:

https://swagger.io/docs/specification/using-ref/

workingDay:
  description: describe my working days
  type: object
  example:
    $ref: "#/components/examples/workingDay"
  properties:
    dayIndex: 
      description: index my day
      type: foo
    dayStart: 
      description: start my day
      type: bar
    dayEnd: 
      description: end my day
      type: baz

The example object might look something like this:

https://swagger.io/docs/specification/adding-examples/

workingDay:
  summary: example values of a workingDay object
  value:
    dayIndex: 0
    dayStart: 8:00
    dayEnd: 20:00

Then when you go to describe the endpoint requestBody, it'll look more like this:

https://swagger.io/docs/specification/describing-request-body/

requestBody: 
  content:
    application/json:
      schema:
        type: object
        properties:
          name: 
            type: string
          validFrom: 
            type: string
          validTo: 
            type: string
          useHolidays: 
            type: bool
          workingDays:
            type: array
            items:
              $ref: "#/components/schemas/workingDay"

Upvotes: 4

Related Questions