We noticed this in the messageIdArray field of a TriggerMetadata of a service bus message batch. String collections may actually contain numbers.
After a thorough search, the culprit seems to be in azure-functions-nodejs-library/src/converters/fromRpcTypedData.ts
In the fromRpcTypedData function, instead of simply returning data.collectionString.string, data.collectionString.string.map(tryJsonParse) is returned:
} else if (data.collectionString && isDefined(data.collectionString.string)) {
return data.collectionString.string.map(tryJsonParse);
What does tryJsonParse do?
function tryJsonParse(data: string): unknown {
try {
return JSON.parse(data);
} catch {
return data;
}
}
So if a string ID is json-like, the json-like value is parsed and returned. Consequently, strings like "123" that are JSON-like will be converted as though they were JSON. In our case, IDs sometimes look like integers.
To add to the injury, if the string is JSON-object-like, the code will convert the keys to camel case. So if my message ID is the string '{"BafflingConversion": true}', fromRpcTypedData will convert it to the object {bafflingConversion: true}.
This issue was introduced in efa5fb3. There is of course no obvious, sane and logical reason that the entries of a string collection should ever be considered as or converted to JSON, and whatever insane circumstance prompted the change isn't clear from the commit message.
@ejizba can you add some context as to why string collections should ever be considered as JSON? What was the intended purpose of the change?
We noticed this in the
messageIdArrayfield of a TriggerMetadata of a service bus message batch. String collections may actually contain numbers.After a thorough search, the culprit seems to be in
azure-functions-nodejs-library/src/converters/fromRpcTypedData.tsIn the
fromRpcTypedDatafunction, instead of simply returningdata.collectionString.string,data.collectionString.string.map(tryJsonParse)is returned:What does
tryJsonParsedo?So if a string ID is json-like, the json-like value is parsed and returned. Consequently, strings like
"123"that are JSON-like will be converted as though they were JSON. In our case, IDs sometimes look like integers.To add to the injury, if the string is JSON-object-like, the code will convert the keys to camel case. So if my message ID is the string
'{"BafflingConversion": true}',fromRpcTypedDatawill convert it to the object{bafflingConversion: true}.This issue was introduced in efa5fb3. There is of course no obvious, sane and logical reason that the entries of a string collection should ever be considered as or converted to JSON, and whatever insane circumstance prompted the change isn't clear from the commit message.
@ejizba can you add some context as to why string collections should ever be considered as JSON? What was the intended purpose of the change?