diff --git a/src/helpers.test.ts b/src/helpers.test.ts index 0c98b7d5..8d904ebf 100644 --- a/src/helpers.test.ts +++ b/src/helpers.test.ts @@ -122,6 +122,14 @@ describe('helpers', () => { }); }); + test('preserves an own __proto__ property', () => { + const input = JSON.parse('{"__proto__":null}'); + const encoded = encodeNullsForIOS(input) as Record; + + expect(Object.prototype.hasOwnProperty.call(encoded, '__proto__')).toBe(true); + expect(encoded['__proto__']).toBe(IOS_NULL_SENTINEL); + }); + test('replaces null values inside nested objects', () => { expect(encodeNullsForIOS({ outer: { inner: null, ok: 'x' } })).toEqual({ outer: { inner: IOS_NULL_SENTINEL, ok: 'x' }, diff --git a/src/helpers.ts b/src/helpers.ts index a383623b..ca6e0f41 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -49,11 +49,9 @@ export function encodeNullsForIOS(value: unknown): unknown { return value.map((item) => encodeNullsForIOS(item)); } if (typeof value === 'object') { - const out: Record = {}; - for (const [k, v] of Object.entries(value)) { - out[k] = encodeNullsForIOS(v); - } - return out; + return Object.fromEntries( + Object.entries(value).map(([key, item]) => [key, encodeNullsForIOS(item)]), + ); } return value; }