You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

azurerm_api_management_api_operation URI参数校验报错问题

问题现象

基于Swagger/OpenAPI JSON规范导入Azure API Management的API操作时,涉及URI路径参数的接口会抛出校验错误,仅配置查询参数的API无同类问题。

相关配置与报错

Terraform API资源配置(含OpenAPI导入逻辑)

resource "azurerm_api_management_api" "sample_api_v2" {
  name                = "sample-api-v2"
  resource_group_name = data.azurerm_resource_group.rg_name
  api_management_name = module.abc01.name
  revision            = "1"
  display_name        = "Sample Data API v2"
  path                = "Sample-data/v2"
  protocols           = ["https"]
  version             = "v2"
  version_set_id      = azurerm_api_management_api_version_set.sample-api-version-set.id
  import {
    content_format = "openapi+json"
    content_value  = <<JSON
    {
    "openapi": "3.0.1",
    "info": {
        "title": "Sample API v2",
        "description": "Sample Data API v2",
        "contact": {
        "name": "John Smith",
        "email": "john.smith@email.com"
        },
        "version": "v2"
    },
    "servers": [
        {
        "url": "https://my-api.example.com/sample-api/v2"
        }
    ],
    "paths": {
        "/businesses/{abn}": {
        "get": {
            "summary": "Get Businesses",
            "description": "Retrieve the abn information of the business with the matching abn number",
            "operationId": "get-business-by-abn",
            "parameters": [
            {
                "name": "abn",
                "in": "path",
                "required": true,
                "schema": {
                "type": "number"
                }
            }
            ],
            "responses": {
            "200": {
                "description": "ABN Found"
            }
            }
        }
        }
    },
    "components": {
        "securitySchemes": {
        "apiKeyQuery": {
            "type": "apiKey",
            "name": "subscription-key",
            "in": "query"
        }
        }
    },
    "security": [
        {
        "apiKeyQuery": []
        }
    ]
    }
    JSON
  }
}

单独定义的API Operation资源配置

resource "azurerm_api_management_api_operation" "get_business_by_abn_v2" {
  operation_id        = "get-business-by-abn"
  api_name            = azurerm_api_management_api.sample_api_v2.name
  api_management_name = module.abc01.name
  resource_group_name = data.azurerm_resource_group.rg_name
  display_name        = "Lookup business by ABN"
  method              = "GET"
  url_template        = "/businesses/{abn}"
  description         = "Lookup a business by ABN Number"
  response {
    status_code = 200
  }
}

部署报错信息

: apimanagement.APIOperationClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="ValidationError" Message="One or more fields contain incorrect values:" Details=[{"code":"ValidationError","message":"All template parameters used in the UriTemplate must be defined in the Operation, and vice-versa.","target":"templateParameters"}]
已完成排查
  • 已在Swagger在线编辑器验证OpenAPI规范合法性,无语法错误
  • OpenAPI规范中已明确定义路径匹配的abn参数,配置如下:
{
   "name": "abn",
   "in": "path",
   "required": true,
   "schema": {
   "type": "number"
   }
}
根因分析

报错核心原因是配置逻辑冲突+参数定义缺失:

  1. 在azurerm_api_management_api资源中配置import块后,Azure会在API创建时自动根据OpenAPI规范生成全量Operation资源,包含路径参数的定义。后续单独声明同ID的azurerm_api_management_api_operation资源,会覆盖导入生成的配置。
  2. 单独声明的Operation资源中,url_template包含路径参数{abn},但没有按照Azure APIM的强校验要求,在资源内通过template_parameter块显式声明该路径参数。OpenAPI里的参数定义仅在导入流程生效,不会自动同步到单独管理的Operation资源中,最终触发模板参数不匹配的校验错误。
  3. 查询参数无此问题是因为APIM不对查询参数做强制的模板参数块声明校验,仅路径参数有该要求。
解决方案

二选一即可:

  • 方案一(推荐):删除单独声明的azurerm_api_management_api_operation资源,所有Operation配置完全通过OpenAPI导入自动生成,无需重复定义,导入流程会自动补全所有路径参数的声明,不会触发校验错误。
  • 方案二:如果需要单独管理Operation资源,要么移除API资源中的import块,要么在声明的Operation资源中补全所有路径参数对应的template_parameter块,修正后的配置示例:
resource "azurerm_api_management_api_operation" "get_business_by_abn_v2" {
  operation_id        = "get-business-by-abn"
  api_name            = azurerm_api_management_api.sample_api_v2.name
  api_management_name = module.abc01.name
  resource_group_name = data.azurerm_resource_group.rg_name
  display_name        = "Lookup business by ABN"
  method              = "GET"
  url_template        = "/businesses/{abn}"
  description         = "Lookup a business by ABN Number"

  # 补全路径对应的模板参数定义
  template_parameter {
    name     = "abn"
    required = true
    type     = "number"
  }

  response {
    status_code = 200
  }
}

内容的提问来源于stack exchange,提问作者RogerIsDead

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 01:18:49