From d341581ff01b6eb41ab20d4ea6cac907cb1f3e07 Mon Sep 17 00:00:00 2001 From: Guoyu Su Date: Sat, 25 Jul 2026 08:47:06 +0800 Subject: [PATCH] fix(mongodb): accept trailing commas in shell arguments --- packages/app-tests/mongoShellCommand.test.ts | 63 ++++++++++++++++++++ packages/mongo-shell/src/json.ts | 36 ++++++++++- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/packages/app-tests/mongoShellCommand.test.ts b/packages/app-tests/mongoShellCommand.test.ts index 4444f9590..e8c4ce459 100644 --- a/packages/app-tests/mongoShellCommand.test.ts +++ b/packages/app-tests/mongoShellCommand.test.ts @@ -534,6 +534,69 @@ test("parseMongoAggregateCommand accepts an empty pipeline", () => { }); }); +test("parseMongoAggregateCommand accepts Mongo Shell trailing commas", () => { + const command = parseMongoAggregateCommand(`db.AdressInfo.aggregate([ + { + $match: { + IsDelete: 0, + DataSource: { $ne: 'XC' }, + TypeName: 1, + }, + }, + { + $project: { + MainId: '$_id', + labels: ['primary', 'backup',], + }, + }, + { $out: 'IBMBiititle' }, + ], { + allowDiskUse: true, + })`); + + assert.ok(command); + assert.deepEqual(JSON.parse(command.pipeline), [ + { + $match: { + IsDelete: 0, + DataSource: { $ne: "XC" }, + TypeName: 1, + }, + }, + { + $project: { + MainId: "$_id", + labels: ["primary", "backup"], + }, + }, + { $out: "IBMBiititle" }, + ]); + assert.deepEqual(JSON.parse(command.options ?? "null"), { allowDiskUse: true }); +}); + +test("parseMongoAggregateCommand preserves trailing-comma text inside strings", () => { + const command = parseMongoAggregateCommand(`db.logs.aggregate([ + { + $project: { + objectText: "literal,}", + arrayText: "literal,]", + escapedQuote: "literal\\",]", + }, + }, + ])`); + + assert.ok(command); + assert.deepEqual(JSON.parse(command.pipeline), [ + { + $project: { + objectText: "literal,}", + arrayText: "literal,]", + escapedQuote: 'literal",]', + }, + }, + ]); +}); + test("parseMongoAggregateCommand ignores comments inside aggregate pipelines", () => { const command = parseMongoAggregateCommand(` db.cash.aggregate([ diff --git a/packages/mongo-shell/src/json.ts b/packages/mongo-shell/src/json.ts index 87a634ade..19573ec57 100644 --- a/packages/mongo-shell/src/json.ts +++ b/packages/mongo-shell/src/json.ts @@ -241,6 +241,39 @@ function mongoLineCommentEnd(source: string, start: number): number { return source.length; } +function removeTrailingCommas(source: string): string { + let result = ""; + let inString = false; + let escaped = false; + + for (let index = 0; index < source.length; index += 1) { + const char = source[index] ?? ""; + if (inString) { + result += char; + if (escaped) escaped = false; + else if (char === "\\") escaped = true; + else if (char === '"') inString = false; + continue; + } + + if (char === '"') { + inString = true; + result += char; + continue; + } + + if (char === ",") { + let next = index + 1; + while (/\s/.test(source[next] ?? "")) next += 1; + if (source[next] === "}" || source[next] === "]") continue; + } + + result += char; + } + + return result; +} + export function quoteUnquotedObjectKeys(source: string): string { let result = ""; let quote: string | null = null; @@ -407,5 +440,6 @@ function convertSingleQuotedStrings(source: string): string { } } - return quote === "'" ? source : result + source.slice(copiedUntil); + const converted = quote === "'" ? source : result + source.slice(copiedUntil); + return removeTrailingCommas(converted); }