Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/converters/BindingConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,23 @@ function isBindingDirection(input: string | undefined): boolean {
export function convertKeysToCamelCase(obj: any) {
const output = {};
for (const key in obj) {
// Only "undefined" will be replaced with original object property. For example:
//{ string : "0" } -> 0
//{ string : "false" } -> false
//"test" -> "test" (undefined returned from fromTypedData)
const valueFromDataType = fromTypedData(obj[key]);
const value = valueFromDataType === undefined ? obj[key] : valueFromDataType;
const camelCasedKey = key.charAt(0).toLocaleLowerCase() + key.slice(1);
// If the value is a JSON object (and not array and not http, which is already cased), convert keys to camel case
if (!Array.isArray(value) && typeof value === 'object' && value && value.http == undefined) {
output[camelCasedKey] = convertKeysToCamelCase(value);
} else {
output[camelCasedKey] = value;
try {
// Only "undefined" will be replaced with original object property. For example:
//{ string : "0" } -> 0
//{ string : "false" } -> false
//"test" -> "test" (undefined returned from fromTypedData)
const valueFromDataType = fromTypedData(obj[key]);
const value = valueFromDataType === undefined ? obj[key] : valueFromDataType;
// If the value is a JSON object (and not array and not http, which is already cased), convert keys to camel case
if (!Array.isArray(value) && typeof value === 'object' && value && value.http == undefined) {
output[camelCasedKey] = convertKeysToCamelCase(value);
} else {
output[camelCasedKey] = value;
}
} catch {
// Just use the original value if we failed to recursively convert for any reason
output[camelCasedKey] = obj[key];
}
}
return output;
Expand Down
17 changes: 17 additions & 0 deletions test/converters/BindingConverters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ describe('Binding Converters', () => {
expect(bindingData.sys.UtcNow).to.be.undefined;
});

it('normalizes binding trigger metadata with bytes as number', () => {
// Test to cover this bug:
// https://github.com/Azure/azure-functions-nodejs-worker/issues/607
const request: RpcInvocationRequest = <RpcInvocationRequest>{
triggerMetadata: {
A: {
data: 'json',
json: '{"B":{"bytes":4}}',
},
},
invocationId: '12341',
};

const bindingData = getNormalizedBindingData(request);
expect(bindingData.a).to.deep.equal({ b: { bytes: 4 } });
});

it('catologues binding definitions', () => {
const functionMetaData: RpcFunctionMetadata = <RpcFunctionMetadata>{
name: 'MyFunction',
Expand Down