Skip to content

Commit f193568

Browse files
authored
Fix bug where nested "bytes" property fails with TypeError (#9)
1 parent b0ba713 commit f193568

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/converters/BindingConverters.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,23 @@ export function getNormalizedBindingData(request: RpcInvocationRequest): Context
3737
export function convertKeysToCamelCase(obj: any) {
3838
const output = {};
3939
for (const key in obj) {
40-
// Only "undefined" will be replaced with original object property. For example:
41-
//{ string : "0" } -> 0
42-
//{ string : "false" } -> false
43-
//"test" -> "test" (undefined returned from fromTypedData)
44-
const valueFromDataType = fromTypedData(obj[key]);
45-
const value = valueFromDataType === undefined ? obj[key] : valueFromDataType;
4640
const camelCasedKey = key.charAt(0).toLocaleLowerCase() + key.slice(1);
47-
// If the value is a JSON object (and not array and not http, which is already cased), convert keys to camel case
48-
if (!Array.isArray(value) && typeof value === 'object' && value && value.http == undefined) {
49-
output[camelCasedKey] = convertKeysToCamelCase(value);
50-
} else {
51-
output[camelCasedKey] = value;
41+
try {
42+
// Only "undefined" will be replaced with original object property. For example:
43+
//{ string : "0" } -> 0
44+
//{ string : "false" } -> false
45+
//"test" -> "test" (undefined returned from fromTypedData)
46+
const valueFromDataType = fromTypedData(obj[key]);
47+
const value = valueFromDataType === undefined ? obj[key] : valueFromDataType;
48+
// If the value is a JSON object (and not array and not http, which is already cased), convert keys to camel case
49+
if (!Array.isArray(value) && typeof value === 'object' && value && value.http == undefined) {
50+
output[camelCasedKey] = convertKeysToCamelCase(value);
51+
} else {
52+
output[camelCasedKey] = value;
53+
}
54+
} catch {
55+
// Just use the original value if we failed to recursively convert for any reason
56+
output[camelCasedKey] = obj[key];
5257
}
5358
}
5459
return output;

test/converters/BindingConverters.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,23 @@ describe('Binding Converters', () => {
110110
expect(bindingData.sys.UtcNow).to.be.undefined;
111111
});
112112

113+
it('normalizes binding trigger metadata with bytes as number', () => {
114+
// Test to cover this bug:
115+
// https://github.com/Azure/azure-functions-nodejs-worker/issues/607
116+
const request: RpcInvocationRequest = <RpcInvocationRequest>{
117+
triggerMetadata: {
118+
A: {
119+
data: 'json',
120+
json: '{"B":{"bytes":4}}',
121+
},
122+
},
123+
invocationId: '12341',
124+
};
125+
126+
const bindingData = getNormalizedBindingData(request);
127+
expect(bindingData.a).to.deep.equal({ b: { bytes: 4 } });
128+
});
129+
113130
it('catologues binding definitions', () => {
114131
const functionMetaData: RpcFunctionMetadata = <RpcFunctionMetadata>{
115132
name: 'MyFunction',

0 commit comments

Comments
 (0)