diff --git a/CHANGELOG.md b/CHANGELOG.md index 23cd8cf07..ac9bdf459 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/js/utils/normalize.ts b/src/js/utils/normalize.ts index fb3245f99..2a6214b2f 100644 --- a/src/js/utils/normalize.ts +++ b/src/js/utils/normalize.ts @@ -8,12 +8,17 @@ const KEY = 'value'; // eslint-disable-next-line @typescript-eslint/no-explicit-any export function convertToNormalizedObject(data: unknown): Record { 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; + } else { return { [KEY]: normalized, }; - } else { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return normalized as Record; } } diff --git a/test/utils/normalize.test.ts b/test/utils/normalize.test.ts index cfd5cf756..7aa338417 100644 --- a/test/utils/normalize.test.ts +++ b/test/utils/normalize.test.ts @@ -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' }); + }); }); });