提问者:小点点

如何在post request swagger的主体中添加默认JSON对象


我想将JSON作为POST请求正文的默认值添加到这个swaggerendpoint。我一生都无法弄清楚这种格式应该是什么,以添加一个非常大的JSON对象作为POSTendpoint的默认值。请参阅下面的示例swagger规范

{
  "consumes": [
    "application/x-www-form-urlencoded"
  ],
  "definitions": {},
  "info": {
    "title": "swagger help",
    "version": "1.0"
  },
  "paths": {
    "/endpoint_name": {
      "post": {
        "consumes": [
          "application/json"
        ],
        "parameters": [
          {
            "default": {
              "application/json": [{"test": "value"}]
            },
            "description": "JSON of a list of values",
            "in": "body",
            "name": "body",
            "required": true,
            "type": "object"
          }
        ],
        "produces": [
          "application/json"
        ],
        "security": [
          {
            "APIKeyHeader": [
              "Authorization"
            ]
          }
        ],
      }
    },
  "produces": [
    "application/json"
  ],
  "securityDefinitions": {
    "APIKeyHeader": {
      "description": "description here",
      "in": "header",
      "name": "Authorization",
      "type": "apiKey"
    }
  },
  "swagger": "2.0"
}

共1个答案

匿名用户

在OpenAPI 2.0中,主体参数使用schema关键字。这是您指定typedefaultexample和其他与类型相关的详细信息的地方。

        "parameters": [
          {
            "description": "JSON of a list of values",
            "in": "body",
            "name": "body",
            "required": true,
            "schema": {
              "type": "object",
              "default": {"test": "value"}
            }
          }
        ],