Bläddra i källkod

fix: Aggregate rejects outputField values that collide with its own outputs

Also strengthens the grouped-aggregate fixture to pin group keys and
membership, and adds a dedicated error fixture for the reserved outputField
names.
fszontagh 1 månad sedan
förälder
incheckning
a9d72cd838
3 ändrade filer med 30 tillägg och 1 borttagningar
  1. 5 0
      nodes/core/aggregate.js
  2. 17 0
      tests/nodes/aggregate-errors.json
  3. 8 1
      tests/nodes/split-aggregate.json

+ 5 - 0
nodes/core/aggregate.js

@@ -57,6 +57,11 @@ async function execute(config, input, context) {
     const groupBy = config.groupBy;
     const fieldToAggregate = config.fieldToAggregate;
 
+    if (outputField === 'count' || outputField === 'groups') {
+        throw new Error('Aggregate: outputField cannot be "' + outputField +
+            '", which is a reserved output name for this node. Pick another name.');
+    }
+
     let items;
     if (Array.isArray(inputField)) {
         items = inputField;

+ 17 - 0
tests/nodes/aggregate-errors.json

@@ -0,0 +1,17 @@
+{
+  "name": "verify-aggregate-errors",
+  "nodes": [
+    {"id": "n1", "name": "Trigger", "type": "click-trigger", "position": {"x": 0, "y": 0}, "config": {}},
+    {"id": "n2", "name": "Fixture", "type": "code", "position": {"x": 0, "y": 100},
+     "config": {"code": "return { items: [{sku: 'a'}, {sku: 'b'}] };"}},
+    {"id": "n3", "name": "AggregateReservedName", "type": "aggregate", "position": {"x": 0, "y": 200},
+     "config": {"inputField": "data.result.items", "outputField": "count"}}
+  ],
+  "connections": [
+    {"sourceNodeId": "n1", "sourceOutput": "main", "targetNodeId": "n2", "targetInput": "data"},
+    {"sourceNodeId": "n2", "sourceOutput": "main", "targetNodeId": "n3", "targetInput": "data"}
+  ],
+  "expect": {
+    "n3": {"status": "failed"}
+  }
+}

+ 8 - 1
tests/nodes/split-aggregate.json

@@ -20,6 +20,13 @@
   "expect": {
     "n3": {"status": "completed", "output": {"count": 3, "items": [{"sku": "a"}, {"sku": "b"}, {"sku": "c"}]}},
     "n4": {"status": "completed", "output": {"count": 3, "skus": ["a", "b", "c"]}},
-    "n5": {"status": "completed", "output": {"count": 3, "groups": 2}}
+    "n5": {"status": "completed", "output": {
+      "count": 3,
+      "groups": 2,
+      "items": [
+        {"key": "x", "count": 2, "items": [{"sku": "a"}, {"sku": "c"}]},
+        {"key": "y", "count": 1, "items": [{"sku": "b"}]}
+      ]
+    }}
   }
 }