-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix out-of-sync binding names causing null ref error (#216)
* Move toCoreFunctionMetadata to separate file * Use string hash instead of count for binding names
- Loading branch information
Showing
5 changed files
with
247 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { ExponentialBackoffRetryOptions, FixedDelayRetryOptions, GenericFunctionOptions } from '@azure/functions'; | ||
import * as coreTypes from '@azure/functions-core'; | ||
import { returnBindingKey } from '../constants'; | ||
import { AzFuncSystemError } from '../errors'; | ||
import { isTrigger } from '../utils/isTrigger'; | ||
import { toRpcDuration } from './toRpcDuration'; | ||
|
||
export function toCoreFunctionMetadata(name: string, options: GenericFunctionOptions): coreTypes.FunctionMetadata { | ||
const bindings: Record<string, coreTypes.RpcBindingInfo> = {}; | ||
const bindingNames: string[] = []; | ||
|
||
const trigger = options.trigger; | ||
bindings[trigger.name] = { | ||
...trigger, | ||
direction: 'in', | ||
type: isTrigger(trigger.type) ? trigger.type : trigger.type + 'Trigger', | ||
}; | ||
bindingNames.push(trigger.name); | ||
|
||
if (options.extraInputs) { | ||
for (const input of options.extraInputs) { | ||
bindings[input.name] = { | ||
...input, | ||
direction: 'in', | ||
}; | ||
bindingNames.push(input.name); | ||
} | ||
} | ||
|
||
if (options.return) { | ||
bindings[returnBindingKey] = { | ||
...options.return, | ||
direction: 'out', | ||
}; | ||
bindingNames.push(returnBindingKey); | ||
} | ||
|
||
if (options.extraOutputs) { | ||
for (const output of options.extraOutputs) { | ||
bindings[output.name] = { | ||
...output, | ||
direction: 'out', | ||
}; | ||
bindingNames.push(output.name); | ||
} | ||
} | ||
|
||
const dupeBindings = bindingNames.filter((v, i) => bindingNames.indexOf(v) !== i); | ||
if (dupeBindings.length > 0) { | ||
throw new AzFuncSystemError( | ||
`Duplicate bindings found for function "${name}". Remove a duplicate binding or manually specify the "name" property to make it unique.` | ||
); | ||
} | ||
|
||
let retryOptions: coreTypes.RpcRetryOptions | undefined; | ||
if (options.retry) { | ||
retryOptions = { | ||
...options.retry, | ||
retryStrategy: options.retry.strategy, | ||
delayInterval: toRpcDuration((<FixedDelayRetryOptions>options.retry).delayInterval, 'retry.delayInterval'), | ||
maximumInterval: toRpcDuration( | ||
(<ExponentialBackoffRetryOptions>options.retry).maximumInterval, | ||
'retry.maximumInterval' | ||
), | ||
minimumInterval: toRpcDuration( | ||
(<ExponentialBackoffRetryOptions>options.retry).minimumInterval, | ||
'retry.minimumInterval' | ||
), | ||
}; | ||
} | ||
|
||
return { name, bindings, retryOptions }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import 'mocha'; | ||
import { expect } from 'chai'; | ||
import { output, trigger } from '../../src'; | ||
import { toCoreFunctionMetadata } from '../../src/converters/toCoreFunctionMetadata'; | ||
|
||
describe('toCoreFunctionMetadata', () => { | ||
const handler = () => {}; | ||
const expectedHttpTrigger = { | ||
authLevel: 'anonymous', | ||
methods: ['GET', 'POST'], | ||
type: 'httpTrigger', | ||
name: 'httpTrigger433d175fc9', | ||
direction: 'in', | ||
}; | ||
const expectedHttpOutput = { type: 'http', name: 'httpOutput9a706511b1', direction: 'out' }; | ||
const expectedQueueOutput1 = { | ||
queueName: 'e2e-test-queue-trigger', | ||
connection: 'e2eTest_storage', | ||
type: 'queue', | ||
name: 'queueOutput8b95495f3d', | ||
direction: 'out', | ||
}; | ||
|
||
it('http trigger', () => { | ||
const result = toCoreFunctionMetadata('funcName', { | ||
handler, | ||
trigger: trigger.http({}), | ||
return: output.http({}), | ||
}); | ||
expect(result).to.deep.equal({ | ||
name: 'funcName', | ||
bindings: { | ||
httpTrigger433d175fc9: expectedHttpTrigger, | ||
$return: expectedHttpOutput, | ||
}, | ||
retryOptions: undefined, | ||
}); | ||
}); | ||
|
||
it('http trigger, storage output', () => { | ||
const result = toCoreFunctionMetadata('funcName', { | ||
handler, | ||
trigger: trigger.http({}), | ||
return: output.http({}), | ||
extraOutputs: [ | ||
output.storageQueue({ | ||
queueName: 'e2e-test-queue-trigger', | ||
connection: 'e2eTest_storage', | ||
}), | ||
], | ||
}); | ||
expect(result).to.deep.equal({ | ||
name: 'funcName', | ||
bindings: { | ||
httpTrigger433d175fc9: expectedHttpTrigger, | ||
$return: expectedHttpOutput, | ||
queueOutput8b95495f3d: expectedQueueOutput1, | ||
}, | ||
retryOptions: undefined, | ||
}); | ||
}); | ||
|
||
it('http trigger, multiple storage output', () => { | ||
const result = toCoreFunctionMetadata('funcName', { | ||
handler, | ||
trigger: trigger.http({}), | ||
return: output.http({}), | ||
extraOutputs: [ | ||
output.storageQueue({ | ||
queueName: 'e2e-test-queue-trigger', | ||
connection: 'e2eTest_storage', | ||
}), | ||
output.storageQueue({ | ||
queueName: 'e2e-test-queue-trigger2', | ||
connection: 'e2eTest_storage', | ||
}), | ||
], | ||
}); | ||
expect(result).to.deep.equal({ | ||
name: 'funcName', | ||
bindings: { | ||
httpTrigger433d175fc9: expectedHttpTrigger, | ||
$return: expectedHttpOutput, | ||
queueOutput8b95495f3d: expectedQueueOutput1, | ||
queueOutput7ab7ce64ad: { | ||
queueName: 'e2e-test-queue-trigger2', | ||
connection: 'e2eTest_storage', | ||
type: 'queue', | ||
name: 'queueOutput7ab7ce64ad', | ||
direction: 'out', | ||
}, | ||
}, | ||
retryOptions: undefined, | ||
}); | ||
}); | ||
|
||
it('http trigger, duplicate storage output', () => { | ||
expect(() => { | ||
toCoreFunctionMetadata('funcName', { | ||
handler, | ||
trigger: trigger.http({}), | ||
return: output.http({}), | ||
extraOutputs: [ | ||
output.storageQueue({ | ||
queueName: 'e2e-test-queue-trigger', | ||
connection: 'e2eTest_storage', | ||
}), | ||
output.storageQueue({ | ||
queueName: 'e2e-test-queue-trigger', | ||
connection: 'e2eTest_storage', | ||
}), | ||
], | ||
}); | ||
}).to.throw(/duplicate bindings found/i); | ||
}); | ||
|
||
it('http trigger, duplicate storage output with name workaround', () => { | ||
const result = toCoreFunctionMetadata('funcName', { | ||
handler, | ||
trigger: trigger.http({}), | ||
return: output.http({}), | ||
extraOutputs: [ | ||
output.storageQueue({ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
name: 'notADupe', | ||
queueName: 'e2e-test-queue-trigger', | ||
connection: 'e2eTest_storage', | ||
}), | ||
output.storageQueue({ | ||
queueName: 'e2e-test-queue-trigger', | ||
connection: 'e2eTest_storage', | ||
}), | ||
], | ||
}); | ||
|
||
expect(result).to.deep.equal({ | ||
name: 'funcName', | ||
bindings: { | ||
httpTrigger433d175fc9: expectedHttpTrigger, | ||
$return: expectedHttpOutput, | ||
queueOutput8b95495f3d: expectedQueueOutput1, | ||
notADupe: { | ||
queueName: 'e2e-test-queue-trigger', | ||
connection: 'e2eTest_storage', | ||
type: 'queue', | ||
name: 'notADupe', | ||
direction: 'out', | ||
}, | ||
}, | ||
retryOptions: undefined, | ||
}); | ||
}); | ||
}); |