From e6149935589aede3793cbc24a45c608c6748bb75 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Mon, 1 Feb 2021 11:40:20 -0800 Subject: [PATCH] Fix incorrect date serialization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: While creating some other tests, discovered that our current date serialization uses `toString()` serialization, causing the amount of milliseconds to be lost. The serialization (see below) uses less bytes as well since the human readable timezone isn't included. This change only affects serialization and is backward compatible. ``` ✓ test serialize and deserializeObject function for non Object input (1 ms) ✕ test makeObjectSerializable and deserializeObject function for Date input (2 ms) ✓ test makeObjectSerializable and deserializeObject function for Map of Sets ✓ test makeObjectSerializable and deserializeObject function for Map, Dates and Set with complex nesting (1 ms) ● test makeObjectSerializable and deserializeObject function for Date input expect(received).toEqual(expected) // deep equality Expected: 2021-03-01T10:31:07.205Z Received: 2021-03-01T10:31:07.000Z ``` Reviewed By: priteshrnandgaonkar Differential Revision: D26145941 fbshipit-source-id: dfd6607a4199ca46e2075027856138efb88a07f9 --- ...ization.node.js => serialization.node.tsx} | 29 ++++++++++--------- desktop/app/src/utils/serialization.tsx | 2 +- 2 files changed, 16 insertions(+), 15 deletions(-) rename desktop/app/src/utils/__tests__/{serialization.node.js => serialization.node.tsx} (95%) diff --git a/desktop/app/src/utils/__tests__/serialization.node.js b/desktop/app/src/utils/__tests__/serialization.node.tsx similarity index 95% rename from desktop/app/src/utils/__tests__/serialization.node.js rename to desktop/app/src/utils/__tests__/serialization.node.tsx index 778946ebe..411daa6a6 100644 --- a/desktop/app/src/utils/__tests__/serialization.node.js +++ b/desktop/app/src/utils/__tests__/serialization.node.tsx @@ -7,18 +7,18 @@ * @format */ -import {makeObjectSerializable, deserializeObject} from '../serialization.tsx'; +import {makeObjectSerializable, deserializeObject} from '../serialization'; class TestObject extends Object { - constructor(title: Object, map: ?Map, set: ?Set) { + constructor(title: Object, map?: Map, set?: Set) { super(); this.title = title; this.map = map; this.set = set; } title: Object; - map: ?Map; - set: ?Set; + map?: Map; + set?: Set; } test('test makeObjectSerializable function for unnested object with no Set and Map', async () => { const obj = {key1: 'value1', key2: 'value2'}; @@ -283,12 +283,13 @@ test('test serialize and deserializeObject function for non Object input', async }); test('test makeObjectSerializable and deserializeObject function for Date input', async () => { - const date = new Date('2019-02-15'); - const expectedDate = { - __flipper_object_type__: 'Date', - data: date.toString(), - }; - expect(await makeObjectSerializable(date)).toEqual(expectedDate); + const date = new Date(2021, 1, 29, 10, 31, 7, 205); + expect(await makeObjectSerializable(date)).toMatchInlineSnapshot(` + Object { + "__flipper_object_type__": "Date", + "data": "${date.toJSON()}", + } + `); expect(deserializeObject(await makeObjectSerializable(date))).toEqual(date); }); @@ -296,7 +297,7 @@ test('test makeObjectSerializable and deserializeObject function for Map of Sets const map = new Map([ ['k1', new Set([1, 2, 3, 4, 5, 6])], [new Set([1, 2]), new Map([['k3', 'v3']])], - ]); + ] as any); const expectedOutput = { __flipper_object_type__: 'Map', data: [ @@ -317,16 +318,16 @@ test('test makeObjectSerializable and deserializeObject function for Map, Dates const map = new Map([ ['k1', date1], ['k2', new Set([date2])], - ]); + ] as any); const expectedOutput = { __flipper_object_type__: 'Map', data: [ - ['k1', {__flipper_object_type__: 'Date', data: date1.toString()}], + ['k1', {__flipper_object_type__: 'Date', data: date1.toJSON()}], [ 'k2', { __flipper_object_type__: 'Set', - data: [{__flipper_object_type__: 'Date', data: date2.toString()}], + data: [{__flipper_object_type__: 'Date', data: date2.toJSON()}], }, ], ], diff --git a/desktop/app/src/utils/serialization.tsx b/desktop/app/src/utils/serialization.tsx index ae6331f8c..11c3eda6e 100644 --- a/desktop/app/src/utils/serialization.tsx +++ b/desktop/app/src/utils/serialization.tsx @@ -174,7 +174,7 @@ export async function makeObjectSerializable( } else if (element instanceof Date) { dict.set(element, { __flipper_object_type__: 'Date', - data: element.toString(), + data: element.toJSON(), }); } else if (element instanceof Array) { const {childNeedsIteration, outputArr} = processArray(