当前位置: 首页 > 知识库问答 >
问题:

如何定义Swagger 2.0 JSON在Swagger UI中填充默认的body参数对象?

林亦
2023-03-14

我们当前的部署模式要求我手动编写我的 swagger json 输出,这些输出将由我公司使用的基于 Swagger 的 UI 使用。我希望我正在编写的 json 提供“默认”值,以填充所有输入字段(包括 body 输入参数)的 Swagger UI。我的 body 参数是一个引用对象,如下所示。如何定义返回的 swagger JSON,以便在单击“尝试此操作”时自动填充请求的正文部分?

下面是Swagger UI中没有填充默认/示例数据的示例Swagger规范。

{
   "swagger":"2.0",
   "info":{
      "description":"Example API Description",
      "title":"Example Title",
      "version":"v3.0"
   },
   "tags":[
      {
         "name":"v3"
      }
   ],
   "consumes":[
      "application/json"
   ],
   "produces":[
      "application/json"
   ],
   "paths":{
      "/v3/api/{subscriptionID}/example":{
         "post":{
            "tags":[
               "v3"
            ],
            "description":"This API will do something",
            "consumes":[
               "application/json"
            ],
            "produces":[
               "application/json"
            ],
            "parameters":[
               {
                  "name":"Accept",
                  "in":"header",
                  "description":"The Accept request-header field can be used to specify the media types which are acceptable for the response. If not provided, the default value will be application/json",
                  "required":false,
                  "default":"application/json",
                  "type":"string"
               },
               {
                  "name":"Content-Type",
                  "in":"header",
                  "description":"The MIME type of the body of the request.  Required for PUT, POST, and PATCH, where a request body is expected to be provided.",
                  "required":true,
                  "default":"application/json; charset=utf-8",
                  "type":"string"
               },
               {
                  "name":"company_locale",
                  "in":"header",
                  "description":"The desired language as spoken in a particular region preference of the customer of this particular transaction.  Consists of two lower case language",
                  "required":true,
                  "default":"en_US",
                  "type":"string"
               },
               {
                  "name":"originatingip",
                  "in":"header",
                  "description":"The originating ip address of the calling device or browser.",
                  "required":true,
                  "default":"100.100.10.1",
                  "type":"string"
               },
               {
                  "name":"transaction_id",
                  "in":"header",
                  "description":"The transaction identifier for this invocation of the service.  ",
                  "required":true,
                  "default":"1e2bd51d-a865-4d37-9ac9-c345dc59119b",
                  "type":"string"
               },
               {
                  "name":"subscriptionID",
                  "in":"path",
                  "description":"The unique identifier that represents the subscribed portfolio of products.",
                  "required":true,
                  "default":"1e2bd51d",
                  "type":"string"
               },
               {
                  "name":"body",
                  "in":"body",
                  "description":"The body of the request",
                  "required":true,
                  "schema":{
                     "$ref":"#/definitions/exampleDefinition"
                  }
               }
            ],
            "responses":{
               "200":{
                  "description":"OK",
                  "headers":{
                     "transaction_id":{
                        "type":"string",
                        "default":"de305d54-75b4-431b-adb2-eb6b9e546013",
                        "description":"The identifier for the service transaction attempt."
                     }
                  }
               }
            }
         }
      }
   },
   "definitions":{
      "exampleDefinition":{
         "type":"object",
         "description":"Request details for Example Definition",
         "properties":{
            "action":{
               "type":"string",
               "description":"Specifies the action to be taken"
            },
            "applyToBase":{
               "type":"string",
               "description":"A boolean value that defines the behaviour of the request against the base"
            },
            "addOnIDs":{
               "type":"string",
               "description":"The identifiers for the add-ons"
            }
         },
         "required":[
            "action",
            "applyToBase",
            "addOnIDs"
         ]
      }
   }
}

我一直在测试这个json在http://editor.swagger.io/#/通过单击文件-

提前感谢任何建议。


共有2个答案

颜嘉福
2023-03-14

要填充单击“尝试此操作”时要使用的默认值,您只需在定义中添加属性的“default”参数即可。这在在线编辑器中起作用:

{
   "swagger":"2.0",
   "info":{
      "description":"Example API Description",
      "title":"Example Title",
      "version":"v3.0"
   },
   "tags":[
      {
         "name":"v3"
      }
   ],
   "consumes":[
      "application/json"
   ],
   "produces":[
      "application/json"
   ],
   "paths":{
      "/v3/api/{subscriptionID}/example":{
         "post":{
            "tags":[
               "v3"
            ],
            "description":"This API will do something",
            "consumes":[
               "application/json"
            ],
            "produces":[
               "application/json"
            ],
            "parameters":[
               {
                  "name":"Accept",
                  "in":"header",
                  "description":"The Accept request-header field can be used to specify the media types which are acceptable for the response. If not provided, the default value will be application/json",
                  "required":false,
                  "default":"application/json",
                  "type":"string"
               },
               {
                  "name":"Content-Type",
                  "in":"header",
                  "description":"The MIME type of the body of the request.  Required for PUT, POST, and PATCH, where a request body is expected to be provided.",
                  "required":true,
                  "default":"application/json; charset=utf-8",
                  "type":"string"
               },
               {
                  "name":"company_locale",
                  "in":"header",
                  "description":"The desired language as spoken in a particular region preference of the customer of this particular transaction.  Consists of two lower case language",
                  "required":true,
                  "default":"en_US",
                  "type":"string"
               },
               {
                  "name":"originatingip",
                  "in":"header",
                  "description":"The originating ip address of the calling device or browser.",
                  "required":true,
                  "default":"100.100.10.1",
                  "type":"string"
               },
               {
                  "name":"transaction_id",
                  "in":"header",
                  "description":"The transaction identifier for this invocation of the service.  ",
                  "required":true,
                  "default":"1e2bd51d-a865-4d37-9ac9-c345dc59119b",
                  "type":"string"
               },
               {
                  "name":"subscriptionID",
                  "in":"path",
                  "description":"The unique identifier that represents the subscribed portfolio of products.",
                  "required":true,
                  "default":"1e2bd51d",
                  "type":"string"
               },
               {
                  "name":"body",
                  "in":"body",
                  "description":"The body of the request",
                  "required":true,
                  "schema":{
                     "$ref":"#/definitions/exampleDefinition"
                  }
               }
            ],
            "responses":{
               "200":{
                  "description":"OK",
                  "headers":{
                     "transaction_id":{
                        "type":"string",
                        "default":"de305d54-75b4-431b-adb2-eb6b9e546013",
                        "description":"The identifier for the service transaction attempt."
                     }
                  }
               }
            }
         }
      }
   },
   "definitions":{
      "exampleDefinition":{
         "type":"object",
         "description":"Request details for Example Definition",
         "properties":{
            "action":{
               "type":"string",
               "description":"Specifies the action to be taken",
               "default": "The default Action"
            },
            "applyToBase":{
               "type":"string",
               "description":"A boolean value that defines the behaviour of the request against the base",
               "default": "0"
            },
            "addOnIDs":{
               "type":"string",
               "description":"The identifiers for the add-ons",
               "default": "The default Add-On"
            }
         },
         "required":[
            "action",
            "applyToBase",
            "addOnIDs"
         ]
      }
   }
}

“”

慎懿轩
2023-03-14

要获得示例值,只需在需要的地方添加“示例”属性:

exampleDefinition:
  type: object
  description: Request details for Example Definition
  properties:
    action:
      type: string
      description: Specifies the action to be taken
      example: An action value
    applyToBase:
      type: string
      description: >-
        A boolean value that defines the behaviour of the request against the base
      example: An apply to base value
    addOnIDs:
      type: string
      description: The identifiers for the add-ons
      example: an ID
 类似资料:
  • 我正在使用Laravel5构建一个多语言网站。 我知道,在Laravel中,我可以为如下路线定义: 在中间件中,我可以获取url路径的片段来检测语言并设置当前请求的区域设置。 但我找不到无论如何添加lang参数在默认情况下的路由像folowings: 或者有什么方法可以钩住框架的路由函数来附加默认参数进入所有路线时,我打电话?(ja是之前在中间件中设置的应用程序的当前区域设置) 谢谢

  • 本文向大家介绍Python如何定义有默认参数的函数,包括了Python如何定义有默认参数的函数的使用技巧和注意事项,需要的朋友参考一下 问题 你想定义一个函数或者方法,它的一个或多个参数是可选的并且有一个默认值。 解决方案 定义一个有可选参数的函数是非常简单的,直接在函数定义中给参数指定一个默认值,并放到参数列表最后就行了。例如: 如果默认参数是一个可修改的容器比如一个列表、集合或者字典,可以使用

  • 问题内容: 我对Java,JavaFX和编程一般还是有点陌生​​,但是我遇到的问题困扰着我。 在大多数教程中,我查找了有关填充ListView(更具体地说,使用ObservableArrayList)的方法,最简单的方法是从String的ObservableList中创建它,如下所示: 但是我不想使用字符串。我想使用我制作的名为Words的自定义对象: 每个Word对象只有两个属性:wordStr

  • 我对Java、JavaFX和一般编程都有点陌生,我有一个问题让我大吃一惊。 在我查阅的大多数关于填充ListView(更具体地说,使用ObservableArrayList)的教程中,最简单的方法是从字符串的ObservableList创建它,如下所示: 但我不想用字符串。我想使用我制作的一个名为Words的自定义对象: 要明确的是,这不是android的,单词列表最终会被更改、保存和加载,所以我

  • 问题 你想定义一个函数或者方法,它的一个或多个参数是可选的并且有一个默认值。 解决方案 定义一个有可选参数的函数是非常简单的,直接在函数定义中给参数指定一个默认值,并放到参数列表最后就行了。例如: def spam(a, b=42): print(a, b) spam(1) # Ok. a=1, b=42 spam(1, 2) # Ok. a=1, b=2 如果默认参数是一个可修改的容器

  • 你能帮我指出什么是默认的RSA填充吗。 准确地说,如果我按照下面的方式创建密码实例,那么肯定java使用了某种填充,因为加密文本字节长度对于2048个RSA密钥总是显示为256字节,而不管纯文本是1个字符还是10个字符。