Skip to content

Commit

Permalink
fix(core): CfnParameter of Number type cannot be used as a string (#1…
Browse files Browse the repository at this point in the history
…0422)

CloudFormation allows for parameters of type 'Number' to be referenced,
using the 'Ref' keyword, into properties that are of type 'String'.

This will let customers now use CloudFormation maximum and minimum
constraints on the number parameter type, and still use the resulting
value in a property of string type.

fixes #10228


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Niranjan Jayakar authored Sep 18, 2020
1 parent a1df511 commit 28adc88
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/core/lib/cfn-parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ export class CfnParameter extends CfnElement {
* The parameter value, if it represents a string.
*/
public get valueAsString(): string {
if (!isStringType(this.type)) {
throw new Error(`Parameter type (${this.type}) is not a string type`);
if (!isStringType(this.type) && !isNumberType(this.type)) {
throw new Error(`Parameter type (${this.type}) is not a string or number type`);
}
return Token.asString(this.value);
}
Expand Down
32 changes: 32 additions & 0 deletions packages/@aws-cdk/core/test/test.cfn-parameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Test } from 'nodeunit';
import { CfnParameter, Stack } from '../lib';

export = {
'valueAsString supports both string and number types'(test: Test) {
// GIVEN
const stack = new Stack();
const numberParam = new CfnParameter(stack, 'numberParam', { type: 'Number', default: 10 });
const stringParam = new CfnParameter(stack, 'stringParam', { type: 'String', default: 'a-default' });

// WHEN
const numVal = numberParam.valueAsString;
const strVal = stringParam.valueAsString;

// THEN
test.deepEqual(stack.resolve(numVal), { Ref: 'numberParam' });
test.deepEqual(stack.resolve(strVal), { Ref: 'stringParam' });

test.done();
},

'valueAsString fails for unsupported types'(test: Test) {
// GIVEN
const stack = new Stack();
const listParam = new CfnParameter(stack, 'listParam', { type: 'List', default: 10 });

// WHEN - THEN
test.throws(() => listParam.valueAsList, /Parameter type \(List\)/);

test.done();
},
}

0 comments on commit 28adc88

Please sign in to comment.