Skip to content

Commit 4599da1

Browse files
authored
Merge pull request #1034 from telefonicaid/task/remove_casts_by_type_after_apply_expression
remove cast by type after apply JEXL expression
2 parents 856a050 + dfdfbcb commit 4599da1

File tree

8 files changed

+67
-239
lines changed

8 files changed

+67
-239
lines changed

CHANGES_NEXT_RELEASE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- Fix: Do not transform attribute value using attribute type after apply expression plugin JEXL (#1036)
12
- Fix: check entityId is valid after apply expression in multientity plugin (#1039)
23
- Fix: check access to data in fillService facility
34
- Fix: use expressionLanguage defined in group is not defined at device level (#1027)

lib/plugins/jexlParser.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,6 @@ function expressionApplier(context, typeInformation) {
125125
}
126126

127127
newAttribute.value = applyExpression(attribute.expression, context, typeInformation);
128-
129-
if (attribute.type === 'Number' && isFloat(newAttribute.value)) {
130-
newAttribute.value = Number.parseFloat(newAttribute.value);
131-
} else if (attribute.type === 'Number' && !Number.isNaN(Number.parseInt(newAttribute.value))) {
132-
newAttribute.value = Number.parseInt(newAttribute.value);
133-
} else if (attribute.type === 'Boolean') {
134-
newAttribute.value = newAttribute.value === 'true' || newAttribute.value === '1';
135-
} else if (attribute.type === 'None') {
136-
newAttribute.value = null;
137-
} else if (attribute.type === 'Text' || attribute.type === 'String') {
138-
newAttribute.value = String(newAttribute.value);
139-
} else if (
140-
attribute.type.toLowerCase().includes('structuredvalue') ||
141-
attribute.type.toLowerCase().startsWith('geo') ||
142-
attribute.type.toLowerCase().includes('json')
143-
) {
144-
newAttribute.value = newAttribute.value;
145-
} else {
146-
newAttribute.value = String(newAttribute.value);
147-
}
148128
return newAttribute;
149129
};
150130
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"contextElements": [
3+
{
4+
"type": "Light",
5+
"isPattern": "false",
6+
"id": "light1",
7+
"attributes": [
8+
{
9+
"name": "pressure",
10+
"type": "Hgmm",
11+
"value": 1040
12+
}
13+
]
14+
}
15+
],
16+
"updateAction": "UPDATE"
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"contextElements": [
3+
{
4+
"type": "WeatherStation",
5+
"isPattern": "false",
6+
"id": "ws1",
7+
"attributes": [
8+
{
9+
"name": "pressure",
10+
"type": "Hgmm",
11+
"value": 1040
12+
},
13+
{
14+
"name": "humidity",
15+
"type": "Percentage",
16+
"value": "12"
17+
},
18+
{
19+
"name": "weather",
20+
"type": "Summary",
21+
"value": "Humidity 6 and pressure 1040"
22+
}
23+
]
24+
}
25+
],
26+
"updateAction": "UPDATE"
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"contextElements": [
3+
{
4+
"type": "WeatherStation",
5+
"isPattern": "false",
6+
"id": "ws1",
7+
"attributes": [
8+
{
9+
"name": "pressure",
10+
"type": "Hgmm",
11+
"value": 1040
12+
}
13+
]
14+
}
15+
],
16+
"updateAction": "UPDATE"
17+
}

test/unit/expressions/expressionCombinedTransformations-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ describe('Combine Jexl and legacy expressions (default JEXL)', function () {
192192
.matchHeader('fiware-servicepath', 'gardens')
193193
.post(
194194
'/v1/updateContext',
195-
utils.readExampleFile('./test/unit/examples/contextRequests/updateContextExpressionPlugin3.json')
195+
utils.readExampleFile('./test/unit/examples/contextRequests/updateContextExpressionPlugin8.json')
196196
)
197197
.reply(
198198
200,
@@ -285,7 +285,7 @@ describe('Combine Jexl and legacy expressions (default Legacy)', function () {
285285
.matchHeader('fiware-servicepath', 'gardens')
286286
.post(
287287
'/v1/updateContext',
288-
utils.readExampleFile('./test/unit/examples/contextRequests/updateContextExpressionPlugin3.json')
288+
utils.readExampleFile('./test/unit/examples/contextRequests/updateContextExpressionPlugin8.json')
289289
)
290290
.reply(
291291
200,

test/unit/expressions/jexlBasedTransformations-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ describe('Javascript Expression Language (JEXL) based transformations plugin ',
158158
.matchHeader('fiware-servicepath', 'gardens')
159159
.post(
160160
'/v1/updateContext',
161-
utils.readExampleFile('./test/unit/examples/contextRequests/updateContextExpressionPlugin1.json')
161+
utils.readExampleFile('./test/unit/examples/contextRequests/updateContextExpressionPlugin6.json')
162162
)
163163
.reply(
164164
200,
@@ -236,7 +236,7 @@ describe('Javascript Expression Language (JEXL) based transformations plugin ',
236236
.matchHeader('fiware-servicepath', 'gardens')
237237
.post(
238238
'/v1/updateContext',
239-
utils.readExampleFile('./test/unit/examples/contextRequests/updateContextExpressionPlugin2.json')
239+
utils.readExampleFile('./test/unit/examples/contextRequests/updateContextExpressionPlugin7.json')
240240
)
241241
.reply(
242242
200,
@@ -313,7 +313,7 @@ describe('Javascript Expression Language (JEXL) based transformations plugin ',
313313
.matchHeader('fiware-servicepath', 'gardens')
314314
.post(
315315
'/v1/updateContext',
316-
utils.readExampleFile('./test/unit/examples/contextRequests/updateContextExpressionPlugin3.json')
316+
utils.readExampleFile('./test/unit/examples/contextRequests/updateContextExpressionPlugin8.json')
317317
)
318318
.reply(
319319
200,

test/unit/ngsiv2/expressions/jexlBasedTransformations-test.js

Lines changed: 0 additions & 214 deletions
Original file line numberDiff line numberDiff line change
@@ -385,41 +385,6 @@ describe('Java expression language (JEXL) based transformations plugin', functio
385385
});
386386
});
387387

388-
describe('When an update comes for attributes with string expression and type integer', function () {
389-
// Case: Update for an integer attribute with string expression
390-
const values = [
391-
{
392-
name: 'e',
393-
type: 'Number',
394-
value: 52
395-
}
396-
];
397-
398-
beforeEach(function () {
399-
nock.cleanAll();
400-
401-
contextBrokerMock = nock('http://192.168.1.1:1026')
402-
.matchHeader('fiware-service', 'smartgondor')
403-
.matchHeader('fiware-servicepath', 'gardens')
404-
.post(
405-
'/v2/entities/ws1/attrs',
406-
utils.readExampleFile(
407-
'./test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin11.json'
408-
)
409-
)
410-
.query({ type: 'WeatherStation' })
411-
.reply(204);
412-
});
413-
414-
it('should apply the expression before sending the values', function (done) {
415-
iotAgentLib.update('ws1', 'WeatherStationMultiple', '', values, function (error) {
416-
should.not.exist(error);
417-
contextBrokerMock.done();
418-
done();
419-
});
420-
});
421-
});
422-
423388
describe('When an update comes for attributes without expressions and type float', function () {
424389
// Case: Update for a Float attribute without expressions
425390

@@ -492,42 +457,6 @@ describe('Java expression language (JEXL) based transformations plugin', functio
492457
});
493458
});
494459

495-
describe('When an update comes for attributes with string expressions and type float', function () {
496-
// Case: Update for a Float attribute with string expression
497-
498-
const values = [
499-
{
500-
name: 'e',
501-
type: 'Number',
502-
value: 0.44
503-
}
504-
];
505-
506-
beforeEach(function () {
507-
nock.cleanAll();
508-
509-
contextBrokerMock = nock('http://192.168.1.1:1026')
510-
.matchHeader('fiware-service', 'smartgondor')
511-
.matchHeader('fiware-servicepath', 'gardens')
512-
.post(
513-
'/v2/entities/ws1/attrs',
514-
utils.readExampleFile(
515-
'./test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin3.json'
516-
)
517-
)
518-
.query({ type: 'WeatherStation' })
519-
.reply(204);
520-
});
521-
522-
it('should apply the expression before sending the values', function (done) {
523-
iotAgentLib.update('ws1', 'WeatherStationMultiple', '', values, function (error) {
524-
should.not.exist(error);
525-
contextBrokerMock.done();
526-
done();
527-
});
528-
});
529-
});
530-
531460
describe('When an update comes for attributes without expressions and NULL type', function () {
532461
// Case: Update for a Null attribute without expression
533462

@@ -564,78 +493,6 @@ describe('Java expression language (JEXL) based transformations plugin', functio
564493
});
565494
});
566495

567-
describe('When an update comes for attributes with numeric expressions and NULL type', function () {
568-
// Case: Update for a Null attribute with arithmetic expression
569-
570-
const values = [
571-
{
572-
name: 'a',
573-
type: 'None',
574-
value: null
575-
}
576-
];
577-
578-
beforeEach(function () {
579-
nock.cleanAll();
580-
581-
contextBrokerMock = nock('http://192.168.1.1:1026')
582-
.matchHeader('fiware-service', 'smartgondor')
583-
.matchHeader('fiware-servicepath', 'gardens')
584-
.post(
585-
'/v2/entities/ws1/attrs',
586-
utils.readExampleFile(
587-
'./test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin5.json'
588-
)
589-
)
590-
.query({ type: 'WeatherStation' })
591-
.reply(204);
592-
});
593-
594-
it('should apply the expression before sending the values', function (done) {
595-
iotAgentLib.update('ws1', 'WeatherStation', '', values, function (error) {
596-
should.not.exist(error);
597-
contextBrokerMock.done();
598-
done();
599-
});
600-
});
601-
});
602-
603-
describe('When an update comes for attributes with string expressions and NULL type', function () {
604-
// Case: Update for a Null attribute with string expression
605-
606-
const values = [
607-
{
608-
name: 'a',
609-
type: 'None',
610-
value: null
611-
}
612-
];
613-
614-
beforeEach(function () {
615-
nock.cleanAll();
616-
617-
contextBrokerMock = nock('http://192.168.1.1:1026')
618-
.matchHeader('fiware-service', 'smartgondor')
619-
.matchHeader('fiware-servicepath', 'gardens')
620-
.post(
621-
'/v2/entities/ws1/attrs',
622-
utils.readExampleFile(
623-
'./test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin5.json'
624-
)
625-
)
626-
.query({ type: 'WeatherStation' })
627-
.reply(204);
628-
});
629-
630-
it('should apply the expression before sending the values', function (done) {
631-
iotAgentLib.update('ws1', 'WeatherStationMultiple', '', values, function (error) {
632-
should.not.exist(error);
633-
contextBrokerMock.done();
634-
done();
635-
});
636-
});
637-
});
638-
639496
describe('When an update comes for attributes without expressions and Boolean type', function () {
640497
// Case: Update for a Boolean attribute without expression
641498

@@ -672,77 +529,6 @@ describe('Java expression language (JEXL) based transformations plugin', functio
672529
});
673530
});
674531

675-
describe('When an update comes for attributes with numeric expressions and Boolean type', function () {
676-
// Case: Update for a Boolean attribute with arithmetic expression
677-
678-
const values = [
679-
{
680-
name: 'u',
681-
type: 'Boolean',
682-
value: true
683-
}
684-
];
685-
686-
beforeEach(function () {
687-
nock.cleanAll();
688-
689-
contextBrokerMock = nock('http://192.168.1.1:1026')
690-
.matchHeader('fiware-service', 'smartgondor')
691-
.matchHeader('fiware-servicepath', 'gardens')
692-
.post(
693-
'/v2/entities/ws1/attrs',
694-
utils.readExampleFile(
695-
'./test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin10.json'
696-
)
697-
)
698-
.query({ type: 'WeatherStation' })
699-
.reply(204);
700-
});
701-
702-
it('should apply the expression before sending the values', function (done) {
703-
iotAgentLib.update('ws1', 'WeatherStation', '', values, function (error) {
704-
should.not.exist(error);
705-
contextBrokerMock.done();
706-
done();
707-
});
708-
});
709-
});
710-
711-
describe('When an update comes for attributes with string expressions and Boolean type', function () {
712-
// Case: Update for a Boolean attribute with string expression
713-
const values = [
714-
{
715-
name: 'u',
716-
type: 'Boolean',
717-
value: true
718-
}
719-
];
720-
721-
beforeEach(function () {
722-
nock.cleanAll();
723-
724-
contextBrokerMock = nock('http://192.168.1.1:1026')
725-
.matchHeader('fiware-service', 'smartgondor')
726-
.matchHeader('fiware-servicepath', 'gardens')
727-
.post(
728-
'/v2/entities/ws1/attrs',
729-
utils.readExampleFile(
730-
'./test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin9.json'
731-
)
732-
)
733-
.query({ type: 'WeatherStation' })
734-
.reply(204);
735-
});
736-
737-
it('should apply the expression before sending the values', function (done) {
738-
iotAgentLib.update('ws1', 'WeatherStationMultiple', '', values, function (error) {
739-
should.not.exist(error);
740-
contextBrokerMock.done();
741-
done();
742-
});
743-
});
744-
});
745-
746532
describe('When an update comes for attributes without expressions and Object type', function () {
747533
// Case: Update for a JSON document attribute without expression
748534
const values = [

0 commit comments

Comments
 (0)