fix(mongodb): preserve int64 filter values

This commit is contained in:
miracle authored and GitHub committed 2026-07-23 17:06:31 +08:00
1 parent f07509b5ad
commit c11b0b160d
6 files changed
+78 -2

No files matched your search

@@ -857,7 +857,36 @@ public final class MongoAgent {
} }
static JsonObject bsonToExtendedJson(Document doc) { static JsonObject bsonToExtendedJson(Document doc) {
return JsonParser.parseString(doc.toJson(EXTENDED_JSON_SETTINGS)).getAsJsonObject(); JsonObject relaxed = JsonParser.parseString(doc.toJson(EXTENDED_JSON_SETTINGS)).getAsJsonObject();
return preserveUnsafeLongsForJsonClients(doc, relaxed).getAsJsonObject();
}
private static JsonElement preserveUnsafeLongsForJsonClients(Object bsonValue, JsonElement relaxedValue) {
if (
bsonValue instanceof Long longValue &&
(longValue < -JS_MAX_SAFE_INTEGER || longValue > JS_MAX_SAFE_INTEGER)
) {
JsonObject wrapper = new JsonObject();
wrapper.addProperty("$numberLong", longValue.toString());
return wrapper;
}
if (bsonValue instanceof Document document && relaxedValue.isJsonObject()) {
JsonObject object = relaxedValue.getAsJsonObject();
for (Map.Entry<String, Object> entry : document.entrySet()) {
if (object.has(entry.getKey())) {
object.add(
entry.getKey(),
preserveUnsafeLongsForJsonClients(entry.getValue(), object.get(entry.getKey()))
);
}
}
} else if (bsonValue instanceof List<?> values && relaxedValue.isJsonArray()) {
JsonArray array = relaxedValue.getAsJsonArray();
for (int index = 0; index < Math.min(values.size(), array.size()); index++) {
array.set(index, preserveUnsafeLongsForJsonClients(values.get(index), array.get(index)));
}
}
return relaxedValue;
} }
static Object convertValue(Object value) { static Object convertValue(Object value) {
@@ -552,6 +552,27 @@ class MongoAgentTest {
); );
} }
@Test
void bsonToExtendedJsonWrapsUnsafeLongsForJsonClients() {
Document doc = new Document("_id", 144_115_205_316_939_462L)
.append("nested", new Document("sequence", -144_115_205_316_939_462L))
.append("items", List.of(144_115_205_316_939_462L))
.append("safe", 42L);
JsonObject json = MongoAgent.bsonToExtendedJson(doc);
assertEquals("144115205316939462", json.getAsJsonObject("_id").get("$numberLong").getAsString());
assertEquals(
"-144115205316939462",
json.getAsJsonObject("nested").getAsJsonObject("sequence").get("$numberLong").getAsString()
);
assertEquals(
"144115205316939462",
json.getAsJsonArray("items").get(0).getAsJsonObject().get("$numberLong").getAsString()
);
assertEquals(42L, json.get("safe").getAsLong());
}
@Test @Test
void documentForWriteParsesMongoShellIsoDateStrings() { void documentForWriteParsesMongoShellIsoDateStrings() {
Document doc = MongoAgent.documentForWrite("{\"$set\":{\"CreateDate\":\"ISODate(\\\"2026-06-10T13:59:31.287Z\\\")\"}}"); Document doc = MongoAgent.documentForWrite("{\"$set\":{\"CreateDate\":\"ISODate(\\\"2026-06-10T13:59:31.287Z\\\")\"}}");
@@ -1787,6 +1787,7 @@ defineExpose({ focusSearch });
rows="1" rows="1"
class="document-query-input flex-1 min-w-0 text-xs bg-transparent outline-none placeholder:text-muted-foreground/60 font-mono" class="document-query-input flex-1 min-w-0 text-xs bg-transparent outline-none placeholder:text-muted-foreground/60 font-mono"
placeholder="{}" placeholder="{}"
@keydown.enter.exact.prevent="applyFilter"
@keydown.ctrl.enter.prevent="applyFilter" @keydown.ctrl.enter.prevent="applyFilter"
@keydown.meta.enter.prevent="applyFilter" @keydown.meta.enter.prevent="applyFilter"
/> />
@@ -1825,6 +1826,7 @@ defineExpose({ focusSearch });
rows="1" rows="1"
class="document-query-input flex-1 min-w-0 text-xs bg-transparent outline-none placeholder:text-muted-foreground/60 font-mono" class="document-query-input flex-1 min-w-0 text-xs bg-transparent outline-none placeholder:text-muted-foreground/60 font-mono"
placeholder="{}" placeholder="{}"
@keydown.enter.exact.prevent="applyFilter"
@keydown.ctrl.enter.prevent="applyFilter" @keydown.ctrl.enter.prevent="applyFilter"
@keydown.meta.enter.prevent="applyFilter" @keydown.meta.enter.prevent="applyFilter"
/> />
@@ -1,4 +1,5 @@
import type { ComposerTranslation } from "vue-i18n"; import type { ComposerTranslation } from "vue-i18n";
import { normalizeJsonArgument } from "@dbx-app/mongo-shell";
import type { DatabaseType } from "@/types/database"; import type { DatabaseType } from "@/types/database";
import { quoteUnquotedObjectKeys } from "@/lib/mongo/mongoShellCommand"; import { quoteUnquotedObjectKeys } from "@/lib/mongo/mongoShellCommand";
import { formatMongoShellLiteral } from "@/lib/mongo/mongoDocumentValues"; import { formatMongoShellLiteral } from "@/lib/mongo/mongoDocumentValues";
@@ -531,11 +532,23 @@ function isDigit(value: string | undefined): boolean {
export function parseDocumentFilterInput(input: string, options: DocumentFilterParseOptions = {}): Record<string, unknown> { export function parseDocumentFilterInput(input: string, options: DocumentFilterParseOptions = {}): Record<string, unknown> {
const trimmed = input.trim(); const trimmed = input.trim();
if (!trimmed) return {}; if (!trimmed) return {};
const safe = quoteUnquotedObjectKeys(trimmed); const safe = normalizeDocumentQueryObjectInput(trimmed, options.kind);
const parsed = parseJsonPreservingLargeIntegers(safe, options); const parsed = parseJsonPreservingLargeIntegers(safe, options);
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? (parsed as Record<string, unknown>) : {}; return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? (parsed as Record<string, unknown>) : {};
} }
type DocumentQueryInputNormalizer = (input: string) => string | null;
const documentQueryInputNormalizers: Record<DocumentStoreKind, DocumentQueryInputNormalizer> = {
mongodb: normalizeJsonArgument,
elasticsearch: quoteUnquotedObjectKeys,
};
function normalizeDocumentQueryObjectInput(input: string, kind?: DocumentStoreKind): string {
const normalize = kind ? documentQueryInputNormalizers[kind] : quoteUnquotedObjectKeys;
return normalize(input) ?? input;
}
export function currentDocumentFilterJson(input: string, structured: Record<string, unknown> | null, kind?: DocumentStoreKind): string | undefined { export function currentDocumentFilterJson(input: string, structured: Record<string, unknown> | null, kind?: DocumentStoreKind): string | undefined {
const manual = parseDocumentFilterInput(input, { kind }); const manual = parseDocumentFilterInput(input, { kind });
const filter = structured ? (Object.keys(manual).length ? { $and: [manual, structured] } : structured) : manual; const filter = structured ? (Object.keys(manual).length ? { $and: [manual, structured] } : structured) : manual;
+6
View File
@@ -61,3 +61,9 @@ test("document save uses shared identity plan and write helpers", () => {
assert.doesNotMatch(source, /async function rekeyDocumentStoreDocument/); assert.doesNotMatch(source, /async function rekeyDocumentStoreDocument/);
assert.doesNotMatch(source, /async function replaceDocumentStoreDocument/); assert.doesNotMatch(source, /async function replaceDocumentStoreDocument/);
}); });
test("document query inputs apply on Enter and reserve Shift+Enter for newlines", () => {
const source = documentBrowserSource();
assert.equal(source.match(/@keydown\.enter\.exact\.prevent="applyFilter"/g)?.length, 2);
assert.doesNotMatch(source, /@keydown\.shift\.enter\.prevent="applyFilter"/);
});
+5
View File
@@ -211,6 +211,11 @@ test("uses elemMatch only for AND conditions on the same array object", () => {
test("formats document query object input", () => { test("formats document query object input", () => {
assert.equal(formatDocumentQueryInput('{profile:{city:"上海"},status:"active"}', "mongodb"), ["{", ' "profile": {', ' "city": "上海"', " },", ' "status": "active"', "}"].join("\n")); assert.equal(formatDocumentQueryInput('{profile:{city:"上海"},status:"active"}', "mongodb"), ["{", ' "profile": {', ' "city": "上海"', " },", ' "status": "active"', "}"].join("\n"));
}); });
test("normalizes Mongo shell syntax in manual document filters", () => {
assert.equal(currentDocumentFilterJson("{id:NumberLong('144115205316939462')}", null, "mongodb"), JSON.stringify({ id: { $numberLong: "144115205316939462" } }));
assert.equal(currentDocumentFilterJson("{owner:ObjectId('507f1f77bcf86cd799439011'),status:'active'}", null, "mongodb"), JSON.stringify({ owner: { $oid: "507f1f77bcf86cd799439011" }, status: "active" }));
});
test("preserves MongoDB int64 document filter values", () => { test("preserves MongoDB int64 document filter values", () => {
const id = "2048938405781032962"; const id = "2048938405781032962";
const firstUnsafeInteger = "9007199254740993"; const firstUnsafeInteger = "9007199254740993";