diff --git a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts index fbe1d06a0170c36d95d05c8b9a7d51dd9c8bc11d..3f95fec3d3baff235c252edef55611fe9ad60df0 100644 --- a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts +++ b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts @@ -1,5 +1,7 @@ import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database"; import { + type NativeQuestionDetails, + createNativeQuestion, createQuestion, getNotebookStep, modal, @@ -229,3 +231,65 @@ describe("issue 39487", () => { return popover().get("button[data-previous]"); } }); + +const MONGO_DB_ID = 2; + +describe("issue 47793", () => { + const questionDetails: NativeQuestionDetails = { + database: MONGO_DB_ID, + native: { + query: `[ + { $match: { quantity: {{quantity}} }}, + { + "$project": { + "_id": "$_id", + "id": "$id", + "user_id": "$user_id", + "product_id": "$product_id", + "subtotal": "$subtotal", + "tax": "$tax", + "total": "$total", + "created_at": "$created_at", + "quantity": "$quantity", + "discount": "$discount" + } + }, + { + "$limit": 1048575 + } +]`, + "template-tags": { + quantity: { + type: "number", + name: "quantity", + id: "754ae827-661c-4fc9-b511-c0fb7b6bae2b", + "display-name": "Quantity", + default: "10", + }, + }, + collection: "orders", + }, + }; + + beforeEach(() => { + restore("mongo-5"); + cy.signInAsAdmin(); + }); + + it( + "should be able to preview queries for mongodb (metabase#47793)", + { tags: ["@external", "@mongo"] }, + () => { + createNativeQuestion(questionDetails, { visitQuestion: true }); + cy.findByTestId("visibility-toggler") + .findByText(/open editor/i) + .click(); + cy.findByTestId("native-query-editor-container") + .findByLabelText("Preview the query") + .click(); + modal() + .should("contain.text", "$project") + .and("contain.text", "quantity: 10"); + }, + ); +}); diff --git a/frontend/src/metabase/lib/engine.ts b/frontend/src/metabase/lib/engine.ts index 9dccf38c740192206f1992cfafd5b87676696b9b..db145d3fb1c86db50b745a809ea47ed36ac39837 100644 --- a/frontend/src/metabase/lib/engine.ts +++ b/frontend/src/metabase/lib/engine.ts @@ -41,8 +41,8 @@ export function formatNativeQuery(query?: string | JSONQuery, engine?: string) { return formatSQL(query); } - if (typeof query === "object" && engineType === "json") { - return formatJsonQuery(query); + if (engineType === "json") { + return typeof query === "object" ? formatJsonQuery(query) : query; } return undefined; diff --git a/frontend/src/metabase/lib/engine.unit.spec.ts b/frontend/src/metabase/lib/engine.unit.spec.ts index 66ac03c107eea424b3169f93cacf357a0c3b4d1d..1ac7292ee6d896b507abb9961300732d2c680cfd 100644 --- a/frontend/src/metabase/lib/engine.unit.spec.ts +++ b/frontend/src/metabase/lib/engine.unit.spec.ts @@ -85,10 +85,6 @@ describe("formatNativeQuery", () => { }); it("should return `undefined` when the query and the engine don't match", () => { - expect(formatNativeQuery("select 1", "mongo")).toBeUndefined(); - expect(formatNativeQuery("foo bar baz", "mongo")).toBeUndefined(); - expect(formatNativeQuery("", "mongo")).toBeUndefined(); - expect(formatNativeQuery({}, "postgres")).toBeUndefined(); expect(formatNativeQuery([], "postgres")).toBeUndefined(); expect(formatNativeQuery([{}], "postgres")).toBeUndefined(); @@ -117,6 +113,7 @@ describe("formatNativeQuery", () => { expect(formatNativeQuery([], "mongo")).toEqual("[]"); expect(formatNativeQuery(["foo"], "mongo")).toEqual('[\n "foo"\n]'); expect(formatNativeQuery({ a: 1 }, "mongo")).toEqual('{\n "a": 1\n}'); + expect(formatNativeQuery('["foo"]', "mongo")).toEqual('["foo"]'); }); });