繁体   English   中英

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

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

我正在尝试为服务发布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

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

如何使样本请求正文在数组中包含多个不同的项目?

我可以通过在对象定义上使用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

我有类似的请求正文,另一个对象附加到数组。 预期:

               {
                    "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"
             }

在YAML中,我这样写:

               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'

但是只能将此作为数组的一部分。 输出:

     [
             {
               "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"
             }
         ]

有人可以帮我吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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