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

utils/setIn: fix issue when the object has a key that is one of the path's parts #569

Merged
merged 1 commit into from Apr 10, 2018

Conversation

islam3zzat
Copy link
Contributor

There is currently an issue with the setIn method that happens when the original object has a key
that is part of the provided path

setIn({x: true}, 'a.x.c', false);

and at some point in the setIn function, the currentObj is set to the originalObject[partOfPath]
and if the originalObject has a value for that the currentObj will be set to that value

    const currentPath: string = pathArray[i];
    let currentObj: any = obj[currentPath];

and then resVal will be set to this value

    //...
    } else if (currentObj) {
      resVal = resVal[currentPath] = cloneDeep(currentObj);
    } else {
    //...

and after the loop wee add a property to this value, which will raise an error

 resVal[pathArray[i]] = value;

I've added this test which will validate this issue

    it('supports path containing key of the object', () => {
      const obj = { x: 'y' };
      const newObj = setIn(obj, 'a.x.c', 'value');
      expect(obj).toEqual({ x: 'y' });
      expect(newObj).toEqual({ x: 'y', a: { x: { c: 'value' } } });
    });

which will faill

TypeError: Cannot create property 'c' on string 'y'

@islam3zzat islam3zzat changed the title fix issue when the object has a key that is one of the path's parts utils/setIn: fix issue when the object has a key that is one of the path's parts Apr 8, 2018
@islam3zzat
Copy link
Contributor Author

@jaredpalmer I've created this sandbox to illustrate how this issue can affect us
https://codesandbox.io/s/rr2ly7wznm

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 this pull request may close these issues.

None yet

2 participants