dregenor / jsonMapper

simple json mapper
MIT License
31 stars 8 forks source link

makeConverter modifies its arguments #10

Closed moxious closed 8 years ago

moxious commented 8 years ago

When you call makeConverter on an object, the implementation of that function actually destructively modifies the source object.

Sample test case to show the issue:

> var jm = require('json-mapper');
undefined
> var myConverter = { 'foo': 'bar', subDocument: { hello: 'world' } };
undefined
> var converter = jm.makeConverter(myConverter);
undefined
> myConverter.foo
[Function]
> myConverter.subDocument
[Function]
> 

The source code that modifies the original object is here

This was the cause of a quite difficult bug; in my application we create mapping classes and we have other uses for those mapping classes besides feeding them into JSON-mapper to create converters. The bug we were seeing was because json-mapper had replaced an object definition (subDocument above) with a function, it's own callback.

Principle of least surprise is that the module should not modify its arguments, instead returning a completely new object leaving the original mapping specification untouched. This makes it possible for us to keep using our data structures as-is.

Our present work-around is to deep clone the objects we have before we give them to json-mapper, so that we can retain a copy without modification.

dregenor commented 8 years ago

Thanks for the bug report. This error has been fixed. Please Upgrade to version "0.0.10".