Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rest

yaml

swagger

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?

like image 420
Mayan19 Avatar asked Sep 03 '26 19:09

Mayan19


1 Answers

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
like image 73
lekshmi Avatar answered Sep 07 '26 05:09

lekshmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!