azure - 为什么在最近的 Azure API 管理升级后,以前稳定且有效的 Liquid 模板会失败?
问题描述
我们有一个 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 策略和请求正文)中进行测试,并将结果总结如下:
根据多次测试和上面的测试结果,我想这可能是升级 APIM 后的一些错误。并且该错误可能与位置或定价层有关(我不确定),因为除了位置和定价层之外,我找不到这些 APIM 之间的任何差异。所以我建议你在另一个 APIM 做同样的工作(具有不同的位置和定价层),它会暂时解决问题。
================================更新================== ===========
我做了一些进一步的测试并找到了解决这个问题的方法。我发现问题是由线路引起的{% assign device_events = body.eventData.events | Where: "eventType", "DEVICE_EVENT" %}
。如果我们不分配body.eventData.events
to device_events
,而是body.eventData.events
直接在 for 循环中使用{% JSONArrayFor event in body.eventData.events %}
. 然后液体模板工作正常。
推荐阅读
- repository - 找不到 com.android.support:appcompat-v7:28.0.3
- html - 在 Safari 中隐藏/放错位置
- python - 在数据框中保持最佳条目
- ffmpeg - ffmpeg 丢失音频流
- woocommerce - Payfast 分期付款
- c++ - 将 C++ std::string 转换为 UTF-16-LE 编码字符串
- spring-security - 带证书的 SSL/TLS 配置
- unit-testing - 朱莉娅:在更改文件时重新运行单元测试
- angular - Angular 5:如果应用程序具有延迟加载功能,应将页眉、页脚和侧边栏组件放在核心模块或共享文件夹中?
- react-native - 如何将本地磁盘上的图片(如相册)复制到图片标签中?