Skip to content

Commit 2d07d1d

Browse files
authored
fix(insert-document): handle invalid bson COMPASS-7316 (#4969)
1 parent ead049c commit 2d07d1d

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

packages/compass-crud/src/stores/crud-store.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,34 @@ describe('store', function () {
10421042
await listener;
10431043
});
10441044
});
1045+
1046+
context('when the document has invalid bson', function () {
1047+
// this is invalid ObjectId
1048+
const jsonDoc = '{"_id": {"$oid": ""}}';
1049+
1050+
beforeEach(function () {
1051+
store.state.insert.jsonView = true;
1052+
store.state.insert.doc = {};
1053+
store.state.insert.jsonDoc = jsonDoc;
1054+
});
1055+
1056+
it('does not insert the document and sets the error', async function () {
1057+
const listener = waitForState(store, (state) => {
1058+
expect(state.docs.length).to.equal(0);
1059+
expect(state.count).to.equal(0);
1060+
expect(state.insert.doc).to.deep.equal({});
1061+
expect(state.insert.jsonDoc).to.equal(jsonDoc);
1062+
expect(state.insert.isOpen).to.equal(true);
1063+
expect(state.insert.jsonView).to.equal(true);
1064+
expect(state.insert.message).to.not.equal('');
1065+
expect(state.insert.mode).to.equal('error');
1066+
});
1067+
1068+
store.insertDocument();
1069+
1070+
await listener;
1071+
});
1072+
});
10451073
});
10461074

10471075
context('when there is an error', function () {

packages/compass-crud/src/stores/crud-store.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,14 +1201,14 @@ class CrudStoreImpl
12011201

12021202
let doc: BSONObject;
12031203

1204-
if (this.state.insert.jsonView) {
1205-
doc = HadronDocument.FromEJSON(
1206-
this.state.insert.jsonDoc ?? ''
1207-
).generateObject();
1208-
} else {
1209-
doc = this.state.insert.doc!.generateObject();
1210-
}
12111204
try {
1205+
if (this.state.insert.jsonView) {
1206+
doc = HadronDocument.FromEJSON(
1207+
this.state.insert.jsonDoc ?? ''
1208+
).generateObject();
1209+
} else {
1210+
doc = this.state.insert.doc!.generateObject();
1211+
}
12121212
await this.dataService.insertOne(this.state.ns, doc);
12131213

12141214
const payload = {

0 commit comments

Comments
 (0)