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

Empty object being created when mapping an empty array of objects #72

Open
ge022 opened this issue Dec 12, 2019 · 4 comments
Open

Empty object being created when mapping an empty array of objects #72

ge022 opened this issue Dec 12, 2019 · 4 comments

Comments

@ge022
Copy link

ge022 commented Dec 12, 2019

Mapping an empty array (ex. empty result from db) to a mapping profile with a sub profile like this:

const mappingProfile = { 
"[].Id": "[].Id",
"[].file": {
    key: "[].file",
    transform: (value) => objectMapper(value, fileMappingProfile),
 }
}

returns
[ {} ]
Adding "?" to the file does not fix this. As a workaround, I am checking the source length to decide whether or not to map.

Another issue I ran into is that a nulls do not work on a sub mapping if the source is null. For example, mapping this object which has a null "file" key, does not include the key in the result:

"[].contents[].file": {
  key: "[].contents[].file",
  transform: (value) => objectMapper(value, fileMappingProfile),
},

result:

[
  {
      "Id": 3,
      "content": [
          {
              "Id": 2,
          }
      ]
  }
]

I've tried "?" and default, resulting in the same issue:

"[].contents[].file": {
  key: "[].contents[].file?",
  transform: (value) => objectMapper(value, null, fileModel),
  default: null
},
@wankdanker
Copy link
Owner

Has this worked for you in a previous version of object-mapper?

@arnaudjnn
Copy link

arnaudjnn commented May 15, 2020

Same issue. Did you find a workaround? I get empty objects.

@sylvainlegault
Copy link

It work when there is no transform function, adding the transform function always create a dest = {} object, which is the one that end up being added to the array.
My expectation is that transform should not be called if there is no value, default should be call in that case.

@GauravChinavle
Copy link

GauravChinavle commented Jul 7, 2022

{ 
           "sourceObject": {
                "key": "destObject",
                transform: function (value,source,dest) {
                    if(Array.isArray(source.rootKey) && source.rootKey.length == 0) {
                        return [];
                    } else {
                        return [...dest.destObject];
                    }
                },
                default: []
            }
 }

It worked for me. but make sure you put it at the end of the key mapping
In response i was getting and
{ response: { data: { resultingArray: [] } } }
and what object mapper was doing
{ response: { data: { resultingArray: [ {} ] } } }

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

5 participants