Skip to content

Fix GeoPoint field values with a zero coordinate failing input validation #3018

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 3 commits into from
May 5, 2020
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
12 changes: 8 additions & 4 deletions packages/firestore/src/api/user_data_writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ export class UserDataWriter<T = firestore.DocumentData> {
case TypeOrder.RefValue:
return this.convertReference(value.referenceValue!);
case TypeOrder.GeoPointValue:
return new GeoPoint(
value.geoPointValue!.latitude!,
value.geoPointValue!.longitude!
);
return this.convertGeoPoint(value.geoPointValue!);
case TypeOrder.ArrayValue:
return this.convertArray(value.arrayValue!);
case TypeOrder.ObjectValue:
Expand All @@ -96,6 +93,13 @@ export class UserDataWriter<T = firestore.DocumentData> {
return result;
}

private convertGeoPoint(value: api.LatLng): GeoPoint {
return new GeoPoint(
normalizeNumber(value.latitude),
normalizeNumber(value.longitude)
);
}

private convertArray(arrayValue: api.ArrayValue): unknown[] {
return (arrayValue.values || []).map(value => this.convertValue(value));
}
Expand Down
12 changes: 10 additions & 2 deletions packages/firestore/test/integration/api/type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,23 @@ apiDescribe('Firestore', (persistence: boolean) => {
it('can read and write geo point fields', () => {
return withTestDoc(persistence, doc => {
return doc
.set({ geopoint: new GeoPoint(1.23, 4.56) })
.set({
geopoint1: new GeoPoint(1.23, 4.56),
geopoint2: new GeoPoint(0, 0)
})
.then(() => {
return doc.get();
})
.then(docSnapshot => {
const latLong = docSnapshot.data()!['geopoint'];
const latLong = docSnapshot.data()!['geopoint1'];
expect(latLong instanceof GeoPoint).to.equal(true);
expect(latLong.latitude).to.equal(1.23);
expect(latLong.longitude).to.equal(4.56);

const zeroLatLong = docSnapshot.data()!['geopoint2'];
expect(zeroLatLong instanceof GeoPoint).to.equal(true);
expect(zeroLatLong.latitude).to.equal(0);
expect(zeroLatLong.longitude).to.equal(0);
});
});
});
Expand Down