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(core): Make filterUndefined null-safe #2789

Merged
merged 1 commit into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions packages/@aws-cdk/cdk/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ export function ignoreEmpty(obj: any): any {
}

/**
* Returns a copy of `obj` without undefined values in maps or arrays.
* Returns a copy of `obj` without `undefined` (or `null`) values in maps or arrays.
*/
export function filterUndefined(obj: any): any {
if (Array.isArray(obj)) {
return obj.filter(x => x !== undefined).map(x => filterUndefined(x));
return obj.filter(x => x != null).map(x => filterUndefined(x));
}

if (typeof(obj) === 'object') {
const ret: any = { };
for (const [key, value] of Object.entries(obj)) {
if (value === undefined) {
if (value == null) {
continue;
}
ret[key] = filterUndefined(value);
Expand Down
20 changes: 16 additions & 4 deletions packages/@aws-cdk/cdk/test/test.util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Test } from 'nodeunit';
import { Test, testCase } from 'nodeunit';
import { Stack } from '../lib';
import { capitalizePropertyNames, ignoreEmpty } from '../lib/util';
import { capitalizePropertyNames, filterUndefined, ignoreEmpty } from '../lib/util';

export = {
export = testCase({
'capitalizeResourceProperties capitalizes all keys of an object (recursively) from camelCase to PascalCase'(test: Test) {
const c = new Stack();

Expand Down Expand Up @@ -71,8 +71,20 @@ export = {
test.deepEqual(stack.resolve(ignoreEmpty({ xoo: { resolve: () => [ undefined, undefined ] }})), { xoo: [] });
test.done();
}
},

'filterUnderined': {
'is null-safe (aka treats null and undefined the same)'(test: Test) {
test.deepEqual(filterUndefined({ 'a null': null, 'a not null': true }), { 'a not null': true });
test.done();
},

'removes undefined, but leaves the rest'(test: Test) {
test.deepEqual(filterUndefined({ 'an undefined': undefined, 'yes': true }), { yes: true });
test.done();
}
}
};
});

class SomeToken {
public foo = 60;
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/cdk/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import { ConstructNode, Stack } from '../lib';

export function toCloudFormation(stack: Stack): any {
return ConstructNode.synth(stack.node, { skipValidation: true }).getStack(stack.name).template;
}
}