Skip to content

Commit 07827e3

Browse files
fix: deserialize document id before fetching document (#237)
* fix: deserialize document id before fetching document * test: add additional assertion to make sure that ejson id is not bson id * refactor: remove fire that left after refactoring
1 parent ed83b67 commit 07827e3

File tree

3 files changed

+91
-13
lines changed

3 files changed

+91
-13
lines changed

src/editors/editDocumentCodeLensProvider.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import EXTENSION_COMMANDS from '../commands';
33
import type { OutputItem, ResultCodeLensInfo } from '../utils/types';
44
import ConnectionController from '../connectionController';
55
import { DocumentSource } from '../telemetry/telemetryService';
6+
import { EJSON } from 'bson';
67

78
export default class EditDocumentCodeLensProvider
89
implements vscode.CodeLensProvider {
@@ -25,6 +26,7 @@ implements vscode.CodeLensProvider {
2526

2627
updateCodeLensesForPlayground(playgroundResult: OutputItem) {
2728
const source = DocumentSource.DOCUMENT_SOURCE_PLAYGROUND;
29+
let codeLensesInfo: ResultCodeLensInfo[] = [];
2830

2931
if (!playgroundResult || !playgroundResult.content) {
3032
this._codeLensesInfo = [];
@@ -38,17 +40,19 @@ implements vscode.CodeLensProvider {
3840
// Show code lenses only for the list of documents or a single document
3941
// that are returned by the find() method.
4042
if (type === 'Cursor') {
41-
this._updateCodeLensesForCursor(data);
43+
codeLensesInfo = this._updateCodeLensesForCursor(data);
4244
} else if (type === 'Document') {
43-
this._updateCodeLensesForDocument(data);
45+
codeLensesInfo = this._updateCodeLensesForDocument(data);
4446
}
47+
48+
this._codeLensesInfo = codeLensesInfo;
4549
}
4650

4751
_updateCodeLensesForCursor(data: {
4852
content: any,
4953
namespace: string | null,
5054
source: string
51-
}) {
55+
}): ResultCodeLensInfo[] {
5256
const codeLensesInfo: ResultCodeLensInfo[] = [];
5357

5458
if (Array.isArray(data.content)) {
@@ -64,7 +68,7 @@ implements vscode.CodeLensProvider {
6468
// to be able to save the editable document.
6569
if (item !== null && item._id && namespace) {
6670
codeLensesInfo.push({
67-
documentId: item._id,
71+
documentId: EJSON.deserialize(EJSON.serialize(item._id)),
6872
source,
6973
line,
7074
namespace,
@@ -79,34 +83,32 @@ implements vscode.CodeLensProvider {
7983
});
8084
}
8185

82-
this._codeLensesInfo = codeLensesInfo;
83-
this._onDidChangeCodeLenses.fire();
86+
return codeLensesInfo;
8487
}
8588

8689
_updateCodeLensesForDocument(data: {
8790
content: any,
8891
namespace: string | null,
8992
source: string
90-
}): void {
91-
const codeLensesInfo: ResultCodeLensInfo[] = [];
93+
}): ResultCodeLensInfo[] {
9294
const { content, namespace, source } = data;
95+
const codeLensesInfo: ResultCodeLensInfo[] = [];
9396

9497
if (content._id && namespace) {
9598
const connectionId = this._connectionController.getActiveConnectionId();
9699

97100
// When the playground result is the single document,
98101
// show the single code lense after {.
99102
codeLensesInfo.push({
100-
documentId: content._id,
103+
documentId: EJSON.deserialize(EJSON.serialize(content._id)),
101104
source,
102105
line: 1,
103106
namespace,
104107
connectionId
105108
});
106109
}
107110

108-
this._codeLensesInfo = codeLensesInfo;
109-
this._onDidChangeCodeLenses.fire();
111+
return codeLensesInfo;
110112
}
111113

112114
provideCodeLenses(): vscode.CodeLens[] {

src/test/suite/editors/editDocumentCodeLensProvider.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import { TestExtensionContext } from '../stubs';
55
import { StorageController } from '../../../storage';
66
import TelemetryService from '../../../telemetry/telemetryService';
77
import { StatusView } from '../../../views';
8+
import { ObjectId } from 'bson';
9+
import { afterEach } from 'mocha';
10+
11+
import sinon from 'sinon';
12+
13+
import * as util from 'util';
814

915
suite('Edit Document Code Lens Provider Test Suite', () => {
1016
const mockExtensionContext = new TestExtensionContext();
@@ -20,6 +26,10 @@ suite('Edit Document Code Lens Provider Test Suite', () => {
2026
testTelemetryService
2127
);
2228

29+
afterEach(() => {
30+
sinon.restore();
31+
});
32+
2333
test('provideCodeLenses returns an empty array if codeLensesInfo is empty', () => {
2434
const testCodeLensProvider = new EditDocumentCodeLensProvider(
2535
testConnectionController
@@ -30,6 +40,72 @@ suite('Edit Document Code Lens Provider Test Suite', () => {
3040
assert(codeLens.length === 0);
3141
});
3242

43+
test('the _updateCodeLensesForCursor function deserialize document id', () => {
44+
const testCodeLensProvider = new EditDocumentCodeLensProvider(
45+
testConnectionController
46+
);
47+
const ejsinId = { $oid: '5d973ae744376d2aae72a160' };
48+
const playgroundResult = {
49+
content: [{
50+
_id: ejsinId,
51+
name: 'test name'
52+
}],
53+
namespace: 'db.coll',
54+
source: 'playground'
55+
};
56+
57+
const mockActiveConnectionId = sinon.fake.returns('tasty_sandwhich');
58+
sinon.replace(
59+
testCodeLensProvider._connectionController,
60+
'getActiveConnectionId',
61+
mockActiveConnectionId
62+
);
63+
64+
const result = testCodeLensProvider._updateCodeLensesForCursor(playgroundResult);
65+
66+
assert(!!result);
67+
const codeLensesInfo = result[0];
68+
assert(!!codeLensesInfo);
69+
assert(!!codeLensesInfo.documentId);
70+
const bsonId = new ObjectId('5d973ae744376d2aae72a160');
71+
72+
assert(util.inspect(codeLensesInfo.documentId) !== util.inspect(ejsinId));
73+
assert(util.inspect(codeLensesInfo.documentId) === util.inspect(bsonId));
74+
});
75+
76+
test('the _updateCodeLensesForDocument function deserialize document id', () => {
77+
const testCodeLensProvider = new EditDocumentCodeLensProvider(
78+
testConnectionController
79+
);
80+
const ejsinId = { $oid: '5d973ae744376d2aae72a160' };
81+
const playgroundResult = {
82+
content: {
83+
_id: ejsinId,
84+
name: 'test name'
85+
},
86+
namespace: 'db.coll',
87+
source: 'playground'
88+
};
89+
90+
const mockActiveConnectionId = sinon.fake.returns('tasty_sandwhich');
91+
sinon.replace(
92+
testCodeLensProvider._connectionController,
93+
'getActiveConnectionId',
94+
mockActiveConnectionId
95+
);
96+
97+
const result = testCodeLensProvider._updateCodeLensesForDocument(playgroundResult);
98+
99+
assert(!!result);
100+
const codeLensesInfo = result[0];
101+
assert(!!codeLensesInfo);
102+
assert(!!codeLensesInfo.documentId);
103+
const bsonId = new ObjectId('5d973ae744376d2aae72a160');
104+
105+
assert(util.inspect(codeLensesInfo.documentId) !== util.inspect(ejsinId));
106+
assert(util.inspect(codeLensesInfo.documentId) === util.inspect(bsonId));
107+
});
108+
33109
suite('after updateCodeLensesForPlayground', () => {
34110
test('provideCodeLenses returns one code lens when result is a single document', () => {
35111
const testCodeLensProvider = new EditDocumentCodeLensProvider(

src/test/suite/editors/mongoDBDocumentService.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import TelemetryService from '../../../telemetry/telemetryService';
99
import { afterEach } from 'mocha';
1010
import { EJSON } from 'bson';
1111

12-
const sinon = require('sinon');
13-
const chai = require('chai');
12+
import sinon from 'sinon';
13+
import chai from 'chai';
1414
const expect = chai.expect;
1515

1616
suite('MongoDB Document Service Test Suite', () => {

0 commit comments

Comments
 (0)