简体   繁体   中英

How to define an example request body containing an array of complex objects in Swagger?

I am trying to write the Swagger spec for a service that posts an array of objects as request body. I would like the user to be able to test the service with a specific set of multiple different complex objects in the array as the default sample inputs.

So far I have the following code defining the service and complex object:

paths:
    /myService
        post:
            summary: test 123
            description: test 123
            parameters:
                - name: bodyParamsObject
                  description: 'Object containing any / all params in the body'
                  in: body
                  required: true
                  schema:
                    properties:
                        data:
                            $ref: '#/definitions/myInputArray'
            responses:
                200:
                    description: OK
                    schema: myOutputArray

definitions:
    myInputArray:
        type: array
        items:
            $ref: '#/definitions/myComplexObject'

    myOutputArray:
        type: array
        items:
            $ref: '#/definitions/myComplexObject'

    myComplexObject:
        type: object
        properties:
            description:
            type: string
            example: 'Example Item'     
        size:
            example: 552
            type: integer
            format: int32
        hasAdditionalProperties:
            type: boolean
            example: true

The output array is coming back correctly, but there is only one item in the model schema.

How can I make the sample request body contain multiple different items in the array?

I was able to solve this by using the example property on the object definition and filling it with the array in json.

definitions:
    myInputArray:
        type: array
        items:
            $ref: '#/definitions/myComplexObject'
        example: [
                    {
                        "description": "Example Item 1",
                        "hasAdditionalProperties": true,
                        "size": 750,
                    },
                    {
                        "description": "Another example",
                        "hasAdditionalProperties": false,
                        "size": -22,
                    },                                
                    {
                        "description": "Last one",
                        "hasAdditionalProperties": true,
                        "size": 0,
                    }
                ]

    myComplexObject:
        type: object
        properties:
            description:
            type: string
            example: 'Example Item'     
        size:
            example: 552
            type: integer
            format: int32
        hasAdditionalProperties:
            type: boolean
            example: true

I have similar request body with another object appending to an array. Expected:

               {
                    "data": [
                               {
                                   "name": "nodename1",
                                   "description": "here is the description",
                                   "expired": "no"
                               },
                               {
                                    "name": "nodename2",
                                    "description": "here is the description",
                                    "expired": "yes"
                               }
                            ],
                           "time_zone": "IST",
                           "version": "2.3.0",
                           "module": "abc"
             }

in the YAML i am writing like this :

               requestBody:
                    description: this is the body
                    required: true
                    content:
                       application/json:
                       schema:
                           type: array
                           items:
                             $ref: '#/components/schemas/data'



        components:
           schemas:
             otherDetails:  
                 type : object
                 required:
                   - time_zone
                   - version
                   - module
                 properties:
                    time_zone:
                       type: string
                       example: IST
                    version:
                       type: string
                       example: 2.3.0
                    module:
                       type: string
                       example: abc
        data:
          type: object
          required:
            - name
            - description
            - expired
          properties:
             name:
                type: string
                example: dummyNode
             description:
                type: string
                example: description
             expired:
                type: string
                example: no
            otherDetails:
                $ref: '#/components/schemas/otherDetails'

But getting this as part of array only. OUTPUT :

     [
             {
               "data": [
                 [
                   {
                     "name": "nodename1",
                     "description": "ab804529-11d0-4781-a49a-3bbbc40243df",
                     "expired": "no"
                   },
                   {
                     "name": "nodename2",
                     "description": "jlefliwajef-45f4-4322-a453-fafe3",
                     "expired": "yes"
                   }
                 ]
               ],
               "time_zone": "string",
               "version": "string",
               "module": "string"
             }
         ]

Can some one help me on this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM