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: 27 additions & 0 deletions packages/@aws-cdk/aws-sam/test/function.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import '@aws-cdk/assert/jest';
import * as cdk from '@aws-cdk/core';
import * as sam from '../lib';

test("correctly chooses a string array from the type unions of the 'policies' property", () => {
const stack = new cdk.Stack();

new sam.CfnFunction(stack, 'MyFunction', {
codeUri: {
bucket: 'my-bucket',
key: 'my-key',
},
runtime: 'nodejs-12.x',
handler: 'index.handler',
policies: ['AWSLambdaExecute'],
});

expect(stack).toHaveResourceLike('AWS::Serverless::Function', {
CodeUri: {
Bucket: 'my-bucket',
Key: 'my-key',
},
Handler: 'index.handler',
Runtime: 'nodejs-12.x',
Policies: ['AWSLambdaExecute'],
});
});
9 changes: 9 additions & 0 deletions tools/cfn2ts/lib/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,15 @@ export default class CodeGenerator {

this.code.line(`const errors = new ${CORE}.ValidationResults();`);

// check that the argument is an object
// normally, we would have to explicitly check for null here,
// as typeof null is 'object' in JavaScript,
// but validators are never called with null
// (as evidenced by the code below accessing properties of the argument without checking for null)
this.code.openBlock("if (typeof properties !== 'object')");
this.code.line(`errors.collect(new ${CORE}.ValidationResult('Expected an object, but received: ' + JSON.stringify(properties)));`);
this.code.closeBlock();

Object.keys(propSpecs).forEach(cfnPropName => {
const propSpec = propSpecs[cfnPropName];
const propName = nameConversionTable[cfnPropName];
Expand Down