如何在 openAPI.json 文件中设置默认标头以在所有请求中将 api_key 作为标头发送

问题描述

著名的 https://petstore.swagger.io/ 在其“securitySchemas”中定义了“api_key”。我按照相同的方法在我的 json 文件中添加了“api_key”。我所有的 Restful Web 服务都需要将其作为标头参数。

我的假设以及我希望它在 openapi.json 中的一个位置定义标头的内容,并且当从 swagger 调用时,我的所有网络服务都应该获得一个请求标头。

它在 petstore 中定义的方式似乎不起作用。我正在设置 api_key 。但似乎我必须在每个服务定义中单独明确定义 header 参数。

enter image description here

以下是我的 openapi.json 文件

 {
  "components": {
    "schemas": {
      "connection": {
        "properties": {
          "connid": {
            "example": "LIMS1_107c5a8c1799d4cf8bb5ce295ab38954","type": "string"
          }
        },"type": "object","$ref": "components/schemas/connection","required": [
          "connid"
        ]
      }
    },"securitySchemes": {
      "api_key": {
        "type": "apiKey","name": "api_key","in": "header"
      }
    }
  },"externalDocs": {
    "description": "Find out more about Test API","url": "http://www.Test.com/API"
  },"info": {
    "contact": {
      "email": "[email protected]"
    },"description": "Test LIMS RESTful and CRUD API","license": {
      "name": "Apache 2.0","url": "http://www.Test.com/licenses/LICENSE-2.0.html"
    },"termsOfService": "http://www.Test.com/terms/","title": "Test LIMS API - OpenAPI 3.0.2","version": "1.0.0"
  },"openapi": "3.0.2","paths": {
    "/rest/authenticate": {
      "description": "authenticate and get a token","summary": "authenticate and get a connection token","get": {
        "tags": [
          "connection"
        ],"operationId": "authenticate","parameters": [
          {
            "required": true,"schema": {
              "description": "TEST datasource to connect to","type": "string"
            },"in": "query","name": "username"
          },{
            "required": true,"schema": {
              "description": "password of the user","name": "password"
          },"name": "datasource"
          },"schema": {
              "description": "TEST Service to connect to","name": "service"
          },{
            "required": false,"schema": {
              "description": "User role","name": "role"
          }
        ],"responses": {
          "200": {
            "description": "Successful Connection","content": {
              "application/json": {
                "schema": {}
              }
            }
          }
        }
      }
    },"/rest/close": {
      "description": "authenticate and get a token","operationId": "close","schema": {
              "description": "id of the connection to close","name": "connectionid"
          }
        ],"/rest/function/ABS": {
      "description": "Answer the absolute value of the number\r\nx = ABS(number)","get": {
        "tags": [
          "REST API"
        ],"schema": {
              "type": "string"
            },"name": "number"
          }
        ],"responses": {
          "200": {
            "description": "Successful Operation","content": {
              "application/xml": {
                "schema": {
                  "format": "string","type": "string","title": "x"
                }
              },"application/json": {
                "schema": {
                  "format": "string","title": "x"
                }
              }
            }
          },"400": {
            "description": "Invalid funnction provided"
          },"405": {
            "description": "Validation Exception"
          }
        }
      }
    }
  },"security": [
    {"api_key" :  []}
  ],"servers": [
    {
      "url": "http://localhost:8080/LIMS-REST/api/","description": "description"
    }
  ],"tags": [
    {
      "name": "connection","description": "Connection management API ","externalDocs": {
        "description": "Connection management API","url": "http://www.Test.com"
      }
    },{
      "name": "REST API","description": "Test's non-crud APIs","externalDocs": {
        "description": "Test's non-crud APIs",{
      "name": "objects","description": "Test CRUD Operations on selected table","externalDocs": {
        "description": "CRUD API Test LIMS. Can be done on selected tables","url": "http://www.Test.com"
      }
    }
  ]
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)