diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a3b958af5..fe529e149a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ > make sure you follow our [migration guide](https://docs.sentry.io/platforms/react-native/migration/) first. +## Unreleased + +### Fixes + +- Fix user `geo` being dropped from the native scope by forwarding it as a structured object instead of a JSON string ([#6309](https://github.com/getsentry/sentry-react-native/pull/6309)) + ## 8.15.1 ### Fixes diff --git a/packages/core/src/js/wrapper.ts b/packages/core/src/js/wrapper.ts index d4225740d6..d38737c803 100644 --- a/packages/core/src/js/wrapper.ts +++ b/packages/core/src/js/wrapper.ts @@ -449,8 +449,8 @@ export const NATIVE: SentryNativeWrapper = { } // separate and serialize all non-default user keys. - let userKeys = null; - let userDataKeys = null; + let userKeys: { [key: string]: unknown } | null = null; + let userDataKeys: { [key: string]: string } | null = null; if (user) { const { id, ip_address, email, username, geo, ...otherKeys } = user; const requiredUser: RequiredKeysUser = { @@ -461,6 +461,14 @@ export const NATIVE: SentryNativeWrapper = { geo, }; userKeys = this._serializeObject(requiredUser); + // `geo` is a structured object that the native SDKs deserialize from a + // nested map. `_serializeObject` JSON-stringifies it like the scalar keys, + // which breaks native deserialization and drops the entire user, so + // overwrite it with the object. See + // https://github.com/getsentry/sentry-react-native/issues/6306 + if (geo !== undefined) { + userKeys.geo = geo; + } userDataKeys = this._serializeObject(otherKeys); } diff --git a/packages/core/test/wrapper.test.ts b/packages/core/test/wrapper.test.ts index 838bf0fbfb..c43dfb84fd 100644 --- a/packages/core/test/wrapper.test.ts +++ b/packages/core/test/wrapper.test.ts @@ -927,11 +927,11 @@ describe('Tests Native Wrapper', () => { id: '123', email: 'test@example.com', username: 'testuser', - geo: JSON.stringify({ + geo: { city: 'San Francisco', country_code: 'US', region: 'California', - }), + }, }, { customField: 'customValue', @@ -951,10 +951,10 @@ describe('Tests Native Wrapper', () => { expect(RNSentry.setUser).toHaveBeenCalledWith( { id: '123', - geo: JSON.stringify({ + geo: { city: 'New York', country_code: 'US', - }), + }, }, {}, ); @@ -969,7 +969,7 @@ describe('Tests Native Wrapper', () => { expect(RNSentry.setUser).toHaveBeenCalledWith( { id: '123', - geo: '{}', + geo: {}, }, {}, );