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(breadcrumbs): Crash when passing array as data to addBreadcrumb. #4021

Merged
merged 2 commits into from
Aug 13, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

- fix(ttid): End and measure TTID regardless current active span ([#4019](https://github.com/getsentry/sentry-react-native/pull/4019))
- Fixes possible missing TTID measurements and spans
- Fix crash when passing array as data to `Sentry.addBreadcrumb({ data: [] })` ([#4021](https://github.com/getsentry/sentry-react-native/pull/4021))
- The expected `data` type is plain JS object, otherwise the data might be lost.

### Dependencies

Expand Down
13 changes: 9 additions & 4 deletions src/js/utils/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ const KEY = 'value';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function convertToNormalizedObject(data: unknown): Record<string, any> {
const normalized: unknown = normalize(data);
if (normalized === null || typeof normalized !== 'object') {
if (
normalized !== null &&
typeof normalized === 'object' &&
!Array.isArray(normalized) &&
normalized.constructor === Object
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return normalized as Record<string, any>;
} else {
return {
[KEY]: normalized,
};
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return normalized as Record<string, any>;
}
}
13 changes: 13 additions & 0 deletions test/utils/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,18 @@ describe('normalize', () => {
const actualResult = convertToNormalizedObject(null);
expect(actualResult).toEqual({ value: null });
});

test('converts array to an object', () => {
const actualResult = convertToNormalizedObject([]);
expect(actualResult).toEqual({ value: [] });
});

test('converts custom class to an object', () => {
class TestClass {
test: string = 'foo';
}
const actualResult = convertToNormalizedObject(new TestClass());
expect(actualResult).toEqual({ test: 'foo' });
});
});
});
Loading