修复 B1 JUEL 致命缺陷:resolveBindings 触 FlowScriptBindings.entrySet UOE(dogfooding 实证)
逐流写 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) <noreply@anthropic.com>
This commit is contained in:
@@ -63,7 +63,12 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
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);
|
ValueExpression expr = EXPRESSION_FACTORY.createValueExpression(elContext, script, Object.class);
|
||||||
return expr.getValue(elContext);
|
return expr.getValue(elContext);
|
||||||
} catch (ELException e) {
|
} catch (ELException e) {
|
||||||
@@ -96,24 +101,6 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable
|
|||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 合并 ENGINE_SCOPE 与 GLOBAL_SCOPE 的 bindings(ENGINE_SCOPE 优先),作为 EL 顶层变量来源。
|
|
||||||
* 与 framework 行为一致:调用方经 {@code evaluate(script, "juel", bindings)} 传入的
|
|
||||||
* bindings 落在 ENGINE_SCOPE。
|
|
||||||
*/
|
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
||||||
private Map<String, Object> 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 {
|
private static String readFully(Reader reader) throws ScriptException {
|
||||||
if (reader == null) {
|
if (reader == null) {
|
||||||
@@ -162,9 +149,9 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable
|
|||||||
private final VariableMapper variableMapper = new NoopVariableMapper();
|
private final VariableMapper variableMapper = new NoopVariableMapper();
|
||||||
private final FunctionMapper functionMapper = new NoopFunctionMapper();
|
private final FunctionMapper functionMapper = new NoopFunctionMapper();
|
||||||
|
|
||||||
private BindingsELContext(Map<String, Object> bindings) {
|
private BindingsELContext(Bindings engineBindings, Bindings globalBindings) {
|
||||||
CompositeELResolver composite = new CompositeELResolver();
|
CompositeELResolver composite = new CompositeELResolver();
|
||||||
composite.add(new BindingsELResolver(bindings));
|
composite.add(new BindingsELResolver(engineBindings, globalBindings));
|
||||||
composite.add(new MapELResolver());
|
composite.add(new MapELResolver());
|
||||||
composite.add(new ListELResolver());
|
composite.add(new ListELResolver());
|
||||||
composite.add(new ArrayELResolver());
|
composite.add(new ArrayELResolver());
|
||||||
@@ -201,17 +188,34 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable
|
|||||||
*/
|
*/
|
||||||
private static final class BindingsELResolver extends ELResolver {
|
private static final class BindingsELResolver extends ELResolver {
|
||||||
|
|
||||||
private final Map<String, Object> bindings;
|
private final Bindings engineBindings;
|
||||||
|
private final Bindings globalBindings;
|
||||||
|
|
||||||
private BindingsELResolver(Map<String, Object> bindings) {
|
private BindingsELResolver(Bindings engineBindings, Bindings globalBindings) {
|
||||||
this.bindings = bindings;
|
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
|
@Override
|
||||||
public Object getValue(ELContext context, Object base, Object property) {
|
public Object getValue(ELContext context, Object base, Object property) {
|
||||||
if (base == null && property instanceof String) {
|
if (base == null && property instanceof String) {
|
||||||
context.setPropertyResolved(true);
|
context.setPropertyResolved(true);
|
||||||
return bindings.get(property);
|
return lookup((String) property);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -220,7 +224,7 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable
|
|||||||
public Class<?> getType(ELContext context, Object base, Object property) {
|
public Class<?> getType(ELContext context, Object base, Object property) {
|
||||||
if (base == null && property instanceof String) {
|
if (base == null && property instanceof String) {
|
||||||
context.setPropertyResolved(true);
|
context.setPropertyResolved(true);
|
||||||
Object value = bindings.get(property);
|
Object value = lookup((String) property);
|
||||||
return value == null ? Object.class : value.getClass();
|
return value == null ? Object.class : value.getClass();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -230,7 +234,9 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable
|
|||||||
public void setValue(ELContext context, Object base, Object property, Object value) {
|
public void setValue(ELContext context, Object base, Object property, Object value) {
|
||||||
if (base == null && property instanceof String) {
|
if (base == null && property instanceof String) {
|
||||||
context.setPropertyResolved(true);
|
context.setPropertyResolved(true);
|
||||||
bindings.put((String) property, value);
|
if (engineBindings != null) {
|
||||||
|
engineBindings.put((String) property, value); // put 可用
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -126,4 +126,32 @@ class JuelScriptEngineSeamTest {
|
|||||||
assertTrue(result instanceof Number);
|
assertTrue(result instanceof Number);
|
||||||
assertEquals(6, ((Number) result).intValue());
|
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<java.util.Map.Entry<String, Object>> entrySet() { throw new UnsupportedOperationException(); }
|
||||||
|
@Override public java.util.Set<String> keySet() { throw new UnsupportedOperationException(); }
|
||||||
|
@Override public java.util.Collection<Object> values() { throw new UnsupportedOperationException(); }
|
||||||
|
@Override public int size() { throw new UnsupportedOperationException(); }
|
||||||
|
@Override public void putAll(java.util.Map<? extends String, ? extends Object> 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user