如何在Swagger中定义包含复杂对象数组的示例请求主体?

时间:2018-04-19 20:20:03

标签: express yaml swagger openapi

我正在尝试为一个将对象数组作为请求体发布的服务编写Swagger规范。我希望用户能够使用阵列中一组特定的多个不同复杂对象来测试服务作为默认样本输入。

到目前为止,我有以下代码定义服务和复杂对象:

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

输出数组正确返回,但模型架构中只有一个项目。

如何让示例请求正文包含数组中的多个不同项?

1 个答案:

答案 0 :(得分:1)

我能够通过在对象定义上使用example属性并使用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