2

How to write documentation in swagger for array of objects. This is my code, but I don't know how to access data in array of objects.

{ 
    "first_name":"Sam",
    "last_name":"Smith",  
    "reservations":[
        {
            "chkin_date":"2016-03-31",
            "chkout_date":"2016-04-08",
            "adults":1,
            "children":2,
            "chdage":[2,3]
        },
        {
            "chkin_date":"2016-03-30",
            "chkout_date":"2016-04-03",
            "adults":1,
            "children":2,
            "chdage":[2,3,5]
        }
    ]
}
Grokify
  • 15,092
  • 6
  • 60
  • 81
user3568043
  • 133
  • 1
  • 2
  • 11
  • 1
    Check these posts: http://stackoverflow.com/questions/19585581/how-to-describe-a-model-i-swagger-for-an-array-with-simple-objects http://stackoverflow.com/questions/27860912/swagger-model-for-an-array-with-named-elements – Sampada Apr 01 '16 at 09:23
  • If you want to avoid typing by hand, you could try this JSON to Swagger Definitions converter: https://roger13.github.io/SwagDefGen/ – Roger Feb 02 '17 at 15:28
  • try the apibldr - Visual api builder for swagger (https://apibldr.com), you can create definitions from importing an example. – Gen4ik Aug 06 '17 at 14:20

1 Answers1

7

Here you go:

definitions:
  SomeObject:
    properties:
      first_name:
        type: string
        example: Sam
      last_name:
        type: string
        example: Smith

      reservations:
        type: array
        items:
          $ref: '#/definitions/Reservation'
  Reservation:
    properties:
      chkin_date:
        type: string
        format: date
        example: 2016-03-31
      chkout_date:
        type: string
        format: date
        example: 2016-04-08
      adults:
        type: integer
        format: int32
        example: 1
      children:
        type: integer
        format: int32
        example: 2
      chdage:
        type: array
        items:
          type: integer
          format: int32
fehguy
  • 6,724
  • 25
  • 23