Skip to content

Commit 1ee969d

Browse files
fix(breadcrumbs): Crash when passing array as data to addBreadcrumb (#4021)
The expected `data` type is plain JS object, otherwise the data might be lost.
1 parent 79930f1 commit 1ee969d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

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

1416
### Dependencies
1517

src/js/utils/normalize.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ const KEY = 'value';
88
// eslint-disable-next-line @typescript-eslint/no-explicit-any
99
export function convertToNormalizedObject(data: unknown): Record<string, any> {
1010
const normalized: unknown = normalize(data);
11-
if (normalized === null || typeof normalized !== 'object') {
11+
if (
12+
normalized !== null &&
13+
typeof normalized === 'object' &&
14+
!Array.isArray(normalized) &&
15+
normalized.constructor === Object
16+
) {
17+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
18+
return normalized as Record<string, any>;
19+
} else {
1220
return {
1321
[KEY]: normalized,
1422
};
15-
} else {
16-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
17-
return normalized as Record<string, any>;
1823
}
1924
}

test/utils/normalize.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,18 @@ describe('normalize', () => {
2121
const actualResult = convertToNormalizedObject(null);
2222
expect(actualResult).toEqual({ value: null });
2323
});
24+
25+
test('converts array to an object', () => {
26+
const actualResult = convertToNormalizedObject([]);
27+
expect(actualResult).toEqual({ value: [] });
28+
});
29+
30+
test('converts custom class to an object', () => {
31+
class TestClass {
32+
test: string = 'foo';
33+
}
34+
const actualResult = convertToNormalizedObject(new TestClass());
35+
expect(actualResult).toEqual({ test: 'foo' });
36+
});
2437
});
2538
});

0 commit comments

Comments
 (0)