@usearazzo/runner 1.0.1-alpha.38 → 1.0.1-alpha.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usearazzo/runner",
3
- "version": "1.0.1-alpha.38",
3
+ "version": "1.0.1-alpha.39",
4
4
  "description": "Runner for Arazzo Workflows.",
5
5
  "keywords": [
6
6
  "arazzo",
@@ -112,8 +112,8 @@
112
112
  "@swaggerexpert/json-pointer": "^3.0.3",
113
113
  "@swaggerexpert/jsonpath": "^4.1.0",
114
114
  "@swaggerexpert/media-types": "^1.0.3",
115
- "@usearazzo/parser": "1.0.1-alpha.38",
116
- "@usearazzo/resolver": "1.0.1-alpha.38",
115
+ "@usearazzo/parser": "1.0.1-alpha.39",
116
+ "@usearazzo/resolver": "1.0.1-alpha.39",
117
117
  "@xmldom/xmldom": "^0.9.12",
118
118
  "fontoxpath": "^3.34.0",
119
119
  "ramda": "^0.32.0",
@@ -148,5 +148,5 @@
148
148
  "@opentelemetry/sdk-trace-base": "^2.12.0",
149
149
  "swagger-client": "^3.39.0"
150
150
  },
151
- "gitHead": "e6f5880ca7ab04dd04bc1d559fa7574d15fc8203"
151
+ "gitHead": "538aacec09d68c1f11ab2a384c83e3b633c5dd2c"
152
152
  }
