如何在 Swagger 中定义包含复杂对象数组的示例请求正文? [英] How to define an example request body containing an array of complex objects in Swagger?

查看:597
本文介绍了如何在 Swagger 中定义包含复杂对象数组的示例请求正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为将对象数组作为请求正文发布的服务编写 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?

推荐答案

我能够通过使用对象定义上的 example 属性并用 json 中的数组填充它来解决这个问题.

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

这篇关于如何在 Swagger 中定义包含复杂对象数组的示例请求正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