Skip to content

Commit 71709ca

Browse files
authored
Fix (#27)
1 parent 80e4c0d commit 71709ca

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 1.1.2 (March 24, 2020)
2+
3+
### Bug fixes
4+
5+
* Fix subsequent calls to Jsonata Transform error
6+
## Note
7+
* `JsontaTransform.jsonataTransform` now will not change body of input message
8+
19
## 1.1.1 (March 24, 2020)
210

311
### General Changes

lib/jsonataTransform/jsonataTransform.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ const PASSTHROUGH_BODY_PROPERTY = 'elasticio';
88
* @param msg incoming message object that contains ``body`` with payload
99
* @param cfg configuration that is account information and configuration field values
1010
* @param context this of action or trigger, optional
11+
1112
*/
1213
export function jsonataTransform(msg, cfg, context) {
1314
const expression = cfg.expression;
1415
const compiledExpression = jsonata(expression);
1516
if (context && context.getFlowVariables) {
1617
compiledExpression.assign('getFlowVariables', () => context.getFlowVariables());
1718
}
18-
handlePassthrough(msg);
19-
const passthrough = msg.body[PASSTHROUGH_BODY_PROPERTY];
19+
const handledMessage = handlePassthrough(msg);
20+
const passthrough = handledMessage.body[PASSTHROUGH_BODY_PROPERTY];
2021
compiledExpression.assign('getPassthrough', () => passthrough);
21-
return compiledExpression.evaluate(msg.body);
22+
return compiledExpression.evaluate(handledMessage.body);
2223
}
2324

2425
function handlePassthrough(message) {
@@ -27,9 +28,10 @@ function handlePassthrough(message) {
2728
throw new Error(`${PASSTHROUGH_BODY_PROPERTY} property is reserved \
2829
if you are using passthrough functionality`);
2930
}
30-
31-
message.body.elasticio = {};
32-
Object.assign(message.body.elasticio, message.passthrough);
31+
const result = JSON.parse(JSON.stringify(message)); // Otherwise subsequent calls to Jsonata transform with same message would fail
32+
result.body.elasticio = {};
33+
Object.assign(result.body.elasticio, result.passthrough);
34+
return result;
3335
}
3436
return message;
3537
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elastic.io/component-commons-library",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "Library for most common component development cases",
55
"author": {
66
"name": "elastic.io GmbH",

test/jsonataTransform/jsonataTransform.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,31 @@ describe('Transformation test', () => {
6969
};
7070
const result = JsonataTransform.jsonataTransform(msg, {
7171
expression: '$getFlowVariables()',
72-
}, { getFlowVariables: () => flowVariables});
72+
}, { getFlowVariables: () => flowVariables });
7373
expect(result).to.deep.equal(flowVariables);
7474
});
75+
it('should handle 2 expressions on same message if dontThrowError=true', () => {
76+
const msg = eioUtils.newMessageWithBody({
77+
first: 'Renat',
78+
last: 'Zubairov',
79+
});
80+
msg.passthrough = {
81+
ps: 'psworks',
82+
};
83+
const first = JsonataTransform.jsonataTransform(msg, {
84+
expression: '$getPassthrough()',
85+
}, null);
86+
msg.passthrough = { test: 'test' };
87+
const second = JsonataTransform.jsonataTransform(msg, {
88+
expression: '$getPassthrough()',
89+
}, null);
90+
91+
expect(first).to.deep.equal({
92+
ps: 'psworks',
93+
});
94+
expect(second).to.deep.equal({
95+
test: 'test',
96+
});
97+
expect(msg.body.elasticio).to.be.undefined;
98+
});
7599
});

0 commit comments

Comments
 (0)