使用 swagger 将模板部署到 Azure API 管理失败,'path' 不能为空

Template deployment to Azure API management with swagger fails with 'path' must not be empty

我正在尝试使用 swagger 导入功能在 azure API 管理中创建一个 API 和操作,使用从 https://docs.microsoft.com/en-us/azure/templates/microsoft.apimanagement/2018-01-01/service/apis

的文档派生的模板

每次我使用 Azure 资源管理器模板将 API 部署到 Azure API 管理时,我都会收到错误 'path' must not be empty。我究竟做错了什么?路径绝对不能为空!

对于此示例,您可以只使用任何有效的 swagger 文件内容,例如 https://petstore.swagger.io/v2/swagger.json

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "apim_name": {
            "type": "string"
        },
        "api_name": {
            "type": "string"
        },
        "swagger_json": {
            "type": "string"
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.ApiManagement/service/apis",
            "name": "[concat(parameters('apim_name'), '/' ,parameters('api_name'))]",
            "apiVersion": "2018-06-01-preview",
            "properties": {
                "displayName": "Pet Store",
                "description": "Cool api def",
                "serviceUrl": "https://petstore.swagger.io/v2",
                "path": "petstore",
                "protocols": [
                    "https"
                ],
                "authenticationSettings": {
                    "oAuth2": null,
                    "openid": null,
                    "subscriptionKeyRequired": true
                },
                "subscriptionKeyParameterNames": {
                    "header": "Ocp-Apim-Subscription-Key",
                    "query": "subscription-key"
                },
                "contentValue": "[parameters('swagger_json')]",
                "contentFormat": "swagger-json"
            }
        }
    ]
}

似乎 API 管理资源管理器 API 在使用 swagger 导入功能时对参数很挑剔,文档和错误消息有点缺乏。

秘密在于 swagger 文件定义替换了您通常会在模板中为 API 传递的大部分属性,因此您需要一个大大简化的模板,如下所示。

希望这对其他人有帮助!

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "apim_name": {
            "type": "string"
        },
        "api_name": {
            "type": "string"
        },
        "swagger_json": {
            "type": "string"
        }
    },
    "resources": [
        {
            "type": "Microsoft.ApiManagement/service/apis",
            "name": "[concat(parameters('apim_name'), '/' ,parameters('api_name'))]",
            "apiVersion": "2018-06-01-preview",
            "properties": {
                "path": "petstore",
                "contentValue": "[parameters('swagger_json')]",
                "contentFormat": "swagger-json"
            }
        }
    ]
}