Skip to content

Commit 8df1a78

Browse files
authored
fix(shell-api): coerce values in getShardDistribution to JS number MONGOSH-1329 (#1363)
1 parent 01194da commit 8df1a78

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

packages/shell-api/src/collection.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import {
3030
markAsExplainOutput,
3131
assertArgsDefinedType,
3232
isValidCollectionName,
33-
shouldRunAggregationImmediately
33+
shouldRunAggregationImmediately,
34+
coerceToJSNumber
3435
} from './helpers';
3536
import {
3637
AnyBulkWriteOperation,
@@ -1721,17 +1722,16 @@ export default class Collection extends ShellApiWithMongoClass {
17211722
(shardStats.numChunks === 0) ? 0 : Math.floor(shardStats.count / shardStats.numChunks);
17221723

17231724
result[key] = {
1724-
data: dataFormat(shardStats.size),
1725+
data: dataFormat(coerceToJSNumber(shardStats.size)),
17251726
docs: shardStats.count,
17261727
chunks: shardStats.numChunks,
17271728
'estimated data per chunk': dataFormat(estChunkData),
17281729
'estimated docs per chunk': estChunkCount
17291730
};
17301731

1731-
1732-
totals.size += shardStats.size;
1733-
totals.count += shardStats.count;
1734-
totals.numChunks += shardStats.numChunks;
1732+
totals.size += coerceToJSNumber(shardStats.size);
1733+
totals.count += coerceToJSNumber(shardStats.count);
1734+
totals.numChunks += coerceToJSNumber(shardStats.numChunks);
17351735

17361736
conciseShardsStats.push(shardStats);
17371737
})()

packages/shell-api/src/helpers.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertArgsDefinedType, dataFormat, getPrintableShardStatus } from './helpers';
1+
import { assertArgsDefinedType, coerceToJSNumber, dataFormat, getPrintableShardStatus } from './helpers';
22
import { Database, Mongo, ShellInstanceState } from './index';
33
import constructShellBson from './shell-bson';
44
import { ServiceProvider, bson } from '@mongosh/service-provider-core';
@@ -219,3 +219,13 @@ describe('getPrintableShardStatus', () => {
219219
expect.fail('missed exception');
220220
});
221221
});
222+
223+
describe('coerceToJSNumber', () => {
224+
it('converts various BSON types to JS numbers', () => {
225+
expect(coerceToJSNumber(0)).to.equal(0);
226+
expect(coerceToJSNumber(new bson.Int32(0))).to.equal(0);
227+
expect(coerceToJSNumber(new bson.Long(0))).to.equal(0);
228+
expect(coerceToJSNumber(new bson.Long('9223372036854775807'))).to.equal(9223372036854776000);
229+
expect(coerceToJSNumber(new bson.Double(1e30))).to.equal(1e30);
230+
});
231+
});

packages/shell-api/src/helpers.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type Database from './database';
1717
import type Collection from './collection';
1818
import { CursorIterationResult } from './result';
1919
import { ShellApiErrors } from './error-codes';
20-
import { BinaryType, ReplPlatform } from '@mongosh/service-provider-core';
20+
import { BinaryType, ReplPlatform, bson } from '@mongosh/service-provider-core';
2121
import { ClientSideFieldLevelEncryptionOptions } from './field-level-encryption';
2222
import { AutoEncryptionOptions } from 'mongodb';
2323
import { shellApiType } from './enums';
@@ -499,6 +499,12 @@ export async function getConfigDB(db: Database): Promise<Database> {
499499
return db.getSiblingDB('config');
500500
}
501501

502+
type AnyBsonNumber = number | typeof bson.Long.prototype | typeof bson.Int32.prototype | typeof bson.Double.prototype;
503+
export function coerceToJSNumber(n: AnyBsonNumber): number {
504+
if (typeof n === 'number') {return n;}
505+
return 'toNumber' in n ? n.toNumber() : n.valueOf();
506+
}
507+
502508
export function dataFormat(bytes?: number): string {
503509
if (bytes === null || bytes === undefined) {
504510
return '0B';

0 commit comments

Comments
 (0)