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

hasPath does not work if one attribute in the path is undefined #2771

Closed
sespinoza-dev opened this issue Jan 23, 2019 · 17 comments
Closed

hasPath does not work if one attribute in the path is undefined #2771

sespinoza-dev opened this issue Jan 23, 2019 · 17 comments

Comments

@sespinoza-dev
Copy link

hasPath does not work if one attribute in the path is undefined

If one of the object attributes in the path is undefined, hasPath throws an error. it should return false.

R.hasPath(['a', 'b'], { a: undefined });
=> TypeError: Cannot convert undefined or null to object
// expected false
@CrossEye
Copy link
Member

This is definitely a bug. Are you interested in creating a PR?

@sespinoza-dev
Copy link
Author

I'll give it a try.

Greettings!

@machad0
Copy link

machad0 commented Jan 31, 2019

Hi @sespinozj, are you still into this issue? If you don't, I'd like to try

cheers

@sespinoza-dev
Copy link
Author

@machad0 do please go ahead. :)

@machad0
Copy link

machad0 commented Jan 31, 2019

Hi again,

Well, actually this bug was fixed at #2738, it's just an outdated bundle.

var hasPath = _curry2(function hasPath(_path, obj) {
...
  while (idx < _path.length) {
    if (_has(_path[idx], val)) {
      val = val[_path[idx]];
      idx += 1;
    } else {
      return false;
    }
 

the method doesn't contains the updated hasPath version, should we bump it?

@sespinoza-dev
Copy link
Author

yes, thanks for checking that out @machad0

We use pathOr as a workaround in some cases thou.

Greetings!

@CrossEye
Copy link
Member

CrossEye commented Feb 1, 2019

Damn, I'm so caught up in what I want to do soon that I've forgotten what we've done recently!

@gouegd
Copy link

gouegd commented Apr 8, 2019

Just got bit by this bug, I don't think the fix is in the latest version (or it has come back):
R.hasPath(['a', 'b'], { a: undefined })
and
R.hasPath(['a', 'b'], { a: { b: undefined } })
both crash.

pathOr is a good workaround... unless you actually want to differentiate the case when a path exists with an undefined value, and the case where it does not exist.

@mattgrande
Copy link
Contributor

mattgrande commented Apr 9, 2019

@gouegd - First one fails for me, but the second one seems to work? (returns true as expected)

Edit: This is strange... It failed in the REPL for me, but I just pulled master, wrote a test that should fail, from what I can tell, and it's passing...

it('returns false for paths with undefined values', function() {
  eq(R.hasPath(['a', 'b'], { a: undefined }), false);
});

@gouegd
Copy link

gouegd commented Apr 9, 2019

@mattgrande That means the fix for #1 is on master but not published, doesn't it ?

@mattgrande
Copy link
Contributor

That's what it seems like, yes.

@CrossEye
Copy link
Member

I've been away from Ramda for a few weeks, and am trying to get back. I will try to publish again soon, and this change should then be included.

@gahabeen
Copy link

gahabeen commented May 10, 2019

Seems like there is still an issue. If the sub value isn't an Object (Object or Array), it fails.

I ended up with this exact error message:

C:\[PATH]\ramda\src\internal\_has.js:2
  return Object.prototype.hasOwnProperty.call(obj, prop);
                                         ^
TypeError: Cannot convert undefined or null to object
    at hasOwnProperty (<anonymous>)
    at _has (C:\[PATH]\ramda\src\internal\_has.js:2:42)

Fixed it by simply checking if the value is indeed an object and can be checked against a sub property with && is(Object, val) as seen above in context.

// hasPath function
...
while (idx < _path.length) {
    if (!isNil(val) && is(Object, val) && _has(_path[idx], val)) {
      val = val[_path[idx]];
      idx += 1;
    } else {
      return false;
    }
  }
...

:)

@LorneCurrie
Copy link
Contributor

Created a pull request #2825 for this issue

CrossEye pushed a commit that referenced this issue May 15, 2019
* hasPath return false for non-object checks

Used to throw an Error for non-object children tests
Added test for object checking if val is an object when testing a path
Added tests testing false is returned for non-objects children tests
Related to github issue #2771

* Linting the file

* Removed change as added to tests
@PaulGrimshaw
Copy link

Is this going to be fixed? We are still getting this issue...

@gulsicka
Copy link

gulsicka commented Dec 9, 2021

the issue still exists

@CrossEye
Copy link
Member

CrossEye commented Dec 9, 2021

@gulsicka: Can you demonstrate? It seems to be working correctly in my test.

@CrossEye CrossEye closed this as completed Dec 9, 2021
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

9 participants