Skip to content

Commit 83fde17

Browse files
fix: [SDK-4386] preserve null property values in trackEvent on iOS (#1946)
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 0567d68 commit 83fde17

6 files changed

Lines changed: 220 additions & 8 deletions

File tree

‎ios/RCTOneSignal/RCTOneSignalEventEmitter.mm‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,40 @@
66
#pragma GCC diagnostic push
77
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
88

9+
// Sentinel string used by the JS side of `trackEvent` to round-trip `null`
10+
// values across the React Native iOS TurboModule bridge. Must match
11+
// IOS_NULL_SENTINEL in src/constants/internal.ts byte-for-byte. The string
12+
// avoids NUL bytes because RN's convertJSIStringToNSString uses
13+
// stringWithUTF8String: which would truncate at the first NUL.
14+
// See SDK-4386.
15+
static NSString *const kOSNullSentinel = @"__OS_RN_NULL_8b3f72d6c1a04f9e__";
16+
17+
// Recursively walks `value`, returning a copy with any string equal to the
18+
// sentinel replaced by `[NSNull null]`. Containers are rebuilt; primitives
19+
// are returned as-is.
20+
static id OSDecodeNullSentinels(id value) {
21+
if ([value isKindOfClass:[NSDictionary class]]) {
22+
NSDictionary *dict = (NSDictionary *)value;
23+
NSMutableDictionary *out = [NSMutableDictionary dictionaryWithCapacity:dict.count];
24+
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
25+
out[key] = OSDecodeNullSentinels(obj);
26+
}];
27+
return out;
28+
}
29+
if ([value isKindOfClass:[NSArray class]]) {
30+
NSArray *arr = (NSArray *)value;
31+
NSMutableArray *out = [NSMutableArray arrayWithCapacity:arr.count];
32+
for (id item in arr) {
33+
[out addObject:OSDecodeNullSentinels(item)];
34+
}
35+
return out;
36+
}
37+
if ([value isKindOfClass:[NSString class]] && [(NSString *)value isEqualToString:kOSNullSentinel]) {
38+
return [NSNull null];
39+
}
40+
return value;
41+
}
42+
943
@interface RCTOneSignalEventEmitter ()
1044
- (void)emitEventWithName:(NSString *)name body:(NSDictionary *)body;
1145
@end
@@ -600,7 +634,8 @@ - (void)removeUserStateObserver {
600634

601635
RCT_EXPORT_METHOD(trackEvent : (NSString *)name
602636
properties : (NSDictionary *_Nullable)properties) {
603-
[OneSignal.User trackEventWithName:name properties:properties];
637+
NSDictionary *decoded = properties == nil ? nil : OSDecodeNullSentinels(properties);
638+
[OneSignal.User trackEventWithName:name properties:decoded];
604639
}
605640

606641
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:

‎src/constants/internal.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Sentinel string used to round-trip JS `null` values across the React Native
3+
* iOS TurboModule bridge, which otherwise drops `null` dictionary values
4+
* before they reach native code. The native side swaps this string back to
5+
* `[NSNull null]`.
6+
*
7+
* The string must not contain NUL bytes: RN's `convertJSIStringToNSString`
8+
* uses `[NSString stringWithUTF8String:]`, which terminates at the first NUL
9+
* byte and would silently truncate the sentinel to `@""`. Collision with a
10+
* customer-supplied string is avoided by the random hex suffix.
11+
*
12+
* See SDK-4386.
13+
*/
14+
export const IOS_NULL_SENTINEL = '__OS_RN_NULL_8b3f72d6c1a04f9e__';

‎src/helpers.test.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import type { NativeModule } from 'react-native';
22
import { beforeEach, describe, expect, test, vi, type MockInstance } from 'vite-plus/test';
33

4-
import { isNativeModuleLoaded, isObjectSerializable, isValidCallback } from './helpers';
4+
import { IOS_NULL_SENTINEL } from './constants/internal';
5+
import {
6+
encodeNullsForIOS,
7+
isNativeModuleLoaded,
8+
isObjectSerializable,
9+
isValidCallback,
10+
} from './helpers';
511

612
describe('helpers', () => {
713
let errorSpy: MockInstance;
@@ -103,4 +109,53 @@ describe('helpers', () => {
103109
expect(isObjectSerializable(circular)).toBe(false);
104110
});
105111
});
112+
113+
describe('encodeNullsForIOS', () => {
114+
test('replaces top-level null with the sentinel', () => {
115+
expect(encodeNullsForIOS(null)).toBe(IOS_NULL_SENTINEL);
116+
});
117+
118+
test('replaces null values inside an object', () => {
119+
expect(encodeNullsForIOS({ a: 1, b: null })).toEqual({
120+
a: 1,
121+
b: IOS_NULL_SENTINEL,
122+
});
123+
});
124+
125+
test('replaces null values inside nested objects', () => {
126+
expect(encodeNullsForIOS({ outer: { inner: null, ok: 'x' } })).toEqual({
127+
outer: { inner: IOS_NULL_SENTINEL, ok: 'x' },
128+
});
129+
});
130+
131+
test('replaces null values inside arrays', () => {
132+
expect(encodeNullsForIOS([1, null, 'x'])).toEqual([1, IOS_NULL_SENTINEL, 'x']);
133+
});
134+
135+
test('replaces null values inside mixed arrays of objects', () => {
136+
expect(encodeNullsForIOS([1, '2', { a: '3' }, null])).toEqual([
137+
1,
138+
'2',
139+
{ a: '3' },
140+
IOS_NULL_SENTINEL,
141+
]);
142+
});
143+
144+
test.each([
145+
{ description: 'a number', value: 42 },
146+
{ description: 'a float', value: 3.14 },
147+
{ description: 'a string', value: 'abc' },
148+
{ description: 'a boolean true', value: true },
149+
{ description: 'a boolean false', value: false },
150+
])('leaves $description untouched', ({ value }: { description: string; value: unknown }) => {
151+
expect(encodeNullsForIOS(value)).toBe(value);
152+
});
153+
154+
test('does not mutate the input object', () => {
155+
const input = { a: 1, b: null, nested: { c: null }, arr: [null] };
156+
const snapshot = JSON.parse(JSON.stringify(input));
157+
encodeNullsForIOS(input);
158+
expect(input).toEqual(snapshot);
159+
});
160+
});
106161
});

