簡體   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