Skip to content

Commit

Permalink
fix(breadcrumbs): Crash when passing array as data to addBreadcrumb (
Browse files Browse the repository at this point in the history
…#4021)

The expected `data` type is plain JS object, otherwise the data might be lost.
  • Loading branch information
krystofwoldrich authored Aug 13, 2024
1 parent 79930f1 commit 1ee969d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
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' });
});
});
});

0 comments on commit 1ee969d

Please sign in to comment.