Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing fromObject creates empty object #48

Open
alemures opened this issue Jan 26, 2018 · 5 comments
Open

Missing fromObject creates empty object #48

alemures opened this issue Jan 26, 2018 · 5 comments

Comments

@alemures
Copy link
Contributor

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
Copy link
Owner

wankdanker commented Jan 26, 2018

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
Copy link
Contributor Author

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

@alemures
Copy link
Contributor Author

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
Copy link

davisc commented Nov 17, 2018

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

switzer pushed a commit to switzer/node-object-mapper that referenced this issue Oct 11, 2019
@switzer
Copy link

switzer commented Oct 11, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants