Skip to content

Commit fd12d77

Browse files
authored
Fix GeoPoint field values with a zero coordinate failing input validation (#3018)
Fix the regression introduced in #2784: a `GeoPoint` where at least one of the coordinates is zero doesn't marshal properly. This is because zeroes happen to be serialized as `undefined` (presumably because protos normally treat zero values and the absence of a value interchangeably) which then fails the strict input validation in `GeoPoint`. Fixes #3006.
1 parent 76ba2ea commit fd12d77

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

packages/firestore/src/api/user_data_writer.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ export class UserDataWriter<T = firestore.DocumentData> {
7575
case TypeOrder.RefValue:
7676
return this.convertReference(value.referenceValue!);
7777
case TypeOrder.GeoPointValue:
78-
return new GeoPoint(
79-
value.geoPointValue!.latitude!,
80-
value.geoPointValue!.longitude!
81-
);
78+
return this.convertGeoPoint(value.geoPointValue!);
8279
case TypeOrder.ArrayValue:
8380
return this.convertArray(value.arrayValue!);
8481
case TypeOrder.ObjectValue:
@@ -96,6 +93,13 @@ export class UserDataWriter<T = firestore.DocumentData> {
9693
return result;
9794
}
9895

96+
private convertGeoPoint(value: api.LatLng): GeoPoint {
97+
return new GeoPoint(
98+
normalizeNumber(value.latitude),
99+
normalizeNumber(value.longitude)
100+
);
101+
}
102+
99103
private convertArray(arrayValue: api.ArrayValue): unknown[] {
100104
return (arrayValue.values || []).map(value => this.convertValue(value));
101105
}

packages/firestore/test/integration/api/type.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,23 @@ apiDescribe('Firestore', (persistence: boolean) => {
6262
it('can read and write geo point fields', () => {
6363
return withTestDoc(persistence, doc => {
6464
return doc
65-
.set({ geopoint: new GeoPoint(1.23, 4.56) })
65+
.set({
66+
geopoint1: new GeoPoint(1.23, 4.56),
67+
geopoint2: new GeoPoint(0, 0)
68+
})
6669
.then(() => {
6770
return doc.get();
6871
})
6972
.then(docSnapshot => {
70-
const latLong = docSnapshot.data()!['geopoint'];
73+
const latLong = docSnapshot.data()!['geopoint1'];
7174
expect(latLong instanceof GeoPoint).to.equal(true);
7275
expect(latLong.latitude).to.equal(1.23);
7376
expect(latLong.longitude).to.equal(4.56);
77+
78+
const zeroLatLong = docSnapshot.data()!['geopoint2'];
79+
expect(zeroLatLong instanceof GeoPoint).to.equal(true);
80+
expect(zeroLatLong.latitude).to.equal(0);
81+
expect(zeroLatLong.longitude).to.equal(0);
7482
});
7583
});
7684
});

0 commit comments

Comments
 (0)