Skip to content

Commit 3313ac8

Browse files
committed
Fixed a bug where the circular reference json stringify helper was eating duplicate array values.
@ejsmith, would be nice to not include this but others are doing even simpler validation...
1 parent 69c75a9 commit 3313ac8

File tree

6 files changed

+81
-23
lines changed

6 files changed

+81
-23
lines changed

dist/exceptionless.js

Lines changed: 18 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Utils-spec.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,47 @@ import { Utils } from 'Utils';
22

33
describe('Utils', () => {
44
it('should stringify circular reference', () => {
5-
var afoo:any = { a: 'foo' };
5+
var afoo:any = {a: 'foo'};
66
afoo.b = afoo;
77

88
expect(Utils.stringify(afoo)).toBe('{"a":"foo"}');
9-
expect(Utils.stringify({ one: afoo, two: afoo })).toBe('{"one":{"a":"foo"}}');
9+
expect(Utils.stringify([{one: afoo, two: afoo}])).toBe('[{"one":{"a":"foo"}}]');
10+
});
11+
12+
it('should stringify array', () => {
13+
var error = {
14+
"type": "error",
15+
"data": {
16+
"@error": {
17+
"type": "Error",
18+
"message": "string error message",
19+
"stack_trace": [
20+
{
21+
"name": "throwStringErrorImpl",
22+
"parameters": [],
23+
"file_name": "http://localhost/index.js",
24+
"line_number": 22,
25+
"column": 9
26+
},
27+
{
28+
"name": "throwStringError",
29+
"parameters": [],
30+
"file_name": "http://localhost/index.js",
31+
"line_number": 10,
32+
"column": 10
33+
}, {
34+
"name": "HTMLButtonElement.onclick",
35+
"parameters": [],
36+
"file_name": "http://localhost/",
37+
"line_number": 22,
38+
"column": 10
39+
}]
40+
}, "@submission_method": "onerror"
41+
}, "tags": []
42+
};
43+
44+
expect(Utils.stringify(error)).toBe(JSON.stringify(error));
45+
expect(Utils.stringify([error, error])).toBe(JSON.stringify([error, error]));
1046
});
1147

1248
it('should parse version from url', () => {

src/Utils.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,31 @@ export class Utils {
9292
}
9393

9494
public static stringify(data:any): string {
95-
var cache = [];
96-
97-
return JSON.stringify(data, function(key, value) {
98-
if (typeof value === 'object' && value !== null) {
99-
if (cache.indexOf(value) !== -1) {
100-
// Circular reference found, discard key
101-
return;
95+
function stringifyImpl(data:any): string {
96+
var cache = [];
97+
return JSON.stringify(data, function(key:string, value:any) {
98+
if (typeof value === 'object' && !!value) {
99+
if (cache.indexOf(value) !== -1) {
100+
// Circular reference found, discard key
101+
return;
102+
}
103+
104+
cache.push(value);
102105
}
103106

104-
cache.push(value);
107+
return value;
108+
});
109+
}
110+
111+
if (toString.call(data) === '[object Array]') {
112+
var result = [];
113+
for (var index = 0; index < data.length; index++) {
114+
result[index] = JSON.parse(stringifyImpl(data[index]));
105115
}
106116

107-
return value;
108-
});
117+
return JSON.stringify(result);
118+
}
119+
120+
return stringifyImpl(data);
109121
}
110122
}

0 commit comments

Comments
 (0)