restyamlswagger

openapi 3.0: how to add examples of objects having nested objects inside it?


I want to add examples of my response schema in openapi 3.0 YAML. I have went through the idea on link https://swagger.io/docs/specification/adding-examples/ but my issue is that my response schema object contains nested objects inside it. Can anyone help and guide me about how to add example while having nested objects?


Solution

  • You can define a response example in two ways. Let this is your nested json object response :

    {
      "status": true,
      "data": {
        "storeId": "string",
        "message": "string"
      }
    }
    

    Method 1: Here in parameter definition itself you can add the example

    myschema:
      type: object
      properties:
        status:
          type: boolean
          required: true
          example: true
        data:
          type: object
          properties:
            "message":
              type: string
              example: Success
            "Id":
              type: string
              example: 1234
    

    Method 2: Here after the property definition you can define an example: tag like this:

    myschema:
      type: object
      properties:
        status:
          type: boolean
          required: true
        data:
          type: object
          properties:
            message:
              type: string
            Id:
              type: string
      example: 
        status: true
        data:
          Id: '1234'
          message: success