Skip to content

Commit

Permalink
fix(scheduler-alpha): scheduler input always get transformed to strin…
Browse files Browse the repository at this point in the history
…g with extra double quotes (#31894)

### Issue # (if applicable)

None

### Reason for this change

When using `ScheduleTargetInput.fromText("some-string")`, the string is always wrapped with an extra double quotes in the final template:
```
"Input": "\"some-string\"",
```

Customers cannot get rid of the extra escaped double quotes other than using escape hatches.

### Description of changes

Skip converting the text to JSON string, which is the reason causing the extra double quotes.

### Description of how you validated changes

Unit test and integ test.


### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*

----

BREAKING CHANGE: Got rid of extra double quotes on Schedule input which blocks customer intention of using no double quotes in the input text
  • Loading branch information
samson-keung authored Oct 28, 2024
1 parent 8c15b5f commit 186b8ab
Show file tree
Hide file tree
Showing 25 changed files with 48,845 additions and 45,323 deletions.
11 changes: 6 additions & 5 deletions packages/@aws-cdk/aws-scheduler-alpha/lib/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export abstract class ScheduleTargetInput {
* @param text Text to use as the input for the target
*/
public static fromText(text: string): ScheduleTargetInput {
return new FieldAwareEventInput(text);
return new FieldAwareEventInput(text, false);
}

/**
Expand All @@ -28,7 +28,7 @@ export abstract class ScheduleTargetInput {
* @param obj object to use to convert to JSON to use as input for the target
*/
public static fromObject(obj: any): ScheduleTargetInput {
return new FieldAwareEventInput(obj);
return new FieldAwareEventInput(obj, true);
}

protected constructor() {
Expand All @@ -41,7 +41,7 @@ export abstract class ScheduleTargetInput {
}

class FieldAwareEventInput extends ScheduleTargetInput {
constructor(private readonly input: any) {
constructor(private readonly input: any, private readonly toJsonString: boolean) {
super();
}

Expand All @@ -57,10 +57,11 @@ class FieldAwareEventInput extends ScheduleTargetInput {
}

const stack = Stack.of(schedule);
return stack.toJsonString(Tokenization.resolve(this.input, {
const inputString = Tokenization.resolve(this.input, {
scope: schedule,
resolver: new Replacer(),
}));
});
return this.toJsonString ? stack.toJsonString(inputString) : inputString;
}
}

Expand Down
26 changes: 21 additions & 5 deletions packages/@aws-cdk/aws-scheduler-alpha/test/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,44 @@ describe('schedule target input', () => {
Template.fromStack(stack).hasResource('AWS::Scheduler::Schedule', {
Properties: {
Target: {
Input: '"test"',
Input: 'test',
},
},
});
});

test('create an input from text with a ref inside', () => {
test('create an input from text concatenated from literal string with a token', () => {
new Schedule(stack, 'MyScheduleDummy', {
schedule: expr,
target: new SomeLambdaTarget(func, ScheduleTargetInput.fromText(stack.account)),
target: new SomeLambdaTarget(func, ScheduleTargetInput.fromText(`ac-${stack.account}`)),
});

Template.fromStack(stack).hasResource('AWS::Scheduler::Schedule', {
Properties: {
Target: {
Input: {
'Fn::Join': ['', ['"', { Ref: 'AWS::AccountId' }, '"']],
'Fn::Join': ['', ['ac-', { Ref: 'AWS::AccountId' }]],
},
},
},
});
});

test('create an input from text with a ref inside', () => {
new Schedule(stack, 'MyScheduleDummy', {
schedule: expr,
target: new SomeLambdaTarget(func, ScheduleTargetInput.fromText(stack.account)),
});

Template.fromStack(stack).hasResource('AWS::Scheduler::Schedule', {
Properties: {
Target: {
Input: { Ref: 'AWS::AccountId' },
},
},
});
});

test('create an input from object', () => {
const input = ScheduleTargetInput.fromObject({
test: 'test',
Expand Down Expand Up @@ -115,7 +131,7 @@ describe('schedule target input', () => {
Template.fromStack(stack).hasResource('AWS::Scheduler::Schedule', {
Properties: {
Target: {
Input: '"Test=<aws.scheduler.schedule-arn>"',
Input: 'Test=<aws.scheduler.schedule-arn>',
},
},
});
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SomeLambdaTarget implements scheduler.IScheduleTarget {
return {
arn: this.fn.functionArn,
role: this.role,
input: scheduler.ScheduleTargetInput.fromText('Input Text'),
input: scheduler.ScheduleTargetInput.fromObject('Input Text'),
retryPolicy: {
maximumEventAgeInSeconds: 180,
maximumRetryAttempts: 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const importedLambdaTagValue = 'importedLambdaTagValue';
new scheduler.Schedule(scheduleStack, 'ScheduleWithImportedLambda', {
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(1)),
target: new LambdaInvoke(importedFunc, {
input: scheduler.ScheduleTargetInput.fromText(importedLambdaTagValue),
input: scheduler.ScheduleTargetInput.fromObject(importedLambdaTagValue),
}),
});

Expand All @@ -88,7 +88,7 @@ const sameStackLambdaTagValue = 'sameStackLambdaTagValue';
new scheduler.Schedule(scheduleStack, 'ScheduleWithSameStackLambda', {
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(1)),
target: new LambdaInvoke(sameStackFunc, {
input: scheduler.ScheduleTargetInput.fromText(sameStackLambdaTagValue),
input: scheduler.ScheduleTargetInput.fromObject(sameStackLambdaTagValue),
}),
});

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"Arn": {
"Ref": "TopicBFC7AF6E"
},
"Input": "\"Hello, Scheduler!\"",
"Input": "Hello, Scheduler!",
"RetryPolicy": {
"MaximumEventAgeInSeconds": 86400,
"MaximumRetryAttempts": 185
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 186b8ab

Please sign in to comment.