Skip to content

Commit b97132e

Browse files
fix(NODE-5102): listDatabases nameOnly setting is sent as NaN (#3742)
Co-authored-by: Warren James <[email protected]>
1 parent a329748 commit b97132e

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

src/operations/list_databases.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ export class ListDatabasesOperation extends CommandOperation<ListDatabasesResult
4040
callback: Callback<ListDatabasesResult>
4141
): void {
4242
const cmd: Document = { listDatabases: 1 };
43-
if (this.options.nameOnly) {
44-
cmd.nameOnly = Number(cmd.nameOnly);
43+
44+
if (typeof this.options.nameOnly === 'boolean') {
45+
cmd.nameOnly = this.options.nameOnly;
4546
}
4647

4748
if (this.options.filter) {

test/integration/enumerate_databases.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from 'chai';
2+
import { once } from 'events';
23

34
import { type AddUserOptions, type MongoClient, MongoServerError } from '../mongodb';
45
import { TestBuilder, UnifiedTestSuiteBuilder } from '../tools/utils';
@@ -141,6 +142,53 @@ describe('listDatabases()', function () {
141142
);
142143
});
143144

145+
describe('nameOnly option', function () {
146+
let client: MongoClient;
147+
const nameOnlyOptions = [true, false, undefined];
148+
const optionToExpectation = {
149+
true: 'with nameOnly = true',
150+
false: 'with nameOnly = false',
151+
undefined: 'without nameOnly field'
152+
};
153+
154+
beforeEach(async function () {
155+
client = await this.configuration.newClient({}, { monitorCommands: true }).connect();
156+
await client.db('test').createCollection('test');
157+
});
158+
159+
afterEach(async function () {
160+
if (client) {
161+
await client.db(`test`).dropDatabase();
162+
await client.close();
163+
}
164+
});
165+
166+
for (const nameOnly of nameOnlyOptions) {
167+
context(`when options.nameOnly is ${nameOnly ?? 'not defined'}`, function () {
168+
it(`sends command ${optionToExpectation[String(nameOnly)]}`, async function () {
169+
const promise = once(client, 'commandStarted');
170+
await client.db().admin().listDatabases({ nameOnly });
171+
172+
const commandStarted = (await promise)[0];
173+
expect(commandStarted.command).to.haveOwnProperty('listDatabases', 1);
174+
175+
switch (nameOnly) {
176+
case true:
177+
expect(commandStarted.command).to.have.property('nameOnly', true);
178+
break;
179+
case false:
180+
expect(commandStarted.command).to.have.property('nameOnly', false);
181+
break;
182+
case undefined:
183+
expect(commandStarted.command).to.not.have.property('nameOnly');
184+
break;
185+
default:
186+
expect.fail(`Unrecognized nameOnly value: ${nameOnly}`);
187+
}
188+
});
189+
});
190+
}
191+
});
144192
UnifiedTestSuiteBuilder.describe('comment option')
145193
.createEntities(UnifiedTestSuiteBuilder.defaultEntities)
146194
.initialData({
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { expect } from 'chai';
2+
3+
import { ListDatabasesOperation, MongoDBNamespace } from '../../mongodb';
4+
5+
const mockDB = {
6+
s: {
7+
namespace: {
8+
withCollection() {
9+
return new MongoDBNamespace('test', 'test');
10+
}
11+
}
12+
}
13+
};
14+
15+
describe('ListDatabasesOperation', function () {
16+
describe('#constructor', function () {
17+
context('when nameOnly is provided', function () {
18+
context('when nameOnly is true', function () {
19+
it('sets nameOnly to true', function () {
20+
const operation = new ListDatabasesOperation(mockDB, { nameOnly: true });
21+
expect(operation.options).to.have.property('nameOnly', true);
22+
});
23+
});
24+
25+
context('when nameOnly is false', function () {
26+
it('sets nameOnly to false', function () {
27+
const operation = new ListDatabasesOperation(mockDB, { nameOnly: false });
28+
expect(operation.options).to.have.property('nameOnly', false);
29+
});
30+
});
31+
});
32+
33+
context('when nameOnly is not specified', function () {
34+
it('nameOnly is undefined', function () {
35+
const operation = new ListDatabasesOperation(mockDB, {});
36+
expect(operation.options).not.to.have.property('nameOnly');
37+
});
38+
});
39+
});
40+
});

0 commit comments

Comments
 (0)