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

Transform runs multiple times in nested array #77

Open
jxeeno opened this issue Mar 11, 2020 · 10 comments · May be fixed by #90
Open

Transform runs multiple times in nested array #77

jxeeno opened this issue Mar 11, 2020 · 10 comments · May be fixed by #90

Comments

@jxeeno
Copy link

jxeeno commented Mar 11, 2020

Hello!

I have a use case where I'm trying to transform a key in a deeply nested array + object structure. See example below:

var objectMapper = require('object-mapper');
var map = {
    '[].data.children[].person_id': {key: 'entity[].humans.children[].person.id', transform: (e) => e+'_+1'},
};
var arr = [{
  "id": "1",
  "data": {
    "children": [
      {
        "person_id": "123123123"
      },
      {
        "person_id": "12312312"
      },
      {
        "relation_id": "111"
      }
    ]
  }
}];
 
var dest = objectMapper(arr, map);

console.log(JSON.stringify(dest, null, 4))

I was expecting an output of:

{
    "entity": [        {
            "humans": {
                "children": [                    {
                        "person": {
                            "id": "123123123_+1"
                        }
                    },
                    {
                        "person": {
                            "id": "12312312_+1"
                        }                    }                ]
            }
        }    ]
}

Instead, I get:

{
    "entity": [        {
            "humans": {
                "children": [                    {
                        "person": {
                            "id": "123123123,12312312_+1_+1"
                        }
                    }
                ]
            }
        }
    ]}

I'm doing some further debugging now, but it appears the transform() function gets called multiple times in the array iteration.

@PasVV
Copy link

PasVV commented Mar 18, 2020

I have updated deps of my project and i catch this bug too (object-mapper updated too from 6.0.1 to 6.2.0).

I will try localize problem.

@PasVV
Copy link

PasVV commented Mar 18, 2020

Problem reveals on 6.0.1 => 6.1.0 update.

@pguijarr, @wankdanker could you check this changes and help us find bug?

@gitricko
Copy link

gitricko commented May 7, 2020

ah... this is the problem...same for me #81 ... Thanks for the heads up on the version. Yes, 6.0.1 does not have this problem ! I will close #81

@denisdnc
Copy link

Same issue using "6.2.0"

@brunomillerps
Copy link

I had the same issue using version 6.2.0

@mwiesmueller
Copy link

Same issue on version 6.2.0 - This breaks the show in my project. :-(

@henryjw
Copy link

henryjw commented Nov 6, 2020

I'm seeing the same issue. My workaround is to define any properties that don't use a transform function first.

const map = {
    "foo": [
        {
            key: "y" // Works a s expected
        },
        {
            key: "foo",
            transform: function (value: string) {
                return value + "_foo";
            }
        },
        {
            key: "a" // This will use foo's transform function
        },
        {
            key: "baz",
            transform: function (value: string) {
                return value + "_baz";
            },
        }
    ],
    "bar": "x"
};

const src = {
    foo: 'blah',
    bar: 'something'
};

console.log(objectMapper.merge(src, map))

@mhuijser
Copy link

This is quite a serieus problem. Multiple transformations are just not possible without interfering with each other. Is there an outlook on a fix ? That would be awesome !

@abhinavjuneja
Copy link

is there any update on this?

@peterbarretto
Copy link

Is there any update on this issue?

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

Successfully merging a pull request may close this issue.

10 participants