Add test cases for setMessageProperty with non-object properties

This commit is contained in:
Nick O'Leary
2020-06-01 13:13:14 +01:00
parent 132254b3a5
commit 4b54a81dfd
4 changed files with 84 additions and 28 deletions

View File

@@ -371,14 +371,14 @@ function setObjectProperty(msg,prop,value,createMissing) {
var length = msgPropParts.length;
var obj = msg;
var key;
var flag = true;
for (var i=0;i<length-1;i++) {
key = msgPropParts[i];
if (typeof key === 'string' || (typeof key === 'number' && !Array.isArray(obj))) {
if (obj.hasOwnProperty(key)) {
if (length > 1 && ((typeof obj[key] !== "object" && typeof obj[key] !== "function") || obj[key] === null)) {
//console.log("Can't override primitive type with object.");
flag = false;
// Break out early as we cannot create a property beneath
// this type of value
return false;
}
obj = obj[key];
} else if (createMissing) {
@@ -389,7 +389,7 @@ function setObjectProperty(msg,prop,value,createMissing) {
}
obj = obj[key];
} else {
return null;
return false;
}
} else if (typeof key === 'number') {
// obj is an array
@@ -402,7 +402,7 @@ function setObjectProperty(msg,prop,value,createMissing) {
}
obj = obj[key];
} else {
return null;
return false;
}
} else {
obj = obj[key];
@@ -420,11 +420,11 @@ function setObjectProperty(msg,prop,value,createMissing) {
if (typeof obj === "object" && obj !== null) {
obj[key] = value;
} else {
//console.log("Can't override primitive type with object.");
flag = false;
}
// Cannot set a property of a non-object/array
return false;
}
}
return flag;
return true;
}
/*!