Skip to content

Commit 856a050

Browse files
authored
Merge pull request #1038 from telefonicaid/fix/check_id_is_valid_after_apply_expression_in_multientity
check entityId is valid after apply expression in multientity plugin
2 parents 2b86ab4 + e71674f commit 856a050

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
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: check entityId is valid after apply expression in multientity plugin (#1039)
12
- Fix: check access to data in fillService facility
23
- Fix: use expressionLanguage defined in group is not defined at device level (#1027)
34
- Fix: ensure service of groups, device and commands is stored in mongo in lowercase (#1023)

lib/plugins/multiEntity.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,19 @@ const utils = require('./pluginUtils');
4141
/* eslint-disable-next-line no-unused-vars */
4242
const aliasPlugin = require('./attributeAlias');
4343

44-
4544
function checkJexl(typeInformation) {
4645
if (
4746
config.getConfig().defaultExpressionLanguage === 'jexl' &&
48-
typeInformation.expressionLanguage &&
49-
typeInformation.expressionLanguage !== 'legacy'
47+
typeInformation.expressionLanguage &&
48+
typeInformation.expressionLanguage !== 'legacy'
5049
) {
5150
return true;
5251
} else if (config.getConfig().defaultExpressionLanguage === 'jexl' && !typeInformation.expressionLanguage) {
5352
return true;
5453
} else if (
5554
config.getConfig().defaultExpressionLanguage === 'legacy' &&
56-
typeInformation.expressionLanguage &&
57-
typeInformation.expressionLanguage === 'jexl'
55+
typeInformation.expressionLanguage &&
56+
typeInformation.expressionLanguage === 'jexl'
5857
) {
5958
return true;
6059
}
@@ -65,6 +64,11 @@ function hasEntityName(item) {
6564
return item.entity_name;
6665
}
6766

67+
function ensureEntityId(entityName, originalEntityName) {
68+
// ensure no return null, 0, false, NaN and other invalids entityIDs
69+
return entityName ? entityName : originalEntityName;
70+
}
71+
6872
/**
6973
* Return a list of all the attributes that don't have a multientity option. It considers NGSIv1.
7074
*
@@ -87,20 +91,23 @@ function filterOutMultientitiesNgsi1(originalAttrs, meAttributes) {
8791
*/
8892
function filterOutMultientitiesNgsi2(originalAttrs, meAttributes) {
8993
const result = {};
90-
const meNamesList = _.pluck( meAttributes, 'name');
91-
const meObjectsList = _.pluck( meAttributes, 'object_id');
94+
const meNamesList = _.pluck(meAttributes, 'name');
95+
const meObjectsList = _.pluck(meAttributes, 'object_id');
9296
let toBeFilteredByObj = [];
9397

9498
for (const att in originalAttrs) {
9599
if (originalAttrs.hasOwnProperty(att)) {
96100
if (!_.contains(meNamesList, att)) {
97101
result[att] = originalAttrs[att];
98102
}
99-
if (originalAttrs[att].hasOwnProperty('multi')){
100-
let cleanAttributes = _.union([_.clone(originalAttrs[att])], originalAttrs[att].multi);
101-
delete cleanAttributes[0].multi;
102-
cleanAttributes = _.map(cleanAttributes, function(val){ val['name'] = att; return val; });
103-
toBeFilteredByObj = _.union(toBeFilteredByObj, cleanAttributes);
103+
if (originalAttrs[att].hasOwnProperty('multi')) {
104+
let cleanAttributes = _.union([_.clone(originalAttrs[att])], originalAttrs[att].multi);
105+
delete cleanAttributes[0].multi;
106+
cleanAttributes = _.map(cleanAttributes, function (val) {
107+
val['name'] = att;
108+
return val;
109+
});
110+
toBeFilteredByObj = _.union(toBeFilteredByObj, cleanAttributes);
104111
}
105112
}
106113
}
@@ -157,7 +164,7 @@ function generateNewCEsNgsi1(entity, newEntities, entityTypes, typeInformation,
157164
result.push({
158165
type: entityTypes[newEntities[i]],
159166
isPattern: 'false',
160-
id: entityName,
167+
id: ensureEntityId(entityName, newEntities[i]),
161168
attributes: newEntityAttributes
162169
});
163170
}
@@ -231,7 +238,7 @@ function generateNewCEsNgsi2(entity, newEntities, entityTypes, typeInformation,
231238
entityName = parser.applyExpression(newEntities[i], ctx, typeInformation);
232239

233240
newEntityAttributes.type = entityTypes[newEntities[i]];
234-
newEntityAttributes.id = entityName;
241+
newEntityAttributes.id = ensureEntityId(entityName, newEntities[i]);
235242

236243
result.push(newEntityAttributes);
237244
}

lib/services/northBound/contextServer-NGSI-v2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ function handleUpdateNgsi2(req, res, next) {
204204
[apply(async.map, req.body.entities, apply(generateUpdateActionsNgsi2, req)), reduceActions, async.series],
205205
function (error, result) {
206206
if (error) {
207-
logger.debug(context, 'There was an error handling the update action: %s.', error);
207+
logger.debug(context, 'There was an error handling the update action: %j.', error);
208208

209209
next(error);
210210
} else {

0 commit comments

Comments
 (0)