首页 > 解决方案 > 导入开放 API 规范时 Azure API 管理不遵守 operationId

问题描述

我有从 Swashbuckle 生成的 swagger.json。将开放 API 规范导入 Azure API 管理时,不使用 operationId 作为操作的名称。相反,它使用描述。我附上了产生问题的 JSON 样本。

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "Route Manager API"
  },
  "paths": {
    "/api/account/Logout": {
      "post": {
        "tags": [ "Account" ],
        "summary": "Logs the current user out of the system.",
        "operationId": "ApiAccountLogoutPost",
        "consumes": [],
        "produces": [],
        "parameters": [],
        "responses": { "200": { "description": "Logout successfully performed" } }
      }
    }
  }
}

在此处输入图像描述

以下示例在导入 Open API 规范时取自 Microsoft 文档,并且似乎使用 OperationId 作为名称 (GetSessions) 确实显示为 API 管理中函数的标题。

  "paths": {
    "/sessions": {
      "get": {
        "description": "A list of sessions.  Optional parameters work as filters to reduce the listed sessions.",
        "operationId": "GetSessions",
        "parameters": [
          {
            "name": "speakername",
            "in": "query",
            "type": "string"
          },
          {
            "name": "dayno",
            "in": "query",
            "description": "Format - int32.",
            "type": "integer"
          },
          {
            "name": "keyword",
            "in": "query",
            "type": "string"
          }
        ],
        "responses": { "200": { "description": "OK" } },
        "produces": [ "application/vnd.collection+json" ]
      }
    }

在此处输入图像描述

标签: azure-api-management

解决方案


APIM 确实尊重 operationId,它的值用于将 operation id 形成为 Azure 资源。您在 UI 上看到的是操作标题。使用 operationId 作为标题是错误的,因为 Open API 规范说:

用于标识操作的唯一字符串。在 API 中描述的所有操作中,id 必须是唯一的。工具和库可以使用 operationId 来唯一标识一个操作,因此,建议遵循常见的编程命名约定。

人们不希望使用操作标题遵循常见的编程命名约定。因此,“摘要”字段被用于操作标题。

更多详细信息:https ://blogs.msdn.microsoft.com/apimanagement/2018/04/11/important-changes-to-openapi-import-and-export/


推荐阅读