Azure标准逻辑应用中DotLiquid空值/空字符串校验异常问题
Azure标准逻辑应用中DotLiquid的空值校验Bug问题及规避方案
问题场景
在Azure标准逻辑应用中使用Liquid模板时,空值/空字符串的校验逻辑表现与LiquidJS、在线Liquid测试环境不一致。
校验逻辑代码:
{% assign itemExists = content.existing != null and content.existing.UID != null and content.existing.UID != "" -%}
后续通过该变量生成GraphQL代码:
{% if itemExists -%} _a0: updateFoo(input: { UID: "{{ content.existing.UID }}" {% else -%} _a0: insertFoo(input: { {% endif -%}
实际问题:当content.existing.UID为空字符串""时,模板仍会进入if分支,生成包含空UID的updateFoo语句,且已通过逻辑应用运行历史确认UID确实为空。但相同模板和输入在LiquidJS的Jasmine测试中表现正常。
问题定位(更新1)
仅在将校验逻辑赋值给变量后再用于if判断时出现异常。例如以下测试代码:
{% assign test = "asdf" -%} {% assign isNullOrWhitespace = test == null or test == empty %} isNullOrWhitespace is: {{ isNullOrWhitespace }}
预期输出为isNullOrWhitespace is: false,但实际输出为isNullOrWhitespace is: asdf,说明布尔表达式未被正确求值,变量直接被赋值为表达式左侧的内容。
确认Bug
这是DotLiquid的已知Bug,对应编号537的问题:当将布尔逻辑表达式赋值给变量时,表达式不会被正确计算为布尔值,而是直接返回表达式左侧的操作数,导致逻辑判断完全失效。
规避方案
方案1:直接在if标签中写入完整校验逻辑
跳过变量赋值步骤,将校验逻辑直接放在if条件中,避免触发Bug:
{% if content.existing != null and content.existing.UID != null and content.existing.UID != "" -%} _a0: updateFoo(input: { UID: "{{ content.existing.UID }}" {% else -%} _a0: insertFoo(input: { {% endif -%}
方案2:拆分校验逻辑为分步单一判断
将复合逻辑拆分为多个单一判断的变量赋值,每一步仅做一个逻辑判断,避免复合表达式触发Bug:
{% assign hasExistingItem = content.existing != null %} {% assign hasValidUID = hasExistingItem and content.existing.UID != null %} {% assign uidIsNotEmpty = hasValidUID and content.existing.UID != "" %} {% if uidIsNotEmpty -%} _a0: updateFoo(input: { UID: "{{ content.existing.UID }}" {% else -%} _a0: insertFoo(input: { {% endif -%}
内容的提问来源于stack exchange,提问作者thinkOfaNumber
相关产品推荐
相关产品推荐

