-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
Comparing object with "hasOwnProperty" keys fails #58
Comments
https://eslint.org/docs/rules/no-prototype-builtins would catch that. |
anko
added a commit
to anko/deep-object-diff
that referenced
this issue
Oct 9, 2020
mattphillips#58 "Comparing object with "hasOwnProperty" keys fails"
anko
added a commit
to anko/deep-object-diff
that referenced
this issue
Oct 9, 2020
Fixes mattphillips#58. Previously, the code assumed that input objects have a `hasOwnProperty` function, or that at least they will acquire one when passed through `utils.properObject`. However, this assumption is flawed: As noted in issue mattphillips#58, when given input objects have a property on them called `hasOwnProperty`, it overrides the prototype's function property that the code relied on, causing any diffing function to error out with Uncaught TypeError: r.hasOwnProperty is not a function The solution taken here is to forget about `utils.properObject`, and instead introduce `utils.hasOwnProperty` which uses `Object.prototype.hasOwnProperty.call` instead of assuming anything about the input object, and replacing all direct `inputObject.hasOwnProperty` calls with it instead.
anko
added a commit
to anko/deep-object-diff
that referenced
this issue
Oct 9, 2020
The way to prevent issues like mattphillips#58 is not obvious, and I would expect not all programmers to have memorised the contents of `Object.prototype`, so I think it would be prudent to check for such issues automatically. The no-prototype-builtins rule is enforced automatically by the set of rules enabled in `.eslintrc.json` by "extends": "eslint:recommended" It's a great sign that all the other enabled rules also pass without modifications! I've added the eslint invocation to `package.json`'s `posttest` field. This should make the check minimally intrusive while developing, as it will only run when the functional tests (via `jest`) pass first.
@anko thanks for posting the fix, I've tried it and can confirm it fixes my problem. |
mattphillips
pushed a commit
to anko/deep-object-diff
that referenced
this issue
Jan 25, 2022
mattphillips#58 "Comparing object with "hasOwnProperty" keys fails"
mattphillips
pushed a commit
to anko/deep-object-diff
that referenced
this issue
Jan 25, 2022
Fixes mattphillips#58. Previously, the code assumed that input objects have a `hasOwnProperty` function, or that at least they will acquire one when passed through `utils.properObject`. However, this assumption is flawed: As noted in issue mattphillips#58, when given input objects have a property on them called `hasOwnProperty`, it overrides the prototype's function property that the code relied on, causing any diffing function to error out with Uncaught TypeError: r.hasOwnProperty is not a function The solution taken here is to forget about `utils.properObject`, and instead introduce `utils.hasOwnProperty` which uses `Object.prototype.hasOwnProperty.call` instead of assuming anything about the input object, and replacing all direct `inputObject.hasOwnProperty` calls with it instead.
mattphillips
pushed a commit
to anko/deep-object-diff
that referenced
this issue
Jan 25, 2022
The way to prevent issues like mattphillips#58 is not obvious, and I would expect not all programmers to have memorised the contents of `Object.prototype`, so I think it would be prudent to check for such issues automatically. The no-prototype-builtins rule is enforced automatically by the set of rules enabled in `.eslintrc.json` by "extends": "eslint:recommended" It's a great sign that all the other enabled rules also pass without modifications! I've added the eslint invocation to `package.json`'s `posttest` field. This should make the check minimally intrusive while developing, as it will only run when the functional tests (via `jest`) pass first.
mattphillips
pushed a commit
that referenced
this issue
Jan 25, 2022
#58 "Comparing object with "hasOwnProperty" keys fails"
mattphillips
pushed a commit
that referenced
this issue
Jan 25, 2022
Fixes #58. Previously, the code assumed that input objects have a `hasOwnProperty` function, or that at least they will acquire one when passed through `utils.properObject`. However, this assumption is flawed: As noted in issue #58, when given input objects have a property on them called `hasOwnProperty`, it overrides the prototype's function property that the code relied on, causing any diffing function to error out with Uncaught TypeError: r.hasOwnProperty is not a function The solution taken here is to forget about `utils.properObject`, and instead introduce `utils.hasOwnProperty` which uses `Object.prototype.hasOwnProperty.call` instead of assuming anything about the input object, and replacing all direct `inputObject.hasOwnProperty` calls with it instead.
mattphillips
pushed a commit
that referenced
this issue
Jan 25, 2022
The way to prevent issues like #58 is not obvious, and I would expect not all programmers to have memorised the contents of `Object.prototype`, so I think it would be prudent to check for such issues automatically. The no-prototype-builtins rule is enforced automatically by the set of rules enabled in `.eslintrc.json` by "extends": "eslint:recommended" It's a great sign that all the other enabled rules also pass without modifications! I've added the eslint invocation to `package.json`'s `posttest` field. This should make the check minimally intrusive while developing, as it will only run when the functional tests (via `jest`) pass first.
Available in v1.1.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to use deep-object-diff to compare different copies of https://github.com/mdn/browser-compat-data, which is a dataset describing the web platform itself. As such, it has a key called "hasOwnProperty" to describe that method. deep-object-diff uses code like
obj.hasOwnProperty(key)
a lot, andobj.hasOwnProperty
will in this case be an object in the BCD data, notObject.prototype.hasOwnProperty
.This causes deep-object-diff to throw an exception, the first place being here.
deep-object-diff/src/diff/index.js
Line 12 in 45549c8
Simplified a bit, here's a subset of the data:
Repro script showing the problem:
This will throw "Uncaught TypeError: r.hasOwnProperty is not a function".
A possible fix for this would be to add a
hasOwnProperty
wrapper to https://github.com/mattphillips/deep-object-diff/blob/master/src/utils/index.js and always use that, but there may be other ways.The text was updated successfully, but these errors were encountered: