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(utils): Fix infinite recursion in dropUndefinedKeys #5163

Merged
merged 3 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 18 additions & 2 deletions packages/utils/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { WrappedFunction } from '@sentry/types';

import { htmlTreeAsString } from './browser';
import { isElement, isError, isEvent, isInstanceOf, isPlainObject, isPrimitive } from './is';
import { memoBuilder, MemoFunc } from './memo';
import { truncate } from './string';

/**
Expand Down Expand Up @@ -207,18 +208,33 @@ export function extractExceptionKeysForMessage(exception: Record<string, unknown
* Works recursively on objects and arrays.
*/
export function dropUndefinedKeys<T>(val: T): T {
Copy link
Member

Choose a reason for hiding this comment

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

Small nit: Should we update the JSDoc and mention that the function handles circular references?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, thanks. Added comment describing what the function does with circ refs - whis is also implying that it handles circ refs ^^ -> 4429b64

// This function just proxies `_dropUndefinedKeys` to keep the `memoBuilder` out of this function's API
return _dropUndefinedKeys(val, memoBuilder());
}

function _dropUndefinedKeys<T>(val: T, memo: MemoFunc): T {
const [memoize] = memo; // we don't need unmemoize because we don't need to visit nodes twice

if (isPlainObject(val)) {
if (memoize(val)) {
return val;
}
const rv: { [key: string]: any } = {};
for (const key of Object.keys(val)) {
if (typeof val[key] !== 'undefined') {
rv[key] = dropUndefinedKeys(val[key]);
rv[key] = _dropUndefinedKeys(val[key], memo);
}
}
return rv as T;
}

if (Array.isArray(val)) {
return (val as any[]).map(dropUndefinedKeys) as any;
if (memoize(val)) {
return val;
}
return (val as any[]).map(item => {
return _dropUndefinedKeys(item, memo);
}) as any;
}

return val;
Expand Down
41 changes: 41 additions & 0 deletions packages/utils/test/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,47 @@ describe('dropUndefinedKeys()', () => {
},
});
});

test('objects with circular reference', () => {
const dog: any = {
food: undefined,
};

const human = {
brain: undefined,
pets: dog,
};

const rat = {
scares: human,
weight: '4kg',
};

dog.chases = rat;

expect(dropUndefinedKeys(human)).toStrictEqual({
pets: {
chases: rat,
},
});
});

test('arrays with circular reference', () => {
const egg: any[] = [];

const chicken = {
food: undefined,
weight: '1kg',
lays: egg,
};

egg[0] = chicken;

expect(dropUndefinedKeys(chicken)).toStrictEqual({
lays: egg,
weight: '1kg',
});
});
});

describe('objectify()', () => {
Expand Down