Skip to content

Commit 7984d1c

Browse files
committed
fix(shell-api): do not validate explain verbosity in mongosh MONGOSH-811
1 parent 65ce142 commit 7984d1c

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,8 +1477,8 @@ describe('Collection', () => {
14771477

14781478
it('throws in case of non valid verbosity', () => {
14791479
expect(() => {
1480-
collection.explain('badVerbosityArgument' as any);
1481-
}).to.throw('verbosity can only be one of queryPlanner, executionStats, allPlansExecution. Received badVerbosityArgument.');
1480+
collection.explain(0 as any);
1481+
}).to.throw('verbosity must be a string');
14821482
});
14831483

14841484
it('sets the right default verbosity', () => {

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Collection from './collection';
1010
import Explainable from './explainable';
1111
import { ServiceProvider, bson, Document } from '@mongosh/service-provider-core';
1212
import ShellInternalState from './shell-internal-state';
13-
import { CommonErrors, MongoshInvalidInputError } from '@mongosh/errors';
1413

1514
describe('Explainable', () => {
1615
describe('help', () => {
@@ -93,15 +92,11 @@ describe('Explainable', () => {
9392
expect(explainable._verbosity).to.equal('allPlansExecution');
9493
});
9594

96-
it('validates the verbosity', () => {
97-
try {
98-
explainable.setVerbosity('badVerbosityArgument' as any);
99-
expect.fail('expected error');
100-
} catch (e) {
101-
expect(e).to.be.instanceOf(MongoshInvalidInputError);
102-
expect(e.message).to.contain('verbosity can only be one of queryPlanner, executionStats, allPlansExecution. Received badVerbosityArgument.');
103-
expect(e.code).to.equal(CommonErrors.InvalidArgument);
104-
}
95+
96+
it('throws in case of non valid verbosity', () => {
97+
expect(() => {
98+
collection.explain(0 as any);
99+
}).to.throw('verbosity must be a string');
105100
});
106101
});
107102

packages/shell-api/src/helpers.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,8 @@ export function validateExplainableVerbosity(verbosity: ExplainVerbosityLike): E
6767
verbosity = 'queryPlanner';
6868
}
6969

70-
const allowedVerbosity = [
71-
'queryPlanner',
72-
'executionStats',
73-
'allPlansExecution'
74-
];
75-
76-
if (!allowedVerbosity.includes(verbosity as string)) {
77-
throw new MongoshInvalidInputError(
78-
`verbosity can only be one of ${allowedVerbosity.join(', ')}. Received ${verbosity}.`,
79-
CommonErrors.InvalidArgument
80-
);
70+
if (typeof verbosity !== 'string') {
71+
throw new MongoshInvalidInputError('verbosity must be a string', CommonErrors.InvalidArgument);
8172
}
8273

8374
return verbosity;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,17 @@ describe('Shell API (integration)', function() {
14521452
expect(explained.stages[0].$cursor).to.include.all.keys(['queryPlanner', 'executionStats']);
14531453
});
14541454
});
1455+
1456+
describe('invalid verbosity', () => {
1457+
it('rejects with a server error', async() => {
1458+
try {
1459+
await collection.find().explain('foo');
1460+
expect.fail('missed exception');
1461+
} catch (err) {
1462+
expect(err.name).to.equal('MongoServerError');
1463+
}
1464+
});
1465+
});
14551466
});
14561467

14571468
describe('Bulk API', async() => {

0 commit comments

Comments
 (0)