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

fix: support Objects created with Object.create({}) #842

Merged
merged 8 commits into from Jan 8, 2020

Conversation

schmidt-sebastian
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian commented Dec 30, 2019

This changes our plain object check to use the constructor name, and also uses the constructor name in all of our object checks throughout.

fixes: #799

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Dec 30, 2019
@codecov
Copy link

codecov bot commented Dec 30, 2019

Codecov Report

Merging #842 into master will increase coverage by 6.54%.
The diff coverage is 95.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #842      +/-   ##
==========================================
+ Coverage   88.82%   95.36%   +6.54%     
==========================================
  Files          27       25       -2     
  Lines       16755    15138    -1617     
  Branches     1156     1151       -5     
==========================================
- Hits        14882    14437     -445     
+ Misses       1868      695    -1173     
- Partials        5        6       +1
Impacted Files Coverage Δ
dev/src/types.ts 0% <ø> (-100%) ⬇️
dev/src/v1beta1/firestore_client.ts 93.23% <0%> (ø) ⬆️
dev/src/pool.ts 96.65% <100%> (+0.01%) ⬆️
dev/src/watch.ts 98.22% <100%> (-0.23%) ⬇️
dev/src/util.ts 100% <100%> (ø) ⬆️
dev/src/backoff.ts 100% <100%> (ø) ⬆️
dev/src/reference.ts 99.07% <100%> (-0.01%) ⬇️
dev/src/transaction.ts 96.65% <100%> (-0.08%) ⬇️
dev/src/validate.ts 97.32% <100%> (ø) ⬆️
dev/src/write-batch.ts 99.57% <100%> (-0.01%) ⬇️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ce5ef69...f320614. Read the comment docs.

isObject(input) &&
(Object.getPrototypeOf(input) === Object.prototype ||
Object.getPrototypeOf(input) === null ||
input.constructor.name === 'Object')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this stackoverflow post, obj.constructor can be changed by the user, so they recommend using instanceof instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, the recommendation here does not quite work for us. We don't have access to the user's type and instanceof Object returns true for custom types as well as plain objects such as {}.

It also looks like the other suggestion Object.prototype.toString.call just returns [object Object] for custom types, so that is likely out of the question too.

@@ -1073,7 +1073,7 @@ describe('getAll() method', () => {
expect(actualErrorAttempts).to.deep.eq(expectedErrorAttempts);
});
});
});
}).timeout(5000);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in master now, so the diff should eventually disappear: https://github.com/googleapis/nodejs-firestore/blob/master/dev/test/index.ts#L1103

Copy link

@thebrianchen thebrianchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in person, consider adding unit tests to match the is-plain-object results from the npm package.

dev/test/util.ts Outdated
import {isPlainObject} from '../src/util';

describe('util', () => {
it('isPlainObject supports Object.create()', () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitting for nits sake: Replace 'supports' with 'allows'?

@schmidt-sebastian schmidt-sebastian added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jan 8, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jan 8, 2020
@schmidt-sebastian schmidt-sebastian merged commit a85f0c3 into master Jan 8, 2020
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/plainobject branch January 15, 2020 04:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Unable to add documents to subcollections using the functions shell
4 participants