From a26836a5b25845c65d4471ea8b77ec4b3afffecf Mon Sep 17 00:00:00 2001 From: Prashanth <30420973+PrashanthCorp@users.noreply.github.com> Date: Fri, 4 May 2018 19:44:13 -0700 Subject: [PATCH] MongoCollectionTreeItem: fix findOne's result to recognize ObjectIDs (#610) * MongoCollectionTreeItem: fix findOne's result to recognize ObjectIDs * Add a comment * parseJSContent: use EJSON.parse instead of sandbox * Remove unused import --- src/mongo/editors/MongoFindOneResultEditor.ts | 2 +- src/mongo/tree/MongoCollectionTreeItem.ts | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/mongo/editors/MongoFindOneResultEditor.ts b/src/mongo/editors/MongoFindOneResultEditor.ts index e249d18..8bde461 100644 --- a/src/mongo/editors/MongoFindOneResultEditor.ts +++ b/src/mongo/editors/MongoFindOneResultEditor.ts @@ -19,7 +19,7 @@ export class MongoFindOneResultEditor implements ICosmosEditor { constructor(databaseNode: IAzureParentNode, collectionName: string, data: string, tree: AzureTreeDataProvider) { this._databaseNode = databaseNode; this._collectionName = collectionName; - this._originalDocument = JSON.parse(data); + this._originalDocument = EJSON.parse(data); this._tree = tree; } diff --git a/src/mongo/tree/MongoCollectionTreeItem.ts b/src/mongo/tree/MongoCollectionTreeItem.ts index 27f81c8..89b7515 100644 --- a/src/mongo/tree/MongoCollectionTreeItem.ts +++ b/src/mongo/tree/MongoCollectionTreeItem.ts @@ -4,7 +4,6 @@ *--------------------------------------------------------------------------------------------*/ import * as vscode from 'vscode'; -import * as vm from 'vm'; import * as path from 'path'; import * as _ from 'underscore'; import * as util from '../../utils/vscodeUtils'; @@ -12,6 +11,8 @@ import { Collection, Cursor, ObjectID, InsertOneWriteOpResult, BulkWriteOpResult import { IAzureParentTreeItem, IAzureTreeItem, IAzureNode, UserCancelledError, DialogResponses } from 'vscode-azureextensionui'; import { DefaultBatchSize } from '../../constants'; import { IMongoDocument, MongoDocumentTreeItem } from './MongoDocumentTreeItem'; +// tslint:disable:no-var-requires +const EJSON = require("mongodb-extended-json"); export class MongoCollectionTreeItem implements IAzureParentTreeItem { public static contextValue: string = "MongoCollection"; @@ -181,7 +182,9 @@ export class MongoCollectionTreeItem implements IAzureParentTreeItem { } else { return Promise.reject(new Error("Too many arguments passed to findOne.")); } - return this.stringify(result); + // findOne is the only command in this file whose output requires EJSON support. + // Hence that's the only function which uses EJSON.stringify rather than this.stringify. + return EJSON.stringify(result, null, '\t'); } private insert(document: Object): Thenable { @@ -269,12 +272,7 @@ function reportProgress(promise: Thenable, title: string): Thenable { // tslint:disable-next-line:no-any function parseJSContent(content: string): any { try { - const sandbox = {}; - // tslint:disable-next-line:insecure-random - const key = 'parse' + Math.floor(Math.random() * 1000000); - sandbox[key] = {}; - vm.runInNewContext(key + '=' + content, sandbox); - return sandbox[key]; + return EJSON.parse(content); } catch (error) { throw error.message; }