‎src/helpers.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import invariant from 'invariant';
22

3+
import { IOS_NULL_SENTINEL } from './constants/internal';
4+
35
export function isValidCallback(handler: Function) {
46
invariant(typeof handler === 'function', 'Must provide a valid callback');
57
}
@@ -30,3 +32,28 @@ export function isObjectSerializable(value: unknown): boolean {
3032
return false;
3133
}
3234
}
35+
36+
/**
37+
* Returns a structurally-identical clone of `value` with every `null` replaced
38+
* by `IOS_NULL_SENTINEL`. Used to round-trip `null` values across the React
39+
* Native iOS TurboModule bridge, which otherwise drops `null` dictionary
40+
* values. See SDK-4386.
41+
*/
42+
export function encodeNullsForIOS(value: Record<string, unknown>): Record<string, unknown>;
43+
export function encodeNullsForIOS(value: unknown): unknown;
44+
export function encodeNullsForIOS(value: unknown): unknown {
45+
if (value === null) {
46+
return IOS_NULL_SENTINEL;
47+
}
48+
if (Array.isArray(value)) {
49+
return value.map((item) => encodeNullsForIOS(item));
50+
}
51+
if (typeof value === 'object') {
52+
const out: Record<string, unknown> = {};
53+
for (const [k, v] of Object.entries(value)) {
54+
out[k] = encodeNullsForIOS(v);
55+
}
56+
return out;
57+
}
58+
return value;
59+
}

