Skip to content

Commit e0459b3

Browse files
fix(sdk): Do not overwrite _metadata option by default sdkInfo (#3036)
1 parent 3827071 commit e0459b3

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

77
- Store envelopes immediately during a fatal crash on iOS ([#3031](https://github.com/getsentry/sentry-react-native/pull/3031))
8+
- Do not overwrite `_metadata` option by default `sdkInfo` ([#3036](https://github.com/getsentry/sentry-react-native/pull/3036))
89

910
### Dependencies
1011

src/js/integrations/sdkinfo.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class SdkInfo implements Integration {
2929
*/
3030
public name: string = SdkInfo.id;
3131

32-
private _nativeSdkInfo: Package | null = null;
32+
private _nativeSdkPackage: Package | null = null;
3333

3434
/**
3535
* @inheritDoc
@@ -38,9 +38,9 @@ export class SdkInfo implements Integration {
3838
addGlobalEventProcessor(async event => {
3939
// The native SDK info package here is only used on iOS as `beforeSend` is not called on `captureEnvelope`.
4040
// this._nativeSdkInfo should be defined a following time so this call won't always be awaited.
41-
if (NATIVE.platform === 'ios' && this._nativeSdkInfo === null) {
41+
if (NATIVE.platform === 'ios' && this._nativeSdkPackage === null) {
4242
try {
43-
this._nativeSdkInfo = await NATIVE.fetchNativeSdkInfo();
43+
this._nativeSdkPackage = await NATIVE.fetchNativeSdkInfo();
4444
} catch (e) {
4545
// If this fails, go ahead as usual as we would rather have the event be sent with a package missing.
4646
logger.warn(
@@ -51,14 +51,14 @@ export class SdkInfo implements Integration {
5151
}
5252

5353
event.platform = event.platform || 'javascript';
54-
event.sdk = {
55-
...(event.sdk ?? {}),
56-
...defaultSdkInfo,
57-
packages: [
58-
...((event.sdk && event.sdk.packages) || []),
59-
...((this._nativeSdkInfo && [this._nativeSdkInfo]) || []),
60-
],
61-
};
54+
event.sdk = event.sdk || {};
55+
event.sdk.name = event.sdk.name || defaultSdkInfo.name;
56+
event.sdk.version = event.sdk.version || defaultSdkInfo.version;
57+
event.sdk.packages = [
58+
// default packages are added by baseclient and should not be added here
59+
...(event.sdk.packages || []),
60+
...((this._nativeSdkPackage && [this._nativeSdkPackage]) || []),
61+
];
6262

6363
return event;
6464
});

test/integrations/sdkinfo.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Event, EventHint, Package } from '@sentry/types';
22

3+
import { SDK_NAME, SDK_VERSION } from '../../src/js';
34
import { SdkInfo } from '../../src/js/integrations';
45
import { NATIVE } from '../../src/js/wrapper';
56

@@ -57,6 +58,29 @@ describe('Sdk Info', () => {
5758
expect(processedEvent?.platform === 'javascript');
5859
expect(mockedFetchNativeSdkInfo).toBeCalledTimes(1);
5960
});
61+
62+
it('Does not overwrite existing sdk name and version', async () => {
63+
mockedFetchNativeSdkInfo = jest.fn().mockResolvedValue(null);
64+
const mockEvent: Event = {
65+
sdk: {
66+
name: 'test-sdk',
67+
version: '1.0.0',
68+
},
69+
};
70+
const processedEvent = await executeIntegrationFor(mockEvent);
71+
72+
expect(processedEvent?.sdk?.name).toEqual('test-sdk');
73+
expect(processedEvent?.sdk?.version).toEqual('1.0.0');
74+
});
75+
76+
it('Does use default sdk name and version', async () => {
77+
mockedFetchNativeSdkInfo = jest.fn().mockResolvedValue(null);
78+
const mockEvent: Event = {};
79+
const processedEvent = await executeIntegrationFor(mockEvent);
80+
81+
expect(processedEvent?.sdk?.name).toEqual(SDK_NAME);
82+
expect(processedEvent?.sdk?.version).toEqual(SDK_VERSION);
83+
});
6084
});
6185

6286
function executeIntegrationFor(mockedEvent: Event, mockedHint: EventHint = {}): Promise<Event | null> {

0 commit comments

Comments
 (0)