繁体   English   中英

在最近的 Azure API 管理升级后,为什么以前稳定且有效的 Liquid 模板会失败?

[英]Why would a previously stable and working Liquid template fail after the most recent Azure API Management upgrade?

我们有一个 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:00 左右,这都按预期工作,并且已经在生产中工作了几个月。 从那时开始,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 策略和请求正文)中进行测试并将结果总结如下:

在此处输入图片说明

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

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

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

因此,我们可以删除“assign”这一行并在for循环中执行“where”条件。 请参考我下面的液体模板: 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM