Skip to content

Commit 0b4b331

Browse files
committed
Fix: Stringify didn't serialize inherited properties
1 parent ae1026a commit 0b4b331

8 files changed

+92
-7
lines changed

dist/exceptionless.js

Lines changed: 11 additions & 0 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: 2 additions & 2 deletions
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.

dist/exceptionless.node.js

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

dist/exceptionless.node.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: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,56 @@ describe('Utils', () => {
7474
expect(Utils.stringify([{ one: afoo, two: afoo }])).to.equal('[{"one":{"a":"foo"}}]');
7575
});
7676

77-
it('should behave like JSON.stringify', () => {
78-
expect(Utils.stringify(user)).to.equal(JSON.stringify(user));
77+
it.skip('deep circular reference', () => {
78+
let a:any = {};
79+
let b:any = {};
80+
let c:any = { d: 'test' };
81+
82+
a.b = b;
83+
b.c = c;
84+
c.a = a;
85+
86+
let expected = '{"b":{"c":{"d":"test"}}}';
87+
88+
let actual = Utils.stringify(a);
89+
expect(actual).to.equal(expected);
90+
});
91+
92+
describe('should behave like JSON.stringify', () => {
93+
[new Date(), 1, true, null, undefined, function() { }, user].forEach(value => {
94+
it('for ' + typeof (value), () => {
95+
expect(Utils.stringify(value)).to.equal(JSON.stringify(value));
96+
});
97+
});
98+
});
99+
100+
it.skip('should respect maxDepth', () => {
101+
let deepObject = {
102+
a: {
103+
b: {
104+
c: {
105+
d: {}
106+
}
107+
}
108+
}
109+
};
110+
111+
expect(deepObject).to.equal('TODO');
112+
});
113+
114+
it('should serialize inherited properties', () => {
115+
let Foo = function() { this.a = 'a'; };
116+
let Bar = function() { this.b = 'b'; };
117+
Bar.prototype = new Foo();
118+
let bar = new Bar();
119+
120+
let expected = {
121+
a: 'a',
122+
b: 'b'
123+
};
124+
125+
let result = JSON.parse(Utils.stringify(bar));
126+
expect(result).to.eql(expected);
79127
});
80128

81129
describe('with exclude pattern', () => {

src/Utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ export class Utils {
192192
});
193193
}
194194

195+
if (({}).toString.call(data) === '[object Object]') {
196+
let flattened = {};
197+
/* tslint:disable:forin */
198+
for (let prop in data) {
199+
let value = data[prop];
200+
if (value === data) {
201+
continue;
202+
}
203+
flattened[prop] = data[prop];
204+
}
205+
/* tslint:enable:forin */
206+
207+
return stringifyImpl(flattened, exclusions);
208+
}
209+
195210
if (({}).toString.call(data) === '[object Array]') {
196211
let result = [];
197212
for (let index = 0; index < data.length; index++) {

0 commit comments

Comments
 (0)