APIM - 在消费层验证内容

APIM - validate-content on consumption tier

我最近将我们的 APIM 实例从开发层更改为消费层,并且在验证内容策略中看到了一些奇怪的行为。在开发人员层,此策略将按预期工作,并且 return 出现 400 错误并显示相应的错误消息。

政策如下:

<validate-content unspecified-content-type-action="prevent" max-size="102400" size-exceeded-action="prevent">
   <content type="application/json" validate-as="json" action="prevent" />
</validate-content>

下面是跟踪示例和开发人员层的响应(预期行为):

//Trace
validate-content (0.100 ms)
    {
    "name": "application/json",
    "type": "RequestBody",
    "validationRule": "IncorrectMessage",
    "details": "Body of the request does not conform to the definition skills-POST-request, which is associated with the content type application/json. Property 'nam' has not been defined and the schema does not allow additional properties. Line: 1, Position: 7",
    "action": "Prevented"
}

//Response
HTTP/1.1 400 Bad Request
vary: Origin
    {
    "statusCode": 400,
    "message": "Body of the request does not conform to the definition skills-POST-request, which is associated with the content type application/json. Property 'nam' has not been defined and the schema does not allow additional properties. Line: 1, Position: 7"
}

但是,现在消费层上的相同策略 returns 以下跟踪和响应(不正确的行为):

//Trace
validate-content (4.736 ms)
    {
    "name": "application/json",
    "type": "RequestBody",
    "validationRule": "IncorrectMessage",
    "details": "Body of the request does not conform to the definition skills-POST-request, which is associated with the content type application/json. Property 'nam' has not been defined and the schema does not allow additional properties. Line: 1, Position: 7",
    "action": "Prevented"
}
validate-content (0.714 ms)
    {
    "name": null,
    "type": "RequestBody",
    "validationRule": "ValidationException",
    "details": "Body of the request cannot be validated for the content type application/json. Value cannot be null.\r\nParameter name: key",
    "action": "Prevented"
}
validate-content (2.679 ms)
    {
    "messages": [
        "Value cannot be null.\r\nParameter name: key"
    ]
}

//response
HTTP/1.1 500 Internal Server Error
vary: Origin
    {
    "statusCode": 500,
    "message": "Internal server error",
    "activityId": "b3d76aed-fdf0-4240-a5c1-db49fed82105"
}

这看起来可能是消费层的内容验证策略中的某种错误?

根据 Azure documentation,APIM 中的消费层仅支持 TLS 设置、外部缓存、客户端证书身份验证和 Graph QL API。因此,验证内容 APIM 策略不适用于消费 SKU 上的 APIM 服务 运行。

我向 Microsoft 提出了支持请求,他们确定这是 API 管理中的错误。解决方法是将以下内容添加到策略中:

errors-variable-name="requestBodyValidation"

所以最终的政策现在看起来像:

<validate-content unspecified-content-type-action="prevent" max-size="102400" size-exceeded-action="prevent" errors-variable-name="requestBodyValidation">
   <content type="application/json" validate-as="json" action="prevent" />
</validate-content>