Search code examples
javascriptswaggerswagger-ui

Swagger example post body reference from json file


I am trying to add an example body for my Swagger POST and want to reference the example schema to a JSON file but it is not working.

Do I have to use another ref type or something else or do I really have to define a definition model (which I want to avoid for readability)?

JSON File calendar.json:

{
 "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"
  }
 ],
 "uploadedBy": "um3i7um"
}

Swagger definition:

 * /business-calendar/:
 *    post:
 *      description: Add a new business calender
 *      responses:
 *        '200':
 *          description: Business calendar added
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *    parameters:
 *      - in: body
 *        name: body
 *        schema:
 *          $ref": '../../mock/data/calendar.json'
 *        required: true
 *        description: Calendar object
 *
 */

Solution

  • From their documentation at https://swagger.io/docs/specification/adding-examples/ So it would seem that there needs to be a set schema of an object and after an example is passed through it. My guess is that external value can be any external value be it link or file.

     content:
      application/json:
        schema:
          $ref: '#/components/schemas/MyObject'
        examples:
          jsonObject:
            summary: A sample object
            externalValue: 'http://example.com/examples/object-example.json'