Azure / Azure/azure-functions-nodejs-library
String collections in trigger metadata may contain nulls, objects, arrays, numbers and strings after conversion
- Dominant language
- TypeScript
- Stars
- 70
- Forks
- 35
- Avg merge
- 4d 18h
- Merged PRs (30d)
- 6
Description
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:
```typescript
} else if (data.collectionString && isDefined(data.collectionString.string)) {
return data.collectionString.string.map(tryJsonParse);
```
What does `tryJsonParse` do?
```typescript
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 efa5fb36. 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?
Contributor guide
Assessment
This issue has not been assessed yet.