wankdanker / node-object-mapper

Copy properties from one object to another.
MIT License
276 stars 73 forks source link

Missing fromObject creates empty object #48

Open alemures opened 6 years ago

alemures commented 6 years ago

The following code produces an empty object as result:

var obj = {
  a: 'b'
};
var expect = {
};
var map = {
  z: 'a.b'
};
om(obj, map); // --> { a: {} }

I think it's an unexpected behavior, what do you think?

wankdanker commented 6 years ago

I agree, it should just return an empty object. Is that what you are thinking? I mean like this:

var obj = {
  a: 'b'
};
var expect = {
};
var map = {
  z: 'a.b'
};
om(obj, map); // --> {}
alemures commented 6 years ago

Yep, i may have a chance to try to fix it through the weekend ;).

alemures commented 6 years ago

I have actually a solution for this weird behaviour but, one of the previous tests have been broken. The broken test is called "map object to another - prevent null values from being mapped", and it's exactly reproducing the behaviour we want to remove when an empty object is created even if it doesn't match a key on "map" object.

That's the code of the broken test:

var obj = {
  "a" : 1234,
  "foo": {
    "bar": null
  }
};

var expect = {
  foo:{
    a:1234
  },
  bar:{
  }
};

var map = {
  'foo.bar' : 'bar.bar',
  'a': 'foo.a'
 };

Based on what we spoke, the key "bar:{}" should not be expected so, i suggest that a change in that specific test is necessary in order to add this update along with a version bump.

Any thoughts about that?

davisc commented 5 years ago

Hi there. Checking to see if this issue has been fixed or if there is a valid work around?

switzer commented 4 years ago

I added this test to my branch. My expectation differs from yours though:

  var expect = {
    foo:{
      a:1234
    }
  };

Note that ObjectMapper does not store any key that has a NULL value, unless the null key is set to true in object notation. If you think differently, please let me know and this can be debated.