@@ -16,6 +16,7 @@ var _RuntimeExpressionEvaluator = _interopRequireDefault(require("../expression/
16
16
  var _ArazzoWorkflowExtractor = _interopRequireDefault(require("../extractor/ArazzoWorkflowExtractor.cjs"));
17
17
  var _ArazzoWorkflowNormalizer = _interopRequireDefault(require("../normalizer/ArazzoWorkflowNormalizer.cjs"));
18
18
  var _OutputResolver = _interopRequireDefault(require("../resolver/OutputResolver.cjs"));
19
+ var _WorkflowInputsResolver = _interopRequireDefault(require("../resolver/WorkflowInputsResolver.cjs"));
19
20
  var _WorkflowParameterResolver = _interopRequireDefault(require("../resolver/WorkflowParameterResolver.cjs"));
20
21
  var _WorkflowExecutionState = _interopRequireDefault(require("../state/WorkflowExecutionState.cjs"));
21
22
  var _StepExecutor = require("./StepExecutor.cjs");
@@ -105,11 +106,6 @@ function settledResult(result) {
105
106
  * @internal
106
107
  */
107
108
 
108
- /**
109
- * One completed `dependsOn` run, remembered with the inputs it was actually
110
- * given — see {@link RunScope.dependencyRuns}.
111
- */
112
-
113
109
  /**
114
110
  * One `executeOptions` entry and the canonical URI of the document its name
115
111
  * points at.
@@ -197,6 +193,11 @@ class WorkflowExecutor {
197
193
  #normalizer;
198
194
  #outputResolver = new _OutputResolver.default();
199
195
  #parameterResolver = new _WorkflowParameterResolver.default();
196
+ #inputsResolver = new _WorkflowInputsResolver.default();
197
+ // the inputs each workflow run ran with, defaults filled in, by its result:
198
+ // what a caller records under `$workflows.{id}.inputs`. Beside the result
199
+ // and not on it, since the events a trace is rebuilt from carry no inputs.
200
+ #inputsByResult = new WeakMap();
200
201
  #stepExecutor;
201
202
  #retryRunner;
202
203
  #interpreter = new _StepTransitionInterpreter.default();
@@ -378,12 +379,11 @@ class WorkflowExecutor {
378
379
  // define never starts, and the event can say where the one it runs is.
379
380
  const extracted = this.#extractWorkflow(locator);
380
381
  const location = locator.document.locationOf(extracted);
381
- const invocation = {
382
+ // without its state: the inputs it runs with are known only once the
383
+ // workflow is normalized, which happens inside `workflow:start`.
384
+ const entered = {
382
385
  workflowId,
383
386
  document: locator.document,
384
- state: new _WorkflowExecutionState.default({
385
- inputs
386
- }),
387
387
  callStack: nested,
388
388
  via,
389
389
  frame
@@ -411,7 +411,7 @@ class WorkflowExecutor {
411
411
  location
412
412
  }, startedAt);
413
413
  }
414
- result = await this.#runInvocation(locator, extracted, invocation, scope, startedAt);
414
+ result = await this.#runInvocation(locator, extracted, entered, inputs, scope, startedAt);
415
415
  } catch (error) {
416
416
  this.#settleThrown({
417
417
  type: 'workflow:settled',
@@ -429,6 +429,9 @@ class WorkflowExecutor {
429
429
  ...frame,
430
430
  status: result.status,
431
431
  settledStatus: result.settledStatus,
432
+ ...(result.inputDefaults === undefined ? {} : {
433
+ inputDefaults: result.inputDefaults
434
+ }),
432
435
  outputs: result.outputs
433
436
  }, startedAt + result.durationMs);
434
437
  }
@@ -441,13 +444,23 @@ class WorkflowExecutor {
441
444
  * normalizes the workflow, checks its definition, runs its dependencies
442
445
  * and its steps, and builds its result.
443
446
  */
444
- async #runInvocation(locator, extracted, invocation, scope, startedAt) {
447
+ async #runInvocation(locator, extracted, entered, inputs, scope, startedAt) {
445
448
  const {
446
449
  workflowId,
447
450
  callStack: nested,
448
451
  frame
449
- } = invocation;
452
+ } = entered;
450
453
  const workflow = await this.#resolveWorkflow(locator, extracted, scope, frame);
454
+ // the defaults of the workflow's `inputs` schema, read from the normalized
455
+ // workflow so that one behind a reference is found.
456
+ const resolved = this.#inputsResolver.resolve(workflow.inputs, inputs);
457
+ const invocation = {
458
+ ...entered,
459
+ state: new _WorkflowExecutionState.default({
460
+ inputs: resolved.inputs
461
+ }),
462
+ inputDefaults: resolved.defaults
463
+ };
451
464
 
452
465
  // validated before any prerequisite runs: a malformed `steps` is an
453
466
  // authoring error, and discovering it only after the dependencies have
@@ -756,6 +769,9 @@ class WorkflowExecutor {
756
769
  const settledAt = this.#now();
757
770
  const shared = {
758
771
  workflowId: invocation.workflowId,
772
+ ...(Object.keys(invocation.inputDefaults).length > 0 ? {
773
+ inputDefaults: invocation.inputDefaults
774
+ } : {}),
759
775
  steps,
760
776
  durationMs: settledAt - startedAt,
761
777
  ...(dependencies.length > 0 ? {
@@ -778,6 +794,7 @@ class WorkflowExecutor {
778
794
  status: outcome.status,
779
795
  settledStatus: outcome.status
780
796
  };
797
+ this.#inputsByResult.set(result, invocation.state.inputs);
781
798
  return result;
782
799
  }
783
800
 
@@ -907,12 +924,11 @@ class WorkflowExecutor {
907
924
  * action's `parameters` (`#115`), resolved again on every firing
908
925
  * against the attempt that fired it — `{}` without them. Its outputs are
909
926
  * recorded via `state.setWorkflow`, which *is* "context transfers back",
910
- * together with the inputs it ran with when the action carries
911
- * `parameters`. Without them, the recorded `inputs` are preserved rather
912
- * than overwritten with `{}` when this workflowId was already recorded
913
- * (by an earlier `dependsOn` prerequisite or sub-workflow step sharing
914
- * the id): this call's own lack of a real inputs value must not erase one
915
- * a different, better-informed caller already gave.
927
+ * together with the inputs it ran with, the defaults of its `inputs`
928
+ * schema included. That holds without `parameters` too, where the
929
+ * reference runs on those defaults alone: the recorded inputs and
930
+ * outputs then describe the same run, and an earlier run sharing the id
931
+ * (a `dependsOn` prerequisite, a sub-workflow step) is replaced whole.
916
932
  * - a `stepId` reference must name a step of the current workflow (checked
917
933
  * lazily, at fire time, via the same lookup a `goto` target uses) and
918
934
  * runs as one more attempt at that step, through
@@ -979,7 +995,7 @@ class WorkflowExecutor {
979
995
  // the key run state is recorded under, whichever document owns it.
980
996
  const refId = result.workflowId;
981
997
  state.setWorkflow(refId, {
982
- inputs: action.hasKey('parameters') ? inputs : state.getWorkflow(refId)?.inputs ?? {},
998
+ inputs: this.#inputsByResult.get(result),
983
999
  outputs: result.outputs
984
1000
  });
985
1001
  const successful = !this.#settledFailed(result);
@@ -1104,11 +1120,10 @@ class WorkflowExecutor {
1104
1120
  // precondition holds once met, and a diamond (two dependents sharing one
1105
1121
  // dependency) must not duplicate its live side effects. Every dependent
1106
1122
  // still reports and reads that one run — including the inputs the run
1107
- // was actually given, not whatever this dependent's own
1108
- // `dependencyInputs` entry would have supplied had it run it.
1123
+ // ran with, not whatever this dependent's own `dependencyInputs` entry
1124
+ // would have supplied had it run it.
1109
1125
  const memoized = scope.dependencyRuns.get(qualifiedId);
1110
- const inputs = memoized?.inputs ?? scope.dependencyInputs[dependency.inputsKey] ?? {};
1111
- const result = memoized?.result ?? (await this.#run(dependency.locator, inputs, scope, callStack, 'dependsOn'));
1126
+ const result = memoized ?? (await this.#run(dependency.locator, scope.dependencyInputs[dependency.inputsKey] ?? {}, scope, callStack, 'dependsOn'));
1112
1127
  results.push(result);
1113
1128
  // recorded whether or not it completed: a failed prerequisite still
1114
1129
  // resolved (possibly partial) outputs, and the parent resolves its own
@@ -1119,7 +1134,7 @@ class WorkflowExecutor {
1119
1134
  // never ran. Keyed by the bare id even for a cross-document dependency:
1120
1135
  // `$workflows.{id}` is the only form an expression can read it back by.
1121
1136
  state.setWorkflow(dependency.locator.workflowId, {
1122
- inputs,
1137
+ inputs: this.#inputsByResult.get(result),
1123
1138
  outputs: result.outputs
1124
1139
  });
1125
1140
  if (this.#events.hasObservers) {
@@ -1136,10 +1151,7 @@ class WorkflowExecutor {
1136
1151
  // only a dependency whose chain settled non-`failed` is remembered as
1137
1152
  // satisfied — a failure, transferred or not, must never let a later
1138
1153
  // dependent skip running it.
1139
- scope.dependencyRuns.set(qualifiedId, {
1140
- result,
1141
- inputs
1142
- });
1154
+ scope.dependencyRuns.set(qualifiedId, result);
1143
1155
  }
1144
1156
  return results;
1145
1157
  }
@@ -1268,7 +1280,7 @@ class WorkflowExecutor {
1268
1280
  const result = await this.#run(locator, inputs, scope, callStack, 'step');
1269
1281
  subWorkflows.push(result);
1270
1282
  state.setWorkflow(subWorkflowId, {
1271
- inputs,
1283
+ inputs: this.#inputsByResult.get(result),
1272
1284
  outputs: result.outputs
1273
1285
  });
1274
1286
 
@@ -7,6 +7,7 @@ import RuntimeExpressionEvaluator from "../expression/RuntimeExpressionEvaluator
7
7
  import ArazzoWorkflowExtractor from "../extractor/ArazzoWorkflowExtractor.mjs";
8
8
  import ArazzoWorkflowNormalizer from "../normalizer/ArazzoWorkflowNormalizer.mjs";
9
9
  import OutputResolver from "../resolver/OutputResolver.mjs";
10
+ import WorkflowInputsResolver from "../resolver/WorkflowInputsResolver.mjs";
10
11
  import WorkflowParameterResolver from "../resolver/WorkflowParameterResolver.mjs";
11
12
  import WorkflowExecutionState from "../state/WorkflowExecutionState.mjs";
12
13
  import { STEP_TARGET_FIELDS } from "./StepExecutor.mjs";
@@ -91,11 +92,6 @@ export function settledResult(result) {
91
92
  * @internal
92
93
  */
93
94
 
94
- /**
95
- * One completed `dependsOn` run, remembered with the inputs it was actually
96
- * given — see {@link RunScope.dependencyRuns}.
97
- */
98
-
99
95
  /**
100
96
  * One `executeOptions` entry and the canonical URI of the document its name
101
97
  * points at.
@@ -183,6 +179,11 @@ class WorkflowExecutor {
183
179
  #normalizer;
184
180
  #outputResolver = new OutputResolver();
185
181
  #parameterResolver = new WorkflowParameterResolver();
182
+ #inputsResolver = new WorkflowInputsResolver();
183
+ // the inputs each workflow run ran with, defaults filled in, by its result:
184
+ // what a caller records under `$workflows.{id}.inputs`. Beside the result
185
+ // and not on it, since the events a trace is rebuilt from carry no inputs.
186
+ #inputsByResult = new WeakMap();
186
187
  #stepExecutor;
187
188
  #retryRunner;
188
189
  #interpreter = new StepTransitionInterpreter();
@@ -364,12 +365,11 @@ class WorkflowExecutor {
364
365
  // define never starts, and the event can say where the one it runs is.
365
366
  const extracted = this.#extractWorkflow(locator);
366
367
  const location = locator.document.locationOf(extracted);
367
- const invocation = {
368
+ // without its state: the inputs it runs with are known only once the
369
+ // workflow is normalized, which happens inside `workflow:start`.
370
+ const entered = {
368
371
  workflowId,
369
372
  document: locator.document,
370
- state: new WorkflowExecutionState({
371
- inputs
372
- }),
373
373
  callStack: nested,
374
374
  via,
375
375
  frame
@@ -397,7 +397,7 @@ class WorkflowExecutor {
397
397
  location
398
398
  }, startedAt);
399
399
  }
400
- result = await this.#runInvocation(locator, extracted, invocation, scope, startedAt);
400
+ result = await this.#runInvocation(locator, extracted, entered, inputs, scope, startedAt);
401
401
  } catch (error) {
402
402
  this.#settleThrown({
403
403
  type: 'workflow:settled',
@@ -415,6 +415,9 @@ class WorkflowExecutor {
415
415
  ...frame,
416
416
  status: result.status,
417
417
  settledStatus: result.settledStatus,
418
+ ...(result.inputDefaults === undefined ? {} : {
419
+ inputDefaults: result.inputDefaults
420
+ }),
418
421
  outputs: result.outputs
419
422
  }, startedAt + result.durationMs);
420
423
  }
@@ -427,13 +430,23 @@ class WorkflowExecutor {
427
430
  * normalizes the workflow, checks its definition, runs its dependencies
428
431
  * and its steps, and builds its result.
429
432
  */
430
- async #runInvocation(locator, extracted, invocation, scope, startedAt) {
433
+ async #runInvocation(locator, extracted, entered, inputs, scope, startedAt) {
431
434
  const {
432
435
  workflowId,
433
436
  callStack: nested,
434
437
  frame
435
- } = invocation;
438
+ } = entered;
436
439
  const workflow = await this.#resolveWorkflow(locator, extracted, scope, frame);
440
+ // the defaults of the workflow's `inputs` schema, read from the normalized
441
+ // workflow so that one behind a reference is found.
442
+ const resolved = this.#inputsResolver.resolve(workflow.inputs, inputs);
443
+ const invocation = {
444
+ ...entered,
445
+ state: new WorkflowExecutionState({
446
+ inputs: resolved.inputs
447
+ }),
448
+ inputDefaults: resolved.defaults
449
+ };
437
450
 
438
451
  // validated before any prerequisite runs: a malformed `steps` is an
439
452
  // authoring error, and discovering it only after the dependencies have
@@ -742,6 +755,9 @@ class WorkflowExecutor {
742
755
  const settledAt = this.#now();
743
756
  const shared = {
744
757
  workflowId: invocation.workflowId,
758
+ ...(Object.keys(invocation.inputDefaults).length > 0 ? {
759
+ inputDefaults: invocation.inputDefaults
760
+ } : {}),
745
761
  steps,
746
762
  durationMs: settledAt - startedAt,
747
763
  ...(dependencies.length > 0 ? {
@@ -764,6 +780,7 @@ class WorkflowExecutor {
764
780
  status: outcome.status,
765
781
  settledStatus: outcome.status
766
782
  };
783
+ this.#inputsByResult.set(result, invocation.state.inputs);
767
784
  return result;
768
785
  }
769
786
 
@@ -893,12 +910,11 @@ class WorkflowExecutor {
893
910
  * action's `parameters` (`#115`), resolved again on every firing
894
911
  * against the attempt that fired it — `{}` without them. Its outputs are
895
912
  * recorded via `state.setWorkflow`, which *is* "context transfers back",
896
- * together with the inputs it ran with when the action carries
897
- * `parameters`. Without them, the recorded `inputs` are preserved rather
898
- * than overwritten with `{}` when this workflowId was already recorded
899
- * (by an earlier `dependsOn` prerequisite or sub-workflow step sharing
900
- * the id): this call's own lack of a real inputs value must not erase one
901
- * a different, better-informed caller already gave.
913
+ * together with the inputs it ran with, the defaults of its `inputs`
914
+ * schema included. That holds without `parameters` too, where the
915
+ * reference runs on those defaults alone: the recorded inputs and
916
+ * outputs then describe the same run, and an earlier run sharing the id
917
+ * (a `dependsOn` prerequisite, a sub-workflow step) is replaced whole.
902
918
  * - a `stepId` reference must name a step of the current workflow (checked
903
919
  * lazily, at fire time, via the same lookup a `goto` target uses) and
904
920
  * runs as one more attempt at that step, through
@@ -965,7 +981,7 @@ class WorkflowExecutor {
965
981
  // the key run state is recorded under, whichever document owns it.
966
982
  const refId = result.workflowId;
967
983
  state.setWorkflow(refId, {
968
- inputs: action.hasKey('parameters') ? inputs : state.getWorkflow(refId)?.inputs ?? {},
984
+ inputs: this.#inputsByResult.get(result),
969
985
  outputs: result.outputs
970
986
  });
971
987
  const successful = !this.#settledFailed(result);
@@ -1090,11 +1106,10 @@ class WorkflowExecutor {
1090
1106
  // precondition holds once met, and a diamond (two dependents sharing one
1091
1107
  // dependency) must not duplicate its live side effects. Every dependent
1092
1108
  // still reports and reads that one run — including the inputs the run
1093
- // was actually given, not whatever this dependent's own
1094
- // `dependencyInputs` entry would have supplied had it run it.
1109
+ // ran with, not whatever this dependent's own `dependencyInputs` entry
1110
+ // would have supplied had it run it.
1095
1111
  const memoized = scope.dependencyRuns.get(qualifiedId);
1096
- const inputs = memoized?.inputs ?? scope.dependencyInputs[dependency.inputsKey] ?? {};
1097
- const result = memoized?.result ?? (await this.#run(dependency.locator, inputs, scope, callStack, 'dependsOn'));
1112
+ const result = memoized ?? (await this.#run(dependency.locator, scope.dependencyInputs[dependency.inputsKey] ?? {}, scope, callStack, 'dependsOn'));
1098
1113
  results.push(result);
1099
1114
  // recorded whether or not it completed: a failed prerequisite still
1100
1115
  // resolved (possibly partial) outputs, and the parent resolves its own
@@ -1105,7 +1120,7 @@ class WorkflowExecutor {
1105
1120
  // never ran. Keyed by the bare id even for a cross-document dependency:
1106
1121
  // `$workflows.{id}` is the only form an expression can read it back by.
1107
1122
  state.setWorkflow(dependency.locator.workflowId, {
1108
- inputs,
1123
+ inputs: this.#inputsByResult.get(result),
1109
1124
  outputs: result.outputs
1110
1125
  });
1111
1126
  if (this.#events.hasObservers) {
@@ -1122,10 +1137,7 @@ class WorkflowExecutor {
1122
1137
  // only a dependency whose chain settled non-`failed` is remembered as
1123
1138
  // satisfied — a failure, transferred or not, must never let a later
1124
1139
  // dependent skip running it.
1125
- scope.dependencyRuns.set(qualifiedId, {
1126
- result,
1127
- inputs
1128
- });
1140
+ scope.dependencyRuns.set(qualifiedId, result);
1129
1141
  }
1130
1142
  return results;
1131
1143
  }
@@ -1254,7 +1266,7 @@ class WorkflowExecutor {
1254
1266
  const result = await this.#run(locator, inputs, scope, callStack, 'step');
1255
1267
  subWorkflows.push(result);
1256
1268
  state.setWorkflow(subWorkflowId, {
1257
- inputs,
1269
+ inputs: this.#inputsByResult.get(result),
1258
1270
  outputs: result.outputs
1259
1271
  });
1260
1272
 
package/src/index.cjs CHANGED
@@ -359,6 +359,12 @@ Object.defineProperty(exports, "WorkflowExecutor", {
359
359
  return _WorkflowExecutor.default;
360
360
  }
361
361
  });
362
+ Object.defineProperty(exports, "WorkflowInputsResolver", {
363
+ enumerable: true,
364
+ get: function () {
365
+ return _WorkflowInputsResolver.default;
366
+ }
367
+ });
362
368
  Object.defineProperty(exports, "WorkflowParameterResolver", {
363
369
  enumerable: true,
364
370
  get: function () {
@@ -434,6 +440,7 @@ var _ArazzoValueResolver = _interopRequireDefault(require("./resolver/ArazzoValu
434
440
  var _StepParameterResolver = _interopRequireDefault(require("./resolver/StepParameterResolver.cjs"));
435
441
  var _OpenAPIOperationParameterResolver = _interopRequireDefault(require("./resolver/OpenAPIOperationParameterResolver.cjs"));
436
442
  var _WorkflowParameterResolver = _interopRequireDefault(require("./resolver/WorkflowParameterResolver.cjs"));
443
+ var _WorkflowInputsResolver = _interopRequireDefault(require("./resolver/WorkflowInputsResolver.cjs"));
437
444
  var _OutputResolver = _interopRequireDefault(require("./resolver/OutputResolver.cjs"));
438
445
  var _RequestBodyResolver = _interopRequireDefault(require("./resolver/RequestBodyResolver.cjs"));
439
446
  var _ActionResolver = _interopRequireDefault(require("./action/ActionResolver.cjs"));
package/src/index.mjs CHANGED
@@ -50,6 +50,7 @@ export { default as ArazzoValueResolver } from "./resolver/ArazzoValueResolver.m
50
50
  export { default as StepParameterResolver } from "./resolver/StepParameterResolver.mjs";
51
51
  export { default as OpenAPIOperationParameterResolver } from "./resolver/OpenAPIOperationParameterResolver.mjs";
52
52
  export { default as WorkflowParameterResolver } from "./resolver/WorkflowParameterResolver.mjs";
53
+ export { default as WorkflowInputsResolver } from "./resolver/WorkflowInputsResolver.mjs";
53
54
  export { default as OutputResolver } from "./resolver/OutputResolver.mjs";
54
55
  export { default as RequestBodyResolver } from "./resolver/RequestBodyResolver.mjs";
55
56
  /* Actions */
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _apidomCore = require("@speclynx/apidom-core");
8
+ var _apidomDatamodel = require("@speclynx/apidom-datamodel");
9
+ var _apidomNsArazzo = require("@speclynx/apidom-ns-arazzo-1");
10
+ var _apidomTraverse = require("@speclynx/apidom-traverse");
11
+ /**
12
+ * The inputs a workflow runs with, as {@link WorkflowInputsResolver} resolves
13
+ * them.
14
+ * @public
15
+ */
16
+
17
+ /**
18
+ * Resolves the inputs a workflow runs with: the inputs it was given, with the
19
+ * `default` of its `inputs` schema filled in for every input it was not given.
20
+ *
21
+ * JSON Schema makes `default` an annotation and leaves applying it to the
22
+ * application, so the rule is the runner's own:
23
+ *
24
+ * - an input is not given when its name is absent or its value is `undefined`;
25
+ * an explicit `null` is given. That holds for an input a step or an action
26
+ * maps too: a parameter whose expression resolves to nothing, such as an
27
+ * optional input passed on, leaves the default in place;
28
+ * - its default is the `default` of the schema its name has under
29
+ * `properties`, looked up through `allOf`, whose members always apply. The
30
+ * schema's own `default` wins over its members', an earlier member over a
31
+ * later one;
32
+ * - an object that was given has the defaults of its own missing members
33
+ * filled in the same way. One that was not given is not created to hold
34
+ * them;
35
+ * - a `default` under `oneOf`, `anyOf`, `if`, `then`, `else` or `not` is not
36
+ * applied: which branch holds depends on the value, so picking one would be
37
+ * a guess;
38
+ * - a `default` of the `inputs` schema itself is not applied: a run always has
39
+ * an inputs object, if an empty one.
40
+ *
41
+ * The schema is read as the workflow normalizer leaves it, dereferenced, so a
42
+ * `default` behind a reference is found. Two things follow from how it
43
+ * dereferences:
44
+ *
45
+ * - a keyword written beside a reference replaces the same keyword of the
46
+ * schema referenced, so `properties` beside a reference hide the
47
+ * `properties` behind it, and their defaults with them;
48
+ * - a reference back into a schema it is already inside is cut there, so in a
49
+ * schema that refers to itself the defaults are filled in down to where it
50
+ * repeats, and a `default` written beside such a reference is lost with it.
51
+ *
52
+ * Neither the schema nor the given inputs are changed. Inputs no default is
53
+ * filled into come back as the object that was given, and so does an object
54
+ * among them; one a default is filled into is copied. A given object that
55
+ * contains itself is not supported.
56
+ * @public
57
+ */
58
+ class WorkflowInputsResolver {
59
+ /**
60
+ * Returns `inputs` with the defaults of `schema` filled in, and those
61
+ * defaults on their own. A workflow without an `inputs` schema, or with one
62
+ * that is not an object, gets its inputs back as given.
63
+ */
64
+ resolve(schema, inputs) {
65
+ const defaults = this.#defaults(this.#applicable(schema), inputs);
66
+ return {
67
+ inputs: this.#filled(inputs, defaults),
68
+ defaults
69
+ };
70
+ }
71
+
72
+ /**
73
+ * The schemas that always apply to a value `schema` describes, the one
74
+ * whose keywords win first: `schema` itself, then the members of its
75
+ * `allOf` as written, each before its own.
76
+ *
77
+ * The traversal goes into `allOf` only: every other keyword either
78
+ * describes a different value (`properties`, `items`) or applies only to
79
+ * some values (`oneOf`, `anyOf`, `if`). It takes each schema once
80
+ * (`skipVisited`), where it is first met: a dereferenced schema shares
81
+ * structure, and a schema referenced in two places would otherwise be walked
82
+ * again at each of them.
83
+ */
84
+ #applicable(schema) {
85
+ if (!(0, _apidomNsArazzo.isJSONSchemaElement)(schema)) return [];
86
+ const applicable = [];
87
+ const collect = path => {
88
+ applicable.push(path.node);
89
+ };
90
+ (0, _apidomTraverse.traverse)(schema, {
91
+ // a schema written in the Arazzo document, or referenced from an
92
+ // OpenAPI 2.0 or 3.0 one
93
+ JSONSchemaElement: collect,
94
+ // a schema referenced from an OpenAPI 3.1 document
95
+ SchemaElement: collect,
96
+ MemberElement(path) {
97
+ if ((0, _apidomCore.toValue)(path.node.key) !== 'allOf') path.skip();
98
+ }
99
+ }, {
100
+ skipVisited: 'skip'
101
+ });
102
+ return applicable;
103
+ }
104
+
105
+ /**
106
+ * The defaults `schemas` declare for what `value` was not given, in the
107
+ * shape of `value`: the default of an input that is missing, and, for an
108
+ * object that was given, the defaults of its own missing members under its
109
+ * name. `schemas` come as {@link WorkflowInputsResolver.#applicable} orders
110
+ * them, so the first one that declares a default for a name wins.
111
+ */
112
+ #defaults(schemas, value) {
113
+ const names = new Set(schemas.flatMap(schema => this.#properties(schema)?.keys() ?? []));
114
+ const defaults = [];
115
+ for (const name of names) {
116
+ // each once: schemas dereferenced from one reference share their
117
+ // `properties`, and would bring the same property schema once each.
118
+ const propertySchemas = [...new Set(schemas.flatMap(schema => this.#applicable(this.#properties(schema)?.get(name))))];
119
+ if (!this.#isGiven(value, name)) {
120
+ const declaring = propertySchemas.find(schema => schema.default !== undefined);
121
+ if (declaring !== undefined) defaults.push([name, (0, _apidomCore.toValue)(declaring.default)]);
122
+ } else if (this.#isPlainObject(value[name])) {
123
+ const members = this.#defaults(propertySchemas, value[name]);
124
+ if (Object.keys(members).length > 0) defaults.push([name, members]);
125
+ }
126
+ }
127
+ // an entry list, so a name like `__proto__` becomes an own property.
128
+ return Object.fromEntries(defaults);
129
+ }
130
+
131
+ /**
132
+ * `value` with `defaults` filled in: `value` itself when there are none,
133
+ * otherwise a copy of it that has them. A default is copied too, so the
134
+ * result shares nothing with `defaults`, and an object that was given is
135
+ * replaced by one that has its members' defaults.
136
+ */
137
+ #filled(value, defaults) {
138
+ if (Object.keys(defaults).length === 0) return value;
139
+ const filled = Object.entries(defaults).map(([name, found]) => [name, this.#isGiven(value, name) ? this.#filled(value[name], found) : structuredClone(found)]);
140
+ // spread, so a name like `__proto__` stays an own property; the copy
141
+ // keeps the prototype of what was given, which may be none.
142
+ return Object.setPrototypeOf({
143
+ ...value,
144
+ ...Object.fromEntries(filled)
145
+ }, Object.getPrototypeOf(value));
146
+ }
147
+
148
+ /**
149
+ * The `properties` of `schema`, when it declares them as a map.
150
+ */
151
+ #properties(schema) {
152
+ return (0, _apidomDatamodel.isObjectElement)(schema.properties) ? schema.properties : undefined;
153
+ }
154
+
155
+ /**
156
+ * Whether `value` was given an input of that name: present, and not
157
+ * `undefined`.
158
+ */
159
+ #isGiven(value, name) {
160
+ return Object.hasOwn(value, name) && value[name] !== undefined;
161
+ }
162
+
163
+ /**
164
+ * Whether `value` is an object written as a literal or parsed from JSON or
165
+ * YAML. Anything else an input can hold (an array, a `Date`, a `Blob`) is
166
+ * left as given: it has no members a schema's `properties` describes.
167
+ */
168
+ #isPlainObject(value) {
169
+ if (typeof value !== 'object' || value === null) return false;
170
+ const prototype = Object.getPrototypeOf(value);
171
+ return prototype === Object.prototype || prototype === null;
172
+ }
173
+ }
174
+ var _default = exports.default = WorkflowInputsResolver;