2

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?

4
  • Possible duplicate of Return an array of object in Swaggerhub Commented Apr 19, 2018 at 20:30
  • Nope. This one is about input parameters within a POST request body, not outputs from a GET operation. Commented Apr 19, 2018 at 20:48
  • It's mostly the same because the real question here and there is "how to specify an array example containing multiple items?" Commented Apr 19, 2018 at 21:17
  • 1
    I disagree, I think this question specifically addresses the request body of a POST operation which should contain multiple different items in an array. In any case I think this question adds value as it also illustrates how to do a default request body in addition to the array of different items as inputs. Commented Apr 19, 2018 at 23:46

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

0
/**
 * @swagger
 * components: 
 *    schemas:
 *      JSBaseItem:
 *        type: object
 *        properties:
 *          name:
 *            type: string
 *            description:  The skill name for the job req
 *          weight: 
 *            type: integer
 *            description: The value assigned to the skill name 
 */

/**
 * @swagger
 * components: 
 *    schemas:
 *      JobSearchItem:
 *        type: object
 *        properties:
 *          skills:
 *            type: array
 *            description:  The skill name for the job req
 *            items:
 *               $ref: '#/components/schemas/JSBaseItem'
 *          position: 
 *            type: array
 *            description: The value assigned to the skill name 
 *            items:
 *               $ref: '#/components/schemas/JSBaseItem'
 * 
 */


 *      requestBody:
 *        required: true
 *        content: 
 *          application/json:
 *            schema: 
 *               type: object
 *               items:
 *                 $ref: '#/components/schemas/JobSearchItem'
 *            example: {"skills":[{"name":"skillname","weight": 0}],"position":[{"name":"positionname","weight": 0}]}

To get an example of my complex JSON message to appear as an example in my Swagger Documentation, I had to write the example message keeping on a single line.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.