Skip to content

Commit

Permalink
fix(amplify-cli-core): extend js-yaml.JSON_SCHEMA to inherit json typ…
Browse files Browse the repository at this point in the history
…e conversions when parsing .yml cfn templates (#7909)

* fix(amplify-cli-core): extend js-yaml.JSON_SCHEMA to inherit json type conversions

Instead of creating a js-yaml schema that only contains conversions for cloudformation intrinsic
functions, we should extend the built-in JSON-SCHEMA to inherit type conversions for bools, ints,
floats, etc.

fix #7819

* test(amplify-cli-core): add regression tests
  • Loading branch information
Bentheburrito authored Aug 25, 2021
1 parent 8947b18 commit fe5c1ec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions packages/amplify-cli-core/src/__tests__/cfnUtilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,42 @@ describe('readCFNTemplate', () => {
}
`);
});

it('casts yaml boolean values to corresponding JavaScript boolean', async () => {
const yamlContent = `
someKey: true
someOtherKey: false
someStringKey: "true"
`;

((fs_mock.readFile as unknown) as jest.MockedFunction<TwoArgReadFile>).mockResolvedValueOnce(yamlContent);

const result = await readCFNTemplate(testPath);

expect(result.cfnTemplate).toEqual({
someKey: true,
someOtherKey: false,
someStringKey: 'true',
});
});

it('casts yaml integer and float values to corresponding JavaScript number', async () => {
const yamlContent = `
someKey: 1
someOtherKey: 1.234
someStringKey: "1.234"
`;

((fs_mock.readFile as unknown) as jest.MockedFunction<TwoArgReadFile>).mockResolvedValueOnce(yamlContent);

const result = await readCFNTemplate(testPath);

expect(result.cfnTemplate).toEqual({
someKey: 1,
someOtherKey: 1.234,
someStringKey: '1.234',
});
});
});

describe('writeCFNTemplate', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-cli-core/src/cfnUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function writeCFNTemplate(template: object, filePath: string, optio

// Register custom tags for yaml parser
// Order and definition based on docs: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html
const CF_SCHEMA = new yaml.Schema([
const CF_SCHEMA = yaml.JSON_SCHEMA.extend([
new yaml.Type('!Base64', {
kind: 'scalar',
construct: function (data) {
Expand Down

0 comments on commit fe5c1ec

Please sign in to comment.