[英]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 %}
。 然后液體模板工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.