azure - 为什么之前稳定且工作的 Liquid 模板在最近的 Azure API 管理升级后会失败?

标签 azure liquid azure-api-management dotliquid liquid-template

我们有一个 Azure API 管理端点,它接收以下格式的请求:

{
    "messageType": "EVENT",
    "eventData": {
        "installedApp": {
            "installedAppId": "xxx",
            "locationId": "yyy"
        },
        "events": [
            {
                "eventTime": "2020-11-13T13:14:50.8011105+00:00",
                "eventType": "DEVICE_EVENT",
                "deviceEvent": {
                    "eventId": "3a08b3f3-25b1-11eb-962f-975d499d1166",
                    "locationId": "yyy",
                    "ownerId": "a975533a-a1ae-49f7-88f1-94368bd4d605",
                    "ownerType": "LOCATION",
                    "deviceId": "c3fdc7c6-08f2-4ba3-92b3-0cdfa2b141f5",
                    "componentId": "main",
                    "capability": "motionSensor",
                    "attribute": "motion",
                    "value": "inactive",
                    "valueType": "string",
                    "stateChange": true,
                    "data": {},
                    "subscriptionName": "all_motion_sub"
                }
            }
        ]
    }
}

它通过 Liquid 模板传递它们:

<set-body template="liquid">{
    "id": "{{context.Variables["RequestId"]}}",
    "API": "SmartThings",
    "InstalledAppId": "{{body.eventData.installedApp.installedAppId}}",
    "LocationId": "{{body.eventData.installedApp.locationId}}",
    "DeviceEvents":[
        {% assign device_events = body.eventData.events | Where: "eventType", "DEVICE_EVENT" %}
        {% JSONArrayFor event in device_events %}
        {
            "EventId": "{{event.deviceEvent.eventId}}",
            "LocationId": "{{event.deviceEvent.locationId}}",
            "DeviceId": "{{event.deviceEvent.deviceId}}",
            "ComponentId": "{{event.deviceEvent.componentId}}",
            "Capability": "{{event.deviceEvent.capability}}",
            "Attribute": "{{event.deviceEvent.attribute}}",
            "Value": "{{event.deviceEvent.value}}",
            "StateChange": {{event.deviceEvent.stateChange}},
            "EventTime": "{{event.eventTime | Date: "yyyy-MM-ddTHH:mm:sszzz" | Default: context.Variables["RequestDateTime"] }}"
        }
        {% endJSONArrayFor  %}
    ],
    "EventTime": "{{context.Variables["RequestDateTime"]}}"
}</set-body>

并生成一个输出,发送到逻辑应用进行进一步处理:

{
    "id": "d5e2a032-14b3-40ca-9c6b-4e13f8d2285c",
    "API": "SmartThings",
    "InstalledAppId": "xxx",
    "LocationId": "yyy",
    "DeviceEvents": [
        {
            "EventId": "3a08b3f3-25b1-11eb-962f-975d499d1166",
            "LocationId": "yyy",
            "DeviceId": "c3fdc7c6-08f2-4ba3-92b3-0cdfa2b141f5",
            "ComponentId": "main",
            "Capability": "motionSensor",
            "Attribute": "motion",
            "Value": "inactive",
            "StateChange": true,
            "EventTime": "2020-11-13T13:14:50.8011105+00:00"
        }
    ],
    "EventTime": "2020-11-13T13:14:50.8011105+00:00"
}

直到 2020 年 11 月 11 日 23:00Z 左右,该功能按预期运行,并且已经在生产中运行了几个月。从那时开始,Liquid 映射开始失败,而是生成:

{
    "id": "2c93647c-f9ef-4747-adfb-985805a71f0c",
    "API": "SmartThings",
    "InstalledAppId": "xxx",
    "LocationId": "yyy",
    "DeviceEvents": [
        {
            "EventId": "",
            "LocationId": "",
            "DeviceId": "",
            "ComponentId": "",
            "Capability": "",
            "Attribute": "",
            "Value": "",
            "StateChange": ,
            "EventTime": "2020-11-13T13:14:50.8011105+00:00"
        }
    ],
    "EventTime": "2020-11-13T13:14:50.8011105+00:00"
}

我们在日志中安排了从周四午夜开始的“升级 API 管理”维护事件,因此看起来存在某种重大更改。

什么变化导致了这种情况,我们如何解决它?

最佳答案

对于这个问题,我在我这边测试了一下,也重现了你的情况。 APIM中的液体模板似乎存在错误。重现您的问题后,我在另一个 APIM 中进行测试,但没有显示相同的问题,液体模板在该 APIM 中工作正常。然后我在多个 APIM 中进行测试(具有相同的 set-body 策略和请求正文)并将结果总结如下:

enter image description here

根据多次测试以及上面的测试结果,我猜测可能是升级APIM后出现的一些bug。该错误可能与位置或定价层相关(我不确定),因为除了位置和定价层之外,我找不到这些 APIM 之间的任何差异。所以我建议您在另一个 APIM 中做同样的工作(具有不同的位置和定价等级),这将暂时解决问题。

==================================更新======== =====================

我做了一些进一步的测试并找到了解决此问题的方法。我发现问题是由行 {% allocate device_events = body.eventData.events |其中:“eventType”,“DEVICE_EVENT”%}。如果我们不将 body.eventData.events 分配给 device_events,而是直接在 for 循环中使用 body.eventData.events,如 {% JSONArrayFor body.eventData.events 中的事件 %}。然后液体模板就可以正常工作了。

所以我们可以删除“赋值”行并在 for 循环中执行“where”条件。请引用下面我的液体模板: enter image description here

关于azure - 为什么之前稳定且工作的 Liquid 模板在最近的 Azure API 管理升级后会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64821883/

相关文章:

java - 在 Weblogic 上部署 OpenID Azure AD java Web 应用程序时出现 NoClassDefFoundError

windows - Azure 教程 - 如何使用本地 Blob 存储

ruby - Shopify Liquid 语法 - {%- assign [some_var] = [some_val] -%} 和 {% assign [some_var] = [some_val] %} 有什么区别

ruby - 无法从 Jekyll 插件获取页面数据

azure - azure apim 网关 URL 的 HTTP 到 HTTPS 重定向

azure - Azure API 管理中的重定向策略

Azure Function 应用程序还是 Web API?

azure - 修复子网缺少所需委派的问题

javascript - 显示默认展开的折叠过滤器

azure-api-management - 我们如何禁用 Azure Api 管理开发人员门户(旧版和新版)