Skip to content

Treat empty maps correctly in Document.get() #2206

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

Merged
merged 4 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Unreleased (1.5.0)
# Unreleased
- [fixed] Fixed a regression that caused queries with nested field filters to
crash the client if the field was not present in the local copy of the
document.

# 1.5.0
- [feature] Added a `Firestore.waitForPendingWrites()` method that
allows users to wait until all pending writes are acknowledged by the
Firestore backend.
Expand Down
10 changes: 5 additions & 5 deletions packages/firestore/src/model/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class Document extends MaybeDocument {
data(): ObjectValue {
if (!this.objectValue) {
let result = ObjectValue.EMPTY;
obj.forEach(this.proto!.fields, (key: string, value: api.Value) => {
obj.forEach(this.proto!.fields || {}, (key: string, value: api.Value) => {
result = result.set(new FieldPath([key]), this.converter!(value));
});
this.objectValue = result;
Expand Down Expand Up @@ -170,11 +170,11 @@ export class Document extends MaybeDocument {
'Can only call getProtoField() when proto is defined'
);

let protoValue: api.Value | undefined = this.proto!.fields[
path.firstSegment()
];
let protoValue: api.Value | undefined = this.proto!.fields
? this.proto!.fields[path.firstSegment()]
: undefined;
for (let i = 1; i < path.length; ++i) {
if (!protoValue || !protoValue.mapValue) {
if (!protoValue || !protoValue.mapValue || !protoValue.mapValue.fields) {
return undefined;
}
protoValue = protoValue.mapValue.fields[path.get(i)];
Expand Down
4 changes: 3 additions & 1 deletion packages/firestore/src/protos/firestore_proto_api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
@typescript-eslint/interface-name-prefix, @typescript-eslint/class-name-casing
*/
export declare type ApiClientHookFactory = any;
export declare type ApiClientObjectMap<T> = any;
export declare type PromiseRequestService = any;
export interface ApiClientObjectMap<T> {
[k: string]: T;
}

export declare type CompositeFilterOp = 'OPERATOR_UNSPECIFIED' | 'AND';
export interface ICompositeFilterOpEnum {
Expand Down
16 changes: 16 additions & 0 deletions packages/firestore/test/integration/api/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,4 +819,20 @@ apiDescribe('Queries', (persistence: boolean) => {
}
});
});

it('can use filter with nested field', () => {
// Reproduces https://github.com/firebase/firebase-js-sdk/issues/2204
const testDocs = {
a: {},
b: { map: {} },
c: { map: { nested: {} } },
d: { map: { nested: 'foo' } }
};

return withTestCollection(persistence, testDocs, async coll => {
await coll.get(); // Populate the cache
const snapshot = await coll.where('map.nested', '==', 'foo').get();
expect(toDataArray(snapshot)).to.deep.equal([{ map: { nested: 'foo' } }]);
});
});
});