From 3646c06d5d5befb4e45ef2382c2dc238150c32ab Mon Sep 17 00:00:00 2001 From: zhengwr Date: Sun, 14 Jun 2026 00:26:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20B1=20JUEL=20=E8=87=B4?= =?UTF-8?q?=E5=91=BD=E7=BC=BA=E9=99=B7:resolveBindings=20=E8=A7=A6=20FlowS?= =?UTF-8?q?criptBindings.entrySet=20UOE(dogfooding=20=E5=AE=9E=E8=AF=81)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 逐流写 QA dogfooding + 上一提交的 gracefulFlowError 栈日志联合暴露:JuelScriptEngine.resolveBindings 用 SimpleBindings.putAll 合并入参 bindings,而真实服务流传入的是保真层 FlowScriptBindings——其 entrySet/keySet/putAll/size/values 抛 UnsupportedOperationException(仅 get/put/containsKey 实现, 架构设计§1 早记)。致【所有带 conditionExpression 的 EL 写流】在转移求值(${isSave} 等)时崩。 B2 差分用 SimpleBindings(entrySet 正常)故漏检;线性流(通知 OA_NOTICEMANAGE_ADD,无条件转移) 恰好不触发,致先前误判端到端已通。 修:废 resolveBindings 的 putAll 复制;BindingsELResolver 改持 ENGINE/GLOBAL 两层 Bindings, 顶层变量惰性 lookup 仅用 containsKey()+get()(FlowScriptBindings 支持的方法),不复制不遍历。 新增回归测试 (g):FlowScriptBindingsLike(entrySet/putAll 抛 UOE)下 ${x+1}/${isSave}/${x>3}/${nope} 均正确求值。platform-app 9 测试全绿(B2 差分 1376 等价仍不回归)。 Co-Authored-By: Claude Opus 4.8 (1M context) --- .../app/script/juel/JuelScriptEngine.java | 60 ++++++++++--------- .../script/juel/JuelScriptEngineSeamTest.java | 28 +++++++++ 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/platform-app/src/main/java/com/eip4/platform/app/script/juel/JuelScriptEngine.java b/platform-app/src/main/java/com/eip4/platform/app/script/juel/JuelScriptEngine.java index 1f3b25b..7cf3551 100644 --- a/platform-app/src/main/java/com/eip4/platform/app/script/juel/JuelScriptEngine.java +++ b/platform-app/src/main/java/com/eip4/platform/app/script/juel/JuelScriptEngine.java @@ -63,7 +63,12 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable return null; } try { - ELContext elContext = new BindingsELContext(resolveBindings(context)); + // 直接持有 ENGINE/GLOBAL 两层 Bindings,经 ELResolver 惰性 get() 解析顶层变量—— + // 不可 putAll/复制:真实服务流传入的 FlowScriptBindings 的 entrySet/keySet/putAll + // 抛 UnsupportedOperationException(仅 get/put/containsKey 可用),复制会崩(QA dogfooding 实证)。 + ELContext elContext = new BindingsELContext( + context.getBindings(ScriptContext.ENGINE_SCOPE), + context.getBindings(ScriptContext.GLOBAL_SCOPE)); ValueExpression expr = EXPRESSION_FACTORY.createValueExpression(elContext, script, Object.class); return expr.getValue(elContext); } catch (ELException e) { @@ -96,24 +101,6 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable return factory; } - /** - * 合并 ENGINE_SCOPE 与 GLOBAL_SCOPE 的 bindings(ENGINE_SCOPE 优先),作为 EL 顶层变量来源。 - * 与 framework 行为一致:调用方经 {@code evaluate(script, "juel", bindings)} 传入的 - * bindings 落在 ENGINE_SCOPE。 - */ - @SuppressWarnings({"unchecked", "rawtypes"}) - private Map resolveBindings(ScriptContext context) { - SimpleBindings merged = new SimpleBindings(); - Bindings global = context.getBindings(ScriptContext.GLOBAL_SCOPE); - if (global != null) { - merged.putAll(global); - } - Bindings engine = context.getBindings(ScriptContext.ENGINE_SCOPE); - if (engine != null) { - merged.putAll(engine); - } - return (Map) merged; - } private static String readFully(Reader reader) throws ScriptException { if (reader == null) { @@ -162,9 +149,9 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable private final VariableMapper variableMapper = new NoopVariableMapper(); private final FunctionMapper functionMapper = new NoopFunctionMapper(); - private BindingsELContext(Map bindings) { + private BindingsELContext(Bindings engineBindings, Bindings globalBindings) { CompositeELResolver composite = new CompositeELResolver(); - composite.add(new BindingsELResolver(bindings)); + composite.add(new BindingsELResolver(engineBindings, globalBindings)); composite.add(new MapELResolver()); composite.add(new ListELResolver()); composite.add(new ArrayELResolver()); @@ -201,17 +188,34 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable */ private static final class BindingsELResolver extends ELResolver { - private final Map bindings; + private final Bindings engineBindings; + private final Bindings globalBindings; - private BindingsELResolver(Map bindings) { - this.bindings = bindings; + private BindingsELResolver(Bindings engineBindings, Bindings globalBindings) { + this.engineBindings = engineBindings; + this.globalBindings = globalBindings; + } + + /** + * 仅用 {@code get()/containsKey()} 解析顶层变量(ENGINE 优先,缺则 GLOBAL)。 + * 关键:严禁 entrySet/keySet/putAll——真实服务流的 FlowScriptBindings 这些方法抛 + * UnsupportedOperationException,仅 get/put/containsKey 实现(QA dogfooding 实证)。 + */ + private Object lookup(String name) { + if (engineBindings != null && engineBindings.containsKey(name)) { + return engineBindings.get(name); + } + if (globalBindings != null && globalBindings.containsKey(name)) { + return globalBindings.get(name); + } + return null; // 未绑定 → null(保持 JUEL 宽松语义) } @Override public Object getValue(ELContext context, Object base, Object property) { if (base == null && property instanceof String) { context.setPropertyResolved(true); - return bindings.get(property); + return lookup((String) property); } return null; } @@ -220,7 +224,7 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable public Class getType(ELContext context, Object base, Object property) { if (base == null && property instanceof String) { context.setPropertyResolved(true); - Object value = bindings.get(property); + Object value = lookup((String) property); return value == null ? Object.class : value.getClass(); } return null; @@ -230,7 +234,9 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable public void setValue(ELContext context, Object base, Object property, Object value) { if (base == null && property instanceof String) { context.setPropertyResolved(true); - bindings.put((String) property, value); + if (engineBindings != null) { + engineBindings.put((String) property, value); // put 可用 + } } } diff --git a/platform-app/src/test/java/com/eip4/platform/app/script/juel/JuelScriptEngineSeamTest.java b/platform-app/src/test/java/com/eip4/platform/app/script/juel/JuelScriptEngineSeamTest.java index cb34e04..023181f 100644 --- a/platform-app/src/test/java/com/eip4/platform/app/script/juel/JuelScriptEngineSeamTest.java +++ b/platform-app/src/test/java/com/eip4/platform/app/script/juel/JuelScriptEngineSeamTest.java @@ -126,4 +126,32 @@ class JuelScriptEngineSeamTest { assertTrue(result instanceof Number); assertEquals(6, ((Number) result).intValue()); } + + /** + * 模拟保真层 FlowScriptBindings:get/put/containsKey 可用, + * entrySet/keySet/values/size/putAll 抛 UnsupportedOperationException(与真实类一致)。 + */ + static class FlowScriptBindingsLike extends SimpleBindings { + @Override public java.util.Set> entrySet() { throw new UnsupportedOperationException(); } + @Override public java.util.Set keySet() { throw new UnsupportedOperationException(); } + @Override public java.util.Collection values() { throw new UnsupportedOperationException(); } + @Override public int size() { throw new UnsupportedOperationException(); } + @Override public void putAll(java.util.Map m) { throw new UnsupportedOperationException(); } + } + + @Test + @DisplayName("(g) 真实流 FlowScriptBindings(entrySet/putAll 抛 UOE)下仍可求值——仅用 get/containsKey") + void flowScriptBindingsLike_noEntrySet() throws Exception { + // 回归:B1 原 resolveBindings 用 putAll 合并 → 触 FlowScriptBindings.entrySet UOE, + // 致所有带 conditionExpression 的 EL 写流崩(QA dogfooding 实证)。修后只走 get/containsKey。 + FlowScriptBindingsLike b = new FlowScriptBindingsLike(); + b.put("x", 5); + b.put("isSave", Boolean.TRUE); + ScriptEngine engine = new ScriptEngineManager().getEngineByName("juel"); + assertNotNull(engine); + assertEquals(6, ((Number) engine.eval("${x + 1}", b)).intValue()); + assertEquals(Boolean.TRUE, engine.eval("${isSave}", b)); // ${isSave} 转移条件:真实写流核心 + assertEquals(Boolean.TRUE, engine.eval("${x > 3}", b)); + assertNull(engine.eval("${nope}", b)); // 未绑定 → null,不抛、不触 entrySet + } }