Open ge022 opened 4 years ago
Has this worked for you in a previous version of object-mapper?
Same issue. Did you find a workaround? I get empty objects.
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.
{
"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: [ {} ] } } }
Mapping an empty array (ex. empty result from db) to a mapping profile with a sub profile like this:
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:
result:
I've tried "?" and default, resulting in the same issue: