Skip to content

Commit

Permalink
fix(custom-resources): flatten objects with null values in AwsCustomR…
Browse files Browse the repository at this point in the history
…esource (#5073)

Fixes #5061
  • Loading branch information
jogold authored and mergify[bot] committed Nov 18, 2019
1 parent 6b764ea commit f4ea264
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { AwsSdkCall } from '../aws-custom-resource';
* @param object the object to be flattened
* @returns a flat object with path as keys
*/
function flatten(object: object): { [key: string]: string } {
export function flatten(object: object): { [key: string]: string } {
return Object.assign(
{},
...function _flatten(child: any, path: string[] = []): any {
return [].concat(...Object.keys(child)
.map(key =>
typeof child[key] === 'object'
typeof child[key] === 'object' && child[key] !== null
? _flatten(child[key], path.concat([key]))
: ({ [path.concat([key]).join('.')]: child[key] })
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AWS = require('aws-sdk-mock');
import nock = require('nock');
import sinon = require('sinon');
import { AwsSdkCall } from '../../lib';
import { handler } from '../../lib/aws-custom-resource/runtime';
import { flatten, handler } from '../../lib/aws-custom-resource/runtime';

AWS.setSDK(require.resolve('aws-sdk'));

Expand Down Expand Up @@ -321,3 +321,20 @@ test('can specify apiVersion and region', async () => {

expect(request.isDone()).toBeTruthy();
});

test('flatten correctly flattens a nested object', () => {
expect(flatten({
a: { b: 'c' },
d: [
{ e: 'f' },
{ g: 'h', i: 1, j: null, k: { l: false } }
],
})).toEqual({
'a.b': 'c',
'd.0.e': 'f',
'd.1.g': 'h',
'd.1.i': 1,
'd.1.j': null,
'd.1.k.l': false
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "AssetParameters44223b510152d51e381791e0347041da535987f7d6f1703b343f9c06f6075059S3Bucket2360388A"
"Ref": "AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915S3Bucket7871AF0F"
},
"S3Key": {
"Fn::Join": [
Expand All @@ -122,7 +122,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParameters44223b510152d51e381791e0347041da535987f7d6f1703b343f9c06f6075059S3VersionKey012A5944"
"Ref": "AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915S3VersionKey106B28FA"
}
]
}
Expand All @@ -135,7 +135,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParameters44223b510152d51e381791e0347041da535987f7d6f1703b343f9c06f6075059S3VersionKey012A5944"
"Ref": "AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915S3VersionKey106B28FA"
}
]
}
Expand Down Expand Up @@ -230,17 +230,17 @@
}
},
"Parameters": {
"AssetParameters44223b510152d51e381791e0347041da535987f7d6f1703b343f9c06f6075059S3Bucket2360388A": {
"AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915S3Bucket7871AF0F": {
"Type": "String",
"Description": "S3 bucket for asset \"44223b510152d51e381791e0347041da535987f7d6f1703b343f9c06f6075059\""
"Description": "S3 bucket for asset \"ba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915\""
},
"AssetParameters44223b510152d51e381791e0347041da535987f7d6f1703b343f9c06f6075059S3VersionKey012A5944": {
"AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915S3VersionKey106B28FA": {
"Type": "String",
"Description": "S3 key for asset version \"44223b510152d51e381791e0347041da535987f7d6f1703b343f9c06f6075059\""
"Description": "S3 key for asset version \"ba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915\""
},
"AssetParameters44223b510152d51e381791e0347041da535987f7d6f1703b343f9c06f6075059ArtifactHashE1243C2C": {
"AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915ArtifactHashADA31147": {
"Type": "String",
"Description": "Artifact hash for asset \"44223b510152d51e381791e0347041da535987f7d6f1703b343f9c06f6075059\""
"Description": "Artifact hash for asset \"ba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915\""
}
},
"Outputs": {
Expand Down Expand Up @@ -269,4 +269,4 @@
}
}
}
}
}

0 comments on commit f4ea264

Please sign in to comment.