Skip to content

Commit 5b03f4d

Browse files
committed
sdk-core: fix jsonSize returning invalid sizes for objects with toJSON
1 parent 271684b commit 5b03f4d

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

packages/sdk-core/src/common/jsonSize.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,7 @@ function arraySize(array: unknown[], replacer?: (this: unknown, key: string, val
3636
}
3737

3838
const objectSize = (obj: object, replacer?: (this: unknown, key: string, value: unknown) => unknown): number => {
39-
let jsonObject: object;
40-
if ('toJSON' in obj && typeof obj.toJSON === 'function') {
41-
jsonObject = obj.toJSON() as object;
42-
} else {
43-
jsonObject = obj;
44-
}
45-
46-
const entries = Object.entries(jsonObject);
39+
const entries = Object.entries(obj);
4740
const bracketLength = 2;
4841

4942
let entryCount = 0;
@@ -93,6 +86,10 @@ function _jsonSize(
9386
value: unknown,
9487
replacer?: (this: unknown, key: string, value: unknown) => unknown,
9588
): number {
89+
if (value && typeof value === 'object' && 'toJSON' in value && typeof value.toJSON === 'function') {
90+
value = value.toJSON() as object;
91+
}
92+
9693
value = replacer ? replacer.call(parent, key, value) : value;
9794
if (value === null) {
9895
return nullSize;

packages/sdk-core/tests/common/jsonSize.spec.ts

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,17 @@ describe('jsonSize', () => {
328328
expect(actual).toEqual(expected);
329329
});
330330

331+
it('should compute object size with Date value', () => {
332+
const value = {
333+
date: new Date(),
334+
};
335+
336+
const expected = JSON.stringify(value).length;
337+
338+
const actual = jsonSize(value);
339+
expect(actual).toEqual(expected);
340+
});
341+
331342
it('should compute object size with every key type', () => {
332343
const value = {
333344
num: 123,
@@ -458,6 +469,15 @@ describe('jsonSize', () => {
458469
expect(actual).toEqual(expected);
459470
});
460471

472+
it('should compute array size with Date value', () => {
473+
const value = [new Date()];
474+
475+
const expected = JSON.stringify(value).length;
476+
477+
const actual = jsonSize(value);
478+
expect(actual).toEqual(expected);
479+
});
480+
461481
it('should compute array size with every value type', () => {
462482
const value = [
463483
123,
@@ -466,6 +486,7 @@ describe('jsonSize', () => {
466486
false,
467487
null,
468488
undefined,
489+
new Date(),
469490
Symbol.for('symbol'),
470491
(arg: number) => {},
471492
['123'],
@@ -507,24 +528,21 @@ describe('jsonSize', () => {
507528
expect(actual).toEqual(expected);
508529
});
509530

510-
// TODO: This case reports invalid size (36 vs expected 28)
511-
// As most likely this will be rarely used, and the difference is not that large,
512-
// I'm leaving this commented for now
513-
// it('should compute object size for self-referencing object with toJSON', () => {
514-
// const value = {
515-
// a: {
516-
// b: {
517-
// toJSON() {
518-
// return value;
519-
// },
520-
// },
521-
// },
522-
// };
531+
it('should compute object size for self-referencing object with toJSON', () => {
532+
const value = {
533+
a: {
534+
b: {
535+
toJSON() {
536+
return value;
537+
},
538+
},
539+
},
540+
};
523541

524-
// const expected = JSON.stringify(value, jsonEscaper()).length;
525-
// const actual = jsonSize(value, jsonEscaper());
542+
const expected = JSON.stringify(value, jsonEscaper()).length;
543+
const actual = jsonSize(value, jsonEscaper());
526544

527-
// expect(actual).toEqual(expected);
528-
// });
545+
expect(actual).toEqual(expected);
546+
});
529547
});
530548
});

0 commit comments

Comments
 (0)