Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(yaml-cfn): do not deserialize year-month-date as strings #13745

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
AWSTemplateFormatVersion: 2010-09-09
Resources:
Role:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ describe('CDK Include', () => {
);
});

test('can ingest a template with year-month-date parsed as string instead of Date', () => {
includeTestTemplate(stack, 'year-month-date-as-strings.yaml');

expect(stack).toMatchTemplate({
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"Role": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": ["ec2.amazonaws.com"],
},
"Action": ["sts:AssumeRole"],
},
],
},
},
},
},
});
});

test('can ingest a template with the short form Base64 function', () => {
includeTestTemplate(stack, 'short-form-base64.yaml');

Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/yaml-cfn/lib/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ const shortForms: yaml_types.Schema.CustomTag[] = [
function parseYamlStrWithCfnTags(text: string): any {
return yaml.parse(text, {
customTags: shortForms,
schema: 'yaml-1.1',
schema: 'core',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But are we sure using the core schema is not going to break other stuff? I.e: is "No" going to be encoded correctly (read: quoted), or is it going to be encoded YAML-1.2-style (which in Yaml 1.1 is an alias for false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I added a test confirming that No is deserialized correctly (as "No", not false).

Let me know if you want to see some more tests of some edge-cases in the YAML handling!

});
}
34 changes: 34 additions & 0 deletions packages/@aws-cdk/yaml-cfn/test/deserialization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import '@aws-cdk/assert/jest';
import * as yaml_cfn from '../lib';

test('Unquoted year-month-day is treated as a string, not a Date', () => {
const value = yaml_cfn.deserialize('Key: 2020-12-31');

expect(value).toEqual({
Key: '2020-12-31',
});
});

test("Unquoted 'No' is treated as a string, not a boolean", () => {
const value = yaml_cfn.deserialize('Key: No');

expect(value).toEqual({
Key: 'No',
});
});

test("Short-form 'Ref' is deserialized correctly", () => {
const value = yaml_cfn.deserialize('!Ref Resource');

expect(value).toEqual({
Ref: 'Resource',
});
});

test("Short-form 'Fn::GetAtt' is deserialized correctly", () => {
const value = yaml_cfn.deserialize('!GetAtt Resource.Attribute');

expect(value).toEqual({
'Fn::GetAtt': 'Resource.Attribute',
});
});
8 changes: 8 additions & 0 deletions packages/@aws-cdk/yaml-cfn/test/serialization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import '@aws-cdk/assert/jest';
import * as yaml_cfn from '../lib';

test('An object with a single string value is serialized as a simple string', () => {
const value = yaml_cfn.serialize({ key: 'some string' });

expect(value).toEqual('key: some string\n');
});
5 changes: 0 additions & 5 deletions packages/@aws-cdk/yaml-cfn/test/yaml.test.ts

This file was deleted.