Skip to content

Commit 99c2c35

Browse files
committed
add validation to getAggregateFromServerDirect
1 parent dc39f06 commit 99c2c35

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

packages/firestore/src/lite-api/aggregate.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { CompositeFilterOpEnum } from '../protos/firestore_proto_api';
1819
import { invokeRunAggregationQueryRpc } from '../remote/datastore';
1920
import { hardAssert } from '../util/assert';
2021
import { cast } from '../util/input_validation';
@@ -82,19 +83,24 @@ export function getAggregateFromServerDirect(
8283

8384
return invokeRunAggregationQueryRpc(datastore, aggregateQuery).then(
8485
result => {
85-
const aggregationFields = new Map<string, any>();
86+
hardAssert(
87+
result[0] !== undefined,
88+
'Aggregation fields are missing from result.'
89+
);
8690

87-
for (const [key, value] of Object.entries(result[0])) {
88-
aggregationFields.set(key, userDataWriter.convertValue(value));
89-
}
90-
const countField = aggregationFields.get('count_alias');
91+
const countField = (result[0] as any).count_alias;
92+
hardAssert(
93+
countField !== undefined,
94+
'Count field is missing from result.'
95+
);
9196

97+
const countAggregateResult = userDataWriter.convertValue(countField);
9298
hardAssert(
93-
countField !== undefined && typeof countField === 'number',
94-
'Count aggeragte field is invalid. countField:' + countField
99+
typeof countAggregateResult === 'number',
100+
'Count aggeragte field is not a number: ' + countAggregateResult
95101
);
96102
return Promise.resolve(
97-
new AggregateQuerySnapshot(aggregateQuery, countField!)
103+
new AggregateQuerySnapshot(aggregateQuery, countAggregateResult)
98104
);
99105
}
100106
);

packages/firestore/src/remote/datastore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ export async function invokeRunAggregationQueryRpc(
255255
return (
256256
response
257257
// Omit RunAggregationQueryResponse that only contain readTimes.
258-
.filter(proto => !!proto.result && !!proto.result.aggregateFields)
258+
.filter(proto => !!proto.result)
259259
.map(proto => proto.result!.aggregateFields!)
260260
);
261261
}

0 commit comments

Comments
 (0)