4

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"
    }
  ]
}

1 Answer 1

5

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.