Skip to content

Commit

Permalink
fix(events): fromObject handles regular and field tokens together (a…
Browse files Browse the repository at this point in the history
…ws#3916)

Fixes a case in `RuleTargetInput.fromObject()` where both `EventField` tokens and regular tokens were used together, which would lead to an error.

Closes aws#3915.
  • Loading branch information
ndchelsea authored and rix0rrr committed Sep 9, 2019
1 parent d3cbbbe commit b01f62d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
18 changes: 15 additions & 3 deletions packages/@aws-cdk/aws-events/lib/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,21 @@ class FieldAwareEventInput extends RuleTargetInput {
private unquoteKeyPlaceholders(sub: string) {
if (this.inputType !== InputType.Object) { return sub; }

return Lazy.stringValue({ produce: (ctx: IResolveContext) =>
ctx.resolve(sub).replace(OPENING_STRING_REGEX, '<').replace(CLOSING_STRING_REGEX, '>')
});
return Lazy.stringValue({ produce: (ctx: IResolveContext) => Token.asString(deepUnquote(ctx.resolve(sub))) });

function deepUnquote(resolved: any): any {
if (Array.isArray(resolved)) {
return resolved.map(deepUnquote);
} else if (typeof(resolved) === 'object' && resolved !== null) {
for (const [key, value] of Object.entries(resolved)) {
resolved[key] = deepUnquote(value);
}
return resolved;
} else if (typeof(resolved) === 'string') {
return resolved.replace(OPENING_STRING_REGEX, '<').replace(CLOSING_STRING_REGEX, '>');
}
return resolved;
}
}
}

Expand Down
41 changes: 40 additions & 1 deletion packages/@aws-cdk/aws-events/test/test.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { User } from '@aws-cdk/aws-iam';
import cdk = require('@aws-cdk/core');
import { Duration, Stack } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { IRuleTarget, RuleTargetInput, Schedule } from '../lib';
import { EventField, IRuleTarget, RuleTargetInput, Schedule } from '../lib';
import { Rule } from '../lib/rule';

export = {
Expand All @@ -29,6 +29,45 @@ export = {
test.done();
},

'can use joined JSON containing refs in JSON object'(test: Test) {
// GIVEN
const stack = new Stack();
const rule = new Rule(stack, 'Rule', {
schedule: Schedule.rate(Duration.minutes(1)),
});

// WHEN
rule.addTarget(new SomeTarget(RuleTargetInput.fromObject({
data: EventField.fromPath('$'),
stackName: cdk.Aws.STACK_NAME,
})));

// THEN
expect(stack).to(haveResourceLike('AWS::Events::Rule', {
Targets: [
{
InputTransformer: {
InputPathsMap: {
f1: '$'
},
InputTemplate: {
'Fn::Join': [
'',
[
'{"data":<f1>,"stackName":"',
{ Ref: 'AWS::StackName' },
'"}'
]
]
},
}
}
]
}));

test.done();
},

'can use token'(test: Test) {
// GIVEN
const stack = new Stack();
Expand Down

0 comments on commit b01f62d

Please sign in to comment.