Clean up arguments after executing Script Transformation (#4524)

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
pull/4528/head
jimtng 2024-12-28 21:28:36 +10:00 committed by GitHub
parent 1f9ba2b0ee
commit 8b68aa7441
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 6 deletions

View File

@ -17,6 +17,7 @@ import static org.openhab.core.automation.module.script.profile.ScriptProfileFac
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
@ -185,8 +186,10 @@ public class ScriptTransformationService implements TransformationService, Confi
: scriptEngineContainer.getScriptEngine();
ScriptContext executionContext = engine.getContext();
executionContext.setAttribute("input", source, ScriptContext.ENGINE_SCOPE);
ArrayList<String> injectedParams = null;
if (params != null) {
injectedParams = new ArrayList<>();
for (String param : params.split("&")) {
String[] splitString = param.split("=");
if (splitString.length != 2) {
@ -197,6 +200,7 @@ public class ScriptTransformationService implements TransformationService, Confi
param = URLDecoder.decode(splitString[0], StandardCharsets.UTF_8);
String value = URLDecoder.decode(splitString[1], StandardCharsets.UTF_8);
executionContext.setAttribute(param, value, ScriptContext.ENGINE_SCOPE);
injectedParams.add(param);
}
}
}
@ -210,8 +214,15 @@ public class ScriptTransformationService implements TransformationService, Confi
scriptRecord.compiledScript = compiledScript;
}
Object result = compiledScript != null ? compiledScript.eval() : engine.eval(scriptRecord.script);
return result == null ? null : result.toString();
try {
Object result = compiledScript != null ? compiledScript.eval() : engine.eval(scriptRecord.script);
return result == null ? null : result.toString();
} finally {
if (injectedParams != null) {
injectedParams
.forEach(param -> executionContext.removeAttribute(param, ScriptContext.ENGINE_SCOPE));
}
}
} catch (ScriptException e) {
throw new TransformationException("Failed to execute script.", e);
} catch (IllegalStateException e) {

View File

@ -112,7 +112,6 @@ public class ScriptTransformationServiceTest extends JavaTest {
verify(scriptContext).setAttribute(eq("input"), eq("input"), eq(ScriptContext.ENGINE_SCOPE));
verify(scriptContext).setAttribute(eq("param1"), eq("value1"), eq(ScriptContext.ENGINE_SCOPE));
verify(scriptContext).setAttribute(eq("param2"), eq("value2"), eq(ScriptContext.ENGINE_SCOPE));
verifyNoMoreInteractions(scriptContext);
}
@Test
@ -122,7 +121,6 @@ public class ScriptTransformationServiceTest extends JavaTest {
verify(scriptContext).setAttribute(eq("input"), eq("input"), eq(ScriptContext.ENGINE_SCOPE));
verify(scriptContext).setAttribute(eq("param1"), eq("&amp;"), eq(ScriptContext.ENGINE_SCOPE));
verify(scriptContext).setAttribute(eq("param2"), eq("=value"), eq(ScriptContext.ENGINE_SCOPE));
verifyNoMoreInteractions(scriptContext);
}
@Test
@ -141,7 +139,23 @@ public class ScriptTransformationServiceTest extends JavaTest {
inOrder.verify(scriptContext, times(2)).setAttribute(anyString(), anyString(), eq(ScriptContext.ENGINE_SCOPE));
inOrder.verify((Compilable) scriptEngine).compile(SCRIPT);
inOrder.verify(scriptEngine).eval(SCRIPT);
inOrder.verifyNoMoreInteractions();
}
@Test
public void scriptAttributesRemovedAfterExecution() throws TransformationException, ScriptException {
abstract class CompilableScriptEngine implements ScriptEngine, Compilable {
}
scriptEngine = mock(CompilableScriptEngine.class);
when(scriptEngineContainer.getScriptEngine()).thenReturn(scriptEngine);
when(scriptEngine.getContext()).thenReturn(scriptContext);
InOrder inOrder = inOrder(scriptContext, scriptEngine);
service.transform(SCRIPT_UID + "?param1=value1", "input");
inOrder.verify(scriptEngine).eval(SCRIPT);
inOrder.verify(scriptContext).removeAttribute(eq("param1"), eq(ScriptContext.ENGINE_SCOPE));
}
@Test
@ -150,7 +164,7 @@ public class ScriptTransformationServiceTest extends JavaTest {
verify(scriptContext).setAttribute(eq("input"), eq("input"), eq(ScriptContext.ENGINE_SCOPE));
verify(scriptContext).setAttribute(eq("param1"), eq("value1"), eq(ScriptContext.ENGINE_SCOPE));
verifyNoMoreInteractions(scriptContext);
verify(scriptContext, times(0)).setAttribute(eq("invalid"), any(), eq(ScriptContext.ENGINE_SCOPE));
}
@Test