Skip to content

Commit 4992468

Browse files
authored
fix(shell-api): add toJSON for CommandResult MONGOSH-905 (#1021)
And also rename the `tojson` method of the `Bulk` class, which had only been cased that way for compatibility with the legacy shell’s `tojson()`/`tojsononeline()` methods, which are not present by default in mongosh.
1 parent 9106867 commit 4992468

File tree

7 files changed

+41
-28
lines changed

7 files changed

+41
-28
lines changed

packages/cli-repl/test/e2e.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ describe('e2e', function() {
409409
expect(await shell.executeLine('explainOutput')).to.match(/g:\s*\{\s*h:\s*\{\s*i:\s*\{\s*j:/);
410410
});
411411

412+
it('allows toJSON on results of db operations', async function() {
413+
if (process.env.MONGOSH_TEST_FORCE_API_STRICT) {
414+
return this.skip(); // listCommands is unversioned
415+
}
416+
expect(await shell.executeLine('typeof JSON.parse(JSON.stringify(db.listCommands())).ping.help')).to.include('string');
417+
expect(await shell.executeLine('typeof JSON.parse(JSON.stringify(db.test.insertOne({}))).insertedId')).to.include('string');
418+
});
419+
412420
describe('document validation errors', () => {
413421
context('post-4.4', () => {
414422
skipIfServerVersion(testServer, '<= 4.4');

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ describe('Bulk API', () => {
115115
expect(() => bulk.insert({})).to.throw(expectedError);
116116
});
117117
});
118-
describe('tojson', () => {
118+
describe('toJSON', () => {
119119
it('returns the batches length + currentInsert/Update/RemoveBatch?', () => {
120-
expect(bulk.tojson()).to.deep.equal({
120+
expect(bulk.toJSON()).to.deep.equal({
121121
nInsertOps: 0, nUpdateOps: 0, nRemoveOps: 0, nBatches: 4
122122
});
123123
});

packages/shell-api/src/bulk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export default class Bulk extends ShellApiWithMongoClass {
170170
* Internal method to determine what is printed for this class.
171171
*/
172172
[asPrintable](): any {
173-
return this.tojson();
173+
return this.toJSON();
174174
}
175175

176176
/**
@@ -224,7 +224,7 @@ export default class Bulk extends ShellApiWithMongoClass {
224224
return this;
225225
}
226226

227-
tojson(): Record<'nInsertOps' | 'nUpdateOps' | 'nRemoveOps' | 'nBatches', number> {
227+
toJSON(): Record<'nInsertOps' | 'nUpdateOps' | 'nRemoveOps' | 'nBatches', number> {
228228
const batches = this._serviceProviderBulkOp.batches.length;
229229

230230
return {
@@ -234,7 +234,7 @@ export default class Bulk extends ShellApiWithMongoClass {
234234
}
235235

236236
toString(): string {
237-
return JSON.stringify(this.tojson());
237+
return JSON.stringify(this.toJSON());
238238
}
239239

240240
getOperations(): Pick<Batch, 'originalZeroIndex' | 'batchType' | 'operations'>[] {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,6 +2352,7 @@ describe('Database', () => {
23522352
serviceProvider.runCommandWithCheck.resolves(expectedResult);
23532353
const result = await database.listCommands();
23542354
expect(result.value).to.deep.equal(expectedResult.commands);
2355+
expect(result.toJSON()).to.deep.equal(expectedResult.commands);
23552356
expect(result.type).to.equal('ListCommandsResult');
23562357
});
23572358

packages/shell-api/src/decorators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ type ClassHelp = {
278278
attr: { name: string; description: string }[];
279279
};
280280

281-
export const toIgnore = ['constructor', 'help'];
281+
export const toIgnore = ['constructor', 'help', 'toJSON'];
282282
function shellApiClassGeneric(constructor: Function, hasHelp: boolean): void {
283283
const className = constructor.name;
284284
const classHelpKeyPrefix = `shell-api.classes.${className}.help`;

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,8 +1511,8 @@ describe('Shell API (integration)', function() {
15111511
expect(await collection.countDocuments()).to.equal(0);
15121512
await bulk.execute();
15131513
});
1514-
it('tojson returns correctly', async() => {
1515-
expect(bulk.tojson()).to.deep.equal({ nInsertOps: size, nUpdateOps: 0, nRemoveOps: 0, nBatches: 1 });
1514+
it('toJSON returns correctly', async() => {
1515+
expect(bulk.toJSON()).to.deep.equal({ nInsertOps: size, nUpdateOps: 0, nRemoveOps: 0, nBatches: 1 });
15161516
});
15171517
it('executes', async() => {
15181518
expect(await collection.countDocuments()).to.equal(size);
@@ -1537,8 +1537,8 @@ describe('Shell API (integration)', function() {
15371537
bulk.find({ x: { $mod: [ 2, 0 ] } }).remove();
15381538
await bulk.execute();
15391539
});
1540-
it('tojson returns correctly', async() => {
1541-
expect(bulk.tojson()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 0, nRemoveOps: 1, nBatches: 1 });
1540+
it('toJSON returns correctly', async() => {
1541+
expect(bulk.toJSON()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 0, nRemoveOps: 1, nBatches: 1 });
15421542
});
15431543
it('executes', async() => {
15441544
expect(await collection.countDocuments()).to.equal(size / 2);
@@ -1562,8 +1562,8 @@ describe('Shell API (integration)', function() {
15621562
bulk.find({ x: { $mod: [ 2, 0 ] } }).removeOne();
15631563
await bulk.execute();
15641564
});
1565-
it('tojson returns correctly', async() => {
1566-
expect(bulk.tojson()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 0, nRemoveOps: 1, nBatches: 1 });
1565+
it('toJSON returns correctly', async() => {
1566+
expect(bulk.toJSON()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 0, nRemoveOps: 1, nBatches: 1 });
15671567
});
15681568
it('executes', async() => {
15691569
expect(await collection.countDocuments()).to.equal(size - 1);
@@ -1587,8 +1587,8 @@ describe('Shell API (integration)', function() {
15871587
bulk.find({ x: 2 }).replaceOne({ x: 1 });
15881588
await bulk.execute();
15891589
});
1590-
it('tojson returns correctly', async() => {
1591-
expect(bulk.tojson()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
1590+
it('toJSON returns correctly', async() => {
1591+
expect(bulk.toJSON()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
15921592
});
15931593
it('executes', async() => {
15941594
expect(await collection.countDocuments({ x: 1 })).to.equal(2);
@@ -1614,8 +1614,8 @@ describe('Shell API (integration)', function() {
16141614
bulk.find({ x: 2 }).updateOne({ $inc: { x: -1 } });
16151615
await bulk.execute();
16161616
});
1617-
it('tojson returns correctly', async() => {
1618-
expect(bulk.tojson()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
1617+
it('toJSON returns correctly', async() => {
1618+
expect(bulk.toJSON()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
16191619
});
16201620
it('executes', async() => {
16211621
expect(await collection.countDocuments({ x: 1 })).to.equal(2);
@@ -1641,8 +1641,8 @@ describe('Shell API (integration)', function() {
16411641
bulk.find({ x: { $mod: [ 2, 0 ] } }).update({ $inc: { x: 1 } });
16421642
await bulk.execute();
16431643
});
1644-
it('tojson returns correctly', async() => {
1645-
expect(bulk.tojson()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
1644+
it('toJSON returns correctly', async() => {
1645+
expect(bulk.toJSON()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
16461646
});
16471647
it('executes', async() => {
16481648
expect(await collection.countDocuments()).to.equal(size);
@@ -1671,8 +1671,8 @@ describe('Shell API (integration)', function() {
16711671
afterEach(async() => {
16721672
await collection.drop();
16731673
});
1674-
it('tojson returns correctly', async() => {
1675-
expect(bulk.tojson()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
1674+
it('toJSON returns correctly', async() => {
1675+
expect(bulk.toJSON()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
16761676
});
16771677
it('executes', async() => {
16781678
expect(await collection.countDocuments()).to.equal(size + 1);
@@ -1698,8 +1698,8 @@ describe('Shell API (integration)', function() {
16981698
bulk.find({ y: 0 }).upsert().updateOne({ $set: { y: 1 } });
16991699
await bulk.execute();
17001700
});
1701-
it('tojson returns correctly', async() => {
1702-
expect(bulk.tojson()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
1701+
it('toJSON returns correctly', async() => {
1702+
expect(bulk.toJSON()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
17031703
});
17041704
it('executes', async() => {
17051705
expect(await collection.countDocuments()).to.equal(size + 1);
@@ -1736,17 +1736,17 @@ describe('Shell API (integration)', function() {
17361736
for (let i = 0; i < size; i++) {
17371737
bulk.insert({ x: 1 });
17381738
}
1739-
expect(bulk.tojson().nBatches).to.equal(1);
1739+
expect(bulk.toJSON().nBatches).to.equal(1);
17401740
bulk.find({ x: 1 }).remove();
1741-
expect(bulk.tojson().nBatches).to.equal(2);
1741+
expect(bulk.toJSON().nBatches).to.equal(2);
17421742
bulk.find({ x: 2 }).update({ $inc: { x: 1 } });
1743-
expect(bulk.tojson().nBatches).to.equal(3);
1743+
expect(bulk.toJSON().nBatches).to.equal(3);
17441744
for (let i = 0; i < size; i++) {
17451745
bulk.insert({ x: 1 });
17461746
}
17471747
});
17481748
it('updates count depending on ordered or not', () => {
1749-
expect(bulk.tojson().nBatches).to.equal(m === 'initializeUnorderedBulkOp' ? 3 : 4);
1749+
expect(bulk.toJSON().nBatches).to.equal(m === 'initializeUnorderedBulkOp' ? 3 : 4);
17501750
});
17511751
});
17521752
describe('collation', () => {
@@ -1777,8 +1777,8 @@ describe('Shell API (integration)', function() {
17771777
// afterEach(async() => {
17781778
// await collection.drop();
17791779
// });
1780-
// it('tojson returns correctly', async() => {
1781-
// expect(bulk.tojson()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
1780+
// it('toJSON returns correctly', async() => {
1781+
// expect(bulk.toJSON()).to.deep.equal({ nInsertOps: 0, nUpdateOps: 1, nRemoveOps: 0, nBatches: 1 });
17821782
// });
17831783
// it('executes', async() => {
17841784
// expect(await collection.countDocuments()).to.equal(10);

packages/shell-api/src/result.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export class CommandResult extends ShellApiValueClass {
1919
[asPrintable](): unknown {
2020
return this.value;
2121
}
22+
23+
toJSON(): unknown {
24+
return this.value;
25+
}
2226
}
2327

2428
@shellApiClassDefault

0 commit comments

Comments
 (0)