Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk): Do not overwrite _metadata option by default sdkInfo #3036

Merged
merged 3 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

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

### Dependencies

Expand Down
22 changes: 11 additions & 11 deletions src/js/integrations/sdkinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class SdkInfo implements Integration {
*/
public name: string = SdkInfo.id;

private _nativeSdkInfo: Package | null = null;
private _nativeSdkPackage: Package | null = null;

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

event.platform = event.platform || 'javascript';
event.sdk = {
...(event.sdk ?? {}),
...defaultSdkInfo,
packages: [
...((event.sdk && event.sdk.packages) || []),
...((this._nativeSdkInfo && [this._nativeSdkInfo]) || []),
],
};
event.sdk = event.sdk || {};
event.sdk.name = event.sdk.name || defaultSdkInfo.name;
event.sdk.version = event.sdk.version || defaultSdkInfo.version;
event.sdk.packages = [
// default packages are added by baseclient and should not be added here
...(event.sdk.packages || []),
...((this._nativeSdkPackage && [this._nativeSdkPackage]) || []),
];

return event;
});
Expand Down
24 changes: 24 additions & 0 deletions test/integrations/sdkinfo.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Event, EventHint, Package } from '@sentry/types';

import { SDK_NAME, SDK_VERSION } from '../../src/js';
import { SdkInfo } from '../../src/js/integrations';
import { NATIVE } from '../../src/js/wrapper';

Expand Down Expand Up @@ -57,6 +58,29 @@ describe('Sdk Info', () => {
expect(processedEvent?.platform === 'javascript');
expect(mockedFetchNativeSdkInfo).toBeCalledTimes(1);
});

it('Does not overwrite existing sdk name and version', async () => {
mockedFetchNativeSdkInfo = jest.fn().mockResolvedValue(null);
const mockEvent: Event = {
sdk: {
name: 'test-sdk',
version: '1.0.0',
},
};
const processedEvent = await executeIntegrationFor(mockEvent);

expect(processedEvent?.sdk?.name).toEqual('test-sdk');
expect(processedEvent?.sdk?.version).toEqual('1.0.0');
});

it('Does use default sdk name and version', async () => {
mockedFetchNativeSdkInfo = jest.fn().mockResolvedValue(null);
const mockEvent: Event = {};
const processedEvent = await executeIntegrationFor(mockEvent);

expect(processedEvent?.sdk?.name).toEqual(SDK_NAME);
expect(processedEvent?.sdk?.version).toEqual(SDK_VERSION);
});
});

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