Skip to content

Commit

Permalink
Merge pull request #750 from telefonicaid/fix/expandVar_single_string…
Browse files Browse the repository at this point in the history
…_values

Fix/expand var single string values
  • Loading branch information
fgalan committed Sep 26, 2023
2 parents c7661af + 114b5b9 commit 9181b45
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
@@ -1,6 +1,7 @@
- Add: apply expandVar with JSON.parse to all fields of all actions (sms, smpp, email, post, update) (#746)
- Fix: check domain before access domain
- Fix: expandVar return a 'null' instead of null (#746)
- Fix: expandVar for single string values with null, boolean, number (#746)
- Fix: smtp and smpp logs (#738)
- Add: allow access entities using NGSIv2 API for non_signal rules (new setting nonSignalByAPI / PERSEO_CHECK_NON_SIGNAL_BY_API) (#549)
- Remove support for ngsv1 notifications (#714)
Expand Down
18 changes: 12 additions & 6 deletions lib/myutils.js
Expand Up @@ -55,7 +55,11 @@ function expandVar(val, mapping, trycast) {
if (typeof val === 'string') {
var expanded = false;
var valObj = null;
var uniqueString = false;
Object.keys(mapping).forEach(function(p) {
if (typeof mapping[p] === 'string' && Object.keys(mapping).length === 1) {
uniqueString = true;
}
val = val.replace(new RegExp('\\$\\{' + p + '\\}', 'g'), mapping[p]);
// javascript replace always return a string: we need to keep object value (#692)
if (!expanded && val === '[object Object]') {
Expand All @@ -66,7 +70,7 @@ function expandVar(val, mapping, trycast) {
if (valObj) {
val = valObj;
}
if (trycast) {
if (trycast && !uniqueString) {
try {
// Check if "${num}" and expand it as real value, non string (#362, #369)
var n = JSON.parse(val);
Expand All @@ -84,11 +88,13 @@ function expandVar(val, mapping, trycast) {
}
}
if (!expanded) {
// Set to 'null' all non expanded values (#469)
val = val.replace(/\$\{\w*\}/g, 'null');
if (val === 'null') {
// val is just one value (non string value composes of several values) (#746)
val = null;
if (val !== 'null' || !uniqueString) {
// Set to 'null' all non expanded values (#469)
val = val.replace(/\$\{\w*\}/g, 'null');
if (val === 'null') {
// val is just one value (non string value composes of several values) (#746)
val = null;
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/component/myutils_test.js
Expand Up @@ -55,7 +55,7 @@ describe('Myutils', function() {
newStr;
newStr = myutils.expandVar(str, map, true);
should.exist(newStr);
newStr.should.be.equal(23);
newStr.should.be.equal('23');
});
});
describe('When string and there is a variable which is a number to expand', function() {
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('Myutils', function() {
newStr;
newStr = myutils.expandVar(str, map, true);
should.exist(newStr);
newStr.should.be.equal(true);
newStr.should.be.equal('true');
});
});
describe('When there is a variable which is a string object to expand', function() {
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('Myutils', function() {
map = { a: 'null' },
newStr;
newStr = myutils.expandVar(str, map, true);
should.equal(newStr, null);
should.equal(newStr, 'null');
});
});
describe('When there is a variable which is not expanded', function() {
Expand Down

0 comments on commit 9181b45

Please sign in to comment.