‎src/index.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
SUBSCRIPTION_CHANGED,
1515
USER_STATE_CHANGED,
1616
} from './constants/events';
17+
import { IOS_NULL_SENTINEL } from './constants/internal';
1718
import EventManager, { type EventListenerMap } from './events/EventManager';
1819
import * as helpers from './helpers';
1920
import { LogLevel, OneSignal, OSNotificationPermission } from './index';
@@ -840,6 +841,79 @@ describe('OneSignal', () => {
840841
expect(errorSpy).toHaveBeenCalledWith('Properties must be a JSON-serializable object');
841842
expect(mockRNOneSignal.trackEvent).not.toHaveBeenCalled();
842843
});
844+
845+
describe('iOS null handling', () => {
846+
beforeEach(() => {
847+
mockPlatform.OS = 'ios';
848+
});
849+
850+
test('replaces top-level null with sentinel', () => {
851+
OneSignal.User.trackEvent('event', { someNum: 1, someNull: null });
852+
expect(mockRNOneSignal.trackEvent).toHaveBeenCalledWith('event', {
853+
someNum: 1,
854+
someNull: IOS_NULL_SENTINEL,
855+
});
856+
});
857+
858+
test('replaces nested-object null with sentinel', () => {
859+
OneSignal.User.trackEvent('event', {
860+
someObject: { abc: '123', nested: { def: '456' }, ghi: null },
861+
});
862+
expect(mockRNOneSignal.trackEvent).toHaveBeenCalledWith('event', {
863+
someObject: { abc: '123', nested: { def: '456' }, ghi: IOS_NULL_SENTINEL },
864+
});
865+
});
866+
867+
test('replaces null inside arrays with sentinel', () => {
868+
OneSignal.User.trackEvent('event', { arr: [1, null, 'x'] });
869+
expect(mockRNOneSignal.trackEvent).toHaveBeenCalledWith('event', {
870+
arr: [1, IOS_NULL_SENTINEL, 'x'],
871+
});
872+
});
873+
874+
test('replaces null inside mixed arrays of objects with sentinel', () => {
875+
OneSignal.User.trackEvent('event', {
876+
mixed: [1, '2', { abc: '123' }, null],
877+
});
878+
expect(mockRNOneSignal.trackEvent).toHaveBeenCalledWith('event', {
879+
mixed: [1, '2', { abc: '123' }, IOS_NULL_SENTINEL],
880+
});
881+
});
882+
883+
test('leaves primitives untouched', () => {
884+
const props = { n: 1, f: 3.14, s: 'abc', b: true };
885+
OneSignal.User.trackEvent('event', props);
886+
expect(mockRNOneSignal.trackEvent).toHaveBeenCalledWith('event', props);
887+
});
888+
889+
test('does not mutate the caller-supplied properties object', () => {
890+
const props = {
891+
top: null,
892+
nested: { ghi: null },
893+
arr: [1, null],
894+
};
895+
const snapshot = JSON.parse(JSON.stringify(props));
896+
OneSignal.User.trackEvent('event', props);
897+
expect(props).toEqual(snapshot);
898+
});
899+
});
900+
901+
describe('Android null handling', () => {
902+
beforeEach(() => {
903+
mockPlatform.OS = 'android';
904+
});
905+
906+
test('passes properties through unchanged (no sentinel substitution)', () => {
907+
const props = {
908+
someNum: 123,
909+
someNull: null,
910+
someObject: { ghi: null },
911+
arr: [1, null],
912+
};
913+
OneSignal.User.trackEvent('event', props);
914+
expect(mockRNOneSignal.trackEvent).toHaveBeenCalledWith('event', props);
915+
});
916+
});
843917
});
844918

845919
describe('Notifications', () => {

‎src/index.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import {
1515
import type { OSNotificationPermission } from './constants/subscription';
1616
import EventManager from './events/EventManager';
1717
import NotificationWillDisplayEvent from './events/NotificationWillDisplayEvent';
18-
import { isNativeModuleLoaded, isObjectSerializable, isValidCallback } from './helpers';
18+
import {
19+
encodeNullsForIOS,
20+
isNativeModuleLoaded,
21+
isObjectSerializable,
22+
isValidCallback,
23+
} from './helpers';
1924
import NativeOneSignal from './NativeOneSignal';
2025
import type {
2126
InAppMessage,
@@ -538,10 +543,7 @@ export namespace OneSignal {
538543
return tags as { [key: string]: string };
539544
}
540545

541-
/**
542-
* Track custom events for the current user.
543-
* Note: Currently, null values will be omitted for Android.
544-
* */
546+
/** Track custom events for the current user. */
545547
export function trackEvent(name: string, properties: Record<string, unknown> = {}) {
546548
if (!isNativeModuleLoaded(RNOneSignal)) return;
547549

@@ -550,7 +552,12 @@ export namespace OneSignal {
550552
return;
551553
}
552554

553-
RNOneSignal.trackEvent(name, properties);
555+
// The iOS TurboModule bridge drops dictionary entries whose value is
556+
// `null`. Encode nulls as a sentinel string so the native side can
557+
// restore them as `NSNull`. See SDK-4386.
558+
const payload = Platform.OS === 'ios' ? encodeNullsForIOS(properties) : properties;
559+
560+
RNOneSignal.trackEvent(name, payload);
554561
}
555562
}
556563

0 commit comments

Comments
 (0)