Skip to content

Commit 905f57f

Browse files
committed
more contortions around #1558
1 parent 5a7c496 commit 905f57f

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java

+34-22
Original file line numberDiff line numberDiff line change
@@ -1025,14 +1025,27 @@ public void init() { // not in constructor because it has to be on Runnable.run(
10251025
AttachResult ar = recurseAndAttach(k, v, seen);
10261026
JS.put(k, ar.value);
10271027
});
1028-
// re-hydrate any functions from caller or background
10291028
vars.forEach((k, v) -> {
1029+
// re-hydrate any functions from caller or background
10301030
AttachResult ar = recurseAndAttach(k, v.getValue(), seen);
10311031
// note that we don't update the vars !
10321032
// if we do, any "bad" out-of-context values will crash the constructor of Variable
10331033
// it is possible the vars are detached / re-used later, so we kind of defer the inevitable
10341034
JS.put(k, ar.value);
10351035
});
1036+
if (runtime.caller.arg != null && runtime.caller.arg.isMap()) {
1037+
// add the call arg as separate "over ride" variables
1038+
Map<String, Object> arg = runtime.caller.arg.getValue();
1039+
arg.forEach((k, v) -> {
1040+
AttachResult ar = recurseAndAttach(k, v, seen);
1041+
try {
1042+
vars.put(k, new Variable(ar.value));
1043+
} catch (Exception e) {
1044+
logger.warn("[*** init call-arg ***] ignoring non-json value: '{}' - {}", k, e.getMessage());
1045+
}
1046+
JS.put(k, ar.value);
1047+
});
1048+
}
10361049
JS.put(KARATE, bridge);
10371050
JS.put(READ, readFunction);
10381051
HttpClient client = runtime.featureRuntime.suite.clientFactory.create(this);
@@ -1096,7 +1109,7 @@ private AttachResult recurseAndAttach(String name, Object o, Set<Object> seen) {
10961109
}
10971110
} catch (Exception e) {
10981111
logger.warn("[*** attach ***] ignoring non-json value: '{}' - {}", name, e.getMessage());
1099-
// here we sweep things under the carpet and hope that graal does not notice !
1112+
// here we try our luck and hope that graal does not notice !
11001113
return AttachResult.dirty(value);
11011114
}
11021115
} else if (o instanceof JsFunction) {
@@ -1196,19 +1209,14 @@ private Object recurseAndDetachAndDeepClone(String name, Object o, Set<Object> s
11961209
if (o instanceof Value) {
11971210
Value value = (Value) o;
11981211
try {
1199-
if (value.canExecute()) {
1200-
if (value.isMetaObject()) { // js function
1201-
return new JsFunction(value);
1202-
} else { // java function
1203-
return new JsExecutable(value);
1204-
}
1212+
if (value.canExecute() && value.isMetaObject()) { // js function
1213+
return new JsFunction(value);
12051214
} else {
1206-
// everything else including java-type references that do not need special attach handling
1207-
o = JsValue.toJava(value);
1215+
return value;
12081216
}
12091217
} catch (Exception e) {
12101218
logger.warn("[*** detach deep ***] ignoring non-json value in callonce / callSingle: '{}' - {}", e.getMessage());
1211-
return null;
1219+
return value; // try our luck
12121220
}
12131221
}
12141222
if (o instanceof List) {
@@ -1301,31 +1309,35 @@ public void setHiddenVariable(String key, Object value) {
13011309

13021310
public void setVariable(String key, Object value) {
13031311
Variable v;
1312+
Object o;
13041313
if (value instanceof Variable) {
13051314
v = (Variable) value;
1315+
o = v.getValue();
13061316
} else {
1307-
v = new Variable(value);
1317+
o = value;
1318+
try {
1319+
v = new Variable(value);
1320+
} catch (Exception e) {
1321+
v = null;
1322+
logger.warn("[*** set variable ***] ignoring non-json value: {} - {}", key, e.getMessage());
1323+
}
1324+
}
1325+
if (v != null) {
1326+
vars.put(key, v);
13081327
}
1309-
vars.put(key, v);
13101328
if (JS != null) {
1311-
JS.put(key, v.getValue());
1329+
JS.put(key, o);
13121330
}
13131331
if (children != null) {
1314-
children.forEach(c -> c.setVariable(key, value));
1332+
children.forEach(c -> c.setVariable(key, o));
13151333
}
13161334
}
13171335

13181336
public void setVariables(Map<String, Object> map) {
13191337
if (map == null) {
13201338
return;
13211339
}
1322-
map.forEach((k, v) -> {
1323-
try {
1324-
setVariable(k, v);
1325-
} catch (Exception e) {
1326-
logger.warn("[*** set variables ***] ignoring non-json value: {} - {}", k, e.getMessage());
1327-
}
1328-
});
1340+
map.forEach((k, v) -> setVariable(k, v));
13291341
}
13301342

13311343
private static Map<String, Variable> copy(Map<String, Variable> source, boolean deep) {

karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,13 @@ protected void logError(String message) {
258258

259259
private Map<String, Object> initMagicVariables() {
260260
Map<String, Object> map = new HashMap();
261-
if (caller.isNone()) { // if feature called via java api
262-
if (caller.arg != null && caller.arg.isMap()) {
263-
engine.setVariables(caller.arg.getValue());
264-
}
265-
} else {
261+
if (!caller.isNone()) {
266262
// karate principle: parent variables are always "visible"
267263
// so we inject the parent magic variables
268264
// but they will be over-written by what is local to this scenario
269265
map.putAll(caller.parentRuntime.magicVariables);
270266
map.put("__arg", caller.arg == null ? null : caller.arg.getValue());
271267
map.put("__loop", caller.getLoopIndex());
272-
if (caller.arg != null && caller.arg.isMap()) {
273-
engine.setVariables(caller.arg.getValue());
274-
}
275268
}
276269
if (scenario.isOutlineExample() && !this.isDynamicBackground()) { // init examples row magic variables
277270
Map<String, Object> exampleData = scenario.getExampleData();

0 commit comments

Comments
 (0)