diff --git a/packages/@aws-cdk/aws-sam/test/sam.test.ts b/packages/@aws-cdk/aws-sam/test/application.test.ts similarity index 100% rename from packages/@aws-cdk/aws-sam/test/sam.test.ts rename to packages/@aws-cdk/aws-sam/test/application.test.ts diff --git a/packages/@aws-cdk/aws-sam/test/function.test.ts b/packages/@aws-cdk/aws-sam/test/function.test.ts new file mode 100644 index 0000000000000..d8dade625be0e --- /dev/null +++ b/packages/@aws-cdk/aws-sam/test/function.test.ts @@ -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'], + }); +}); diff --git a/tools/cfn2ts/lib/codegen.ts b/tools/cfn2ts/lib/codegen.ts index 8d85d05f7ee72..08d0636a841f5 100644 --- a/tools/cfn2ts/lib/codegen.ts +++ b/tools/cfn2ts/lib/codegen.ts @@ -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];