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 1 commit
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
9 changes: 5 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,11 @@ 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!
);
// `geoPointValue` stores zeroes as undefined, which will fail
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sebastian, can you think of any other types that might be affected? Timestamp, I think, isn't.

Copy link
Contributor

Choose a reason for hiding this comment

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

Timestamp goes through normalizeNumber() which turns undefined into 0. Do you want to follow this pattern here as well and invoke normalizeNumber()? It also correctly handles NaN and Infinity, which should never be part of a GeoPoint, but who knows where 2020 is going to take us.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done (and extracted into a small helper function for consistency with Timestamp et al.).

// `GeoPoint` constructor's argument validation if used directly.
const latitude = value.geoPointValue!.latitude! || 0;
const longitude = value.geoPointValue!.longitude! || 0;
return new GeoPoint(latitude, longitude);
case TypeOrder.ArrayValue:
return this.convertArray(value.arrayValue!);
case TypeOrder.ObjectValue:
Expand Down
13 changes: 10 additions & 3 deletions packages/firestore/test/integration/api/type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,22 @@ 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) })
.then(() => {
.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