avocode/json-immutable

getting type sometimes fails

Closed this issue · 6 comments

Okay, so this one is weird, and I can't seem to find where this is happening in my application (still searching), but I'm getting an object without a constructor that breaks serialization at https://github.com/avocode/json-immutable/blob/master/src/helpers/native-type-helpers.js#L7. Ideally an object has a constructor as this makes perfectly logical sense but would it be fair to add a check in lieu of a constructor's name we could fall back to a simple typeof check?

How exactly is the serialization broken for you? Can you provide an error message or some elaboration?

yeah, so an object is being passed to it comes without a constructor for some reason, error it throws is:

TypeError: Cannot read property 'name' of undefined

said code lives here: https://github.com/avocode/json-immutable/blob/master/src/helpers/native-type-helpers.js#L7

I'm still figuring out where in my application I'd get an object without a constructor... never seen anything like it before.

full stack for your perusal:

TypeError: Cannot read property 'name' of undefined
    at getObjectType (node_modules/json-immutable/lib/helpers/native-type-helpers.js:8:27)
    at Object.isDate (node_modules/json-immutable/lib/helpers/native-type-helpers.js:16:10)
    at replace (node_modules/json-immutable/lib/serialize.js:64:32)
    at node_modules/json-immutable/lib/serialize.js:207:20
    at Array.forEach (native)
    at replacePlainObject (node_modules/json-immutable/lib/serialize.js:206:20)
    at replace (node_modules/json-immutable/lib/serialize.js:69:14)
    at node_modules/json-immutable/lib/serialize.js:207:20
    at Array.forEach (native)
    at replacePlainObject (node_modules/json-immutable/lib/serialize.js:206:20)
    at replace (node_modules/json-immutable/lib/serialize.js:69:14)
    at node_modules/json-immutable/lib/serialize.js:183:30
    at node_modules/immutable/dist/immutable.js:1379:16
    at ArrayMapNode.iterate.HashCollisionNode.iterate [as iterate] (node_modules/immutable/dist/immutable.js:1728:11)
    at Map.__iterate (node_modules/immutable/dist/immutable.js:1377:32)
    at Map.forEach (node_modules/immutable/dist/immutable.js:4383:19)
    at node_modules/json-immutable/lib/serialize.js:182:14
    at replaceIterable (node_modules/json-immutable/lib/serialize.js:189:4)
    at Object.replace (node_modules/json-immutable/lib/serialize.js:61:14)
    at Object.stringify (native)
    at serialize (node_modules/json-immutable/lib/serialize.js:39:15)

My line of thinking is, "well, fix the underlying problem," to which I agree. I've monkey-patched my own application at the moment to just copy an objects key/values over but... this is a monkey-patch so...

This is indeed very strange.

The only situation in which I find this possible is when the constructor property is manipulated directly.

var x = {}
x.constructor = undefined

Yeah, it's really bizarre, seems to be coming from React-Router's history implementation, query is all messed up, I'll dig a little further and see if I can't path it up somewhere...

Please do.

I am willing to add a type checking mechanism fallback for objects with missing constructor info.

I have about zero clue why they would remove the prototype on the query object within history, but here it is:

https://github.com/ReactTraining/history/blob/33d330f8bfc5388442d6d3fa5c68b9c0bfe160e5/CHANGES.md#v300