我正在尝试导入API操作(与Swagger JSON规范)。然而,它抱怨我的URI参数。我有点不明白它在抱怨什么。
下面是我的带有swagger/Open API规范的Terraform代码片段: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
}
}
azurerm_api_management_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在线编辑器 中验证了Spec
- 参数块被明确定义以匹配路径:
{
"name": "abn",
"in": "path",
"required": true,
"schema": {
"type": "number"
}
}
我发现这只发生在URI参数上。对于我的其他带有查询参数的api,这个问题不会发生。
谁能告诉我哪里出了问题?谢谢。它与URI参数一起出现,您也必须在Terraform中指定它:
参考下面的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"
type = "number"
required = true
}
response {
status_code = 200
}
}
在我的审判&错误,它并不在乎查询参数不指定。但是URI参数必须在Open API规范和Terraform资源中通过template_parameter
部分指